FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
ftxui / screen

title-img

ftxui::screen 模块是底层基础。它可以独立使用,但主要设计用于与 ftxui::domftxui::component 模块结合使用。

It provides a ftxui::Screen.


ftxui::Screen

ftxui::Screen 类表示一个 2D 样式字符网格,可以渲染到终端。 它提供了创建屏幕、访问像素和渲染元素的方法。

您可以使用 ftxui::Screen::PixelAt 方法访问屏幕的单个单元格 (ftxui::Pixel),该方法返回指定坐标处像素的引用。

Example

void main() {
auto screen = ftxui::Screen::Create(
ftxui::Dimension::Full(), // Use full terminal width
ftxui::Dimension::Fixed(10) // Fixed height of 10 rows
);
// Access a specific pixel at (10, 5)
auto& pixel = screen.PixelAt(10, 5);
// Set properties of the pixel.
pixel.character = U'X';
pixel.foreground_color = ftxui::Color::Red;
pixel.background_color = ftxui::Color::RGB(0, 255, 0);
pixel.bold = true; // Set bold style
screen.Print(); // Print the screen to the terminal
}
static Screen Create(Dimensions dimension)
创建一个具有给定维度的屏幕。
static Color RGB(uint8_t red, uint8_t green, uint8_t blue)
从其 RGB 表示构建一个颜色。 https://en.wikipedia.org/wiki/RGB_color_model
注解
如果坐标超出边界,则返回一个虚拟像素。

屏幕可以使用 ftxui::Screen::Print() 打印到终端,或使用 ftxui::Screen::ToString() 转换为 std::string。

  • Print()
    auto screen = ...;
    screen.Print();
  • ToString()
    auto screen = ...;
    std::cout << screen.ToString();

请注意,您可以在打印后通过调用 ftxui::Screen::ResetCursorPosition() 将光标位置重置到屏幕的左上角。

Example

auto screen = ...;
while(true) {
// Drawing operations:
...
// Print the screen to the terminal. Then reset the cursor position and the
// screen content.
std::cout << screen.ToString();
std::cout << screen.ResetCursorPosition(/*clear=*/true);
std::cout << std::flush;
// Sleep for a short duration to control the refresh rate.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

ftxui::Dimension

ftxui::Dimension 工具控制屏幕尺寸:

  • Dimension::Full() — 使用完整的终端宽度或高度
  • Dimension::Fit(element) — 尺寸以适应渲染的 ftxui::Element
  • Dimension::Fixed(n) — 精确使用 n 列或行

这些值将传递给 ftxui::Screen::Create()

ftxui::Screen::Create() 提供了两个重载:

  • Screen::Create(Dimension) 将宽度和高度都设置为相同类型的尺寸
  • Screen::Create(Dimension width, Dimension height) 允许对每个轴进行不同的控制
auto screen = ftxui::Screen::Create(
ftxui::Dimension::Full(), // width
ftxui::Dimension::Fixed(10) // height
);

Once created, render an element and display the result:

ftxui::Render(screen, element);
screen.Print();
void Render(Screen &screen, const Element &element)
在 ftxui::Screen 上显示元素。

ftxui::Pixel

屏幕网格中的每个单元格都是一个 ftxui::Pixel,它包含:

  • Unicode 码点。
    • character
  • ftxui::Color:
    • foreground_color
    • background_color
  • 布尔值:
    • blink
    • bold
    • dim
    • italic
    • inverted (交换前景色和背景色)
    • underlined
    • underlined_double
    • strikethrough
auto screen = ftxui::Screen::Create(
ftxui::Dimension::Fixed(5),
ftxui::Dimension::Fixed(5),
);
auto& pixel = screen.PixelAt(3, 3);
pixel.character = U'X';
pixel.bold = true;
pixel.foreground_color = ftxui::Color::Red;
pixel.background_color = ftxui::Color::RGB(0, 255, 0);
screen.Print();
注解
PixelAt(x, y) 执行边界检查并返回指定坐标处像素的引用。 如果超出边界,则返回一个虚拟像素引用。

屏幕中的每个单元格都是一个 ftxui::Pixel。您可以使用以下方式修改它们:

auto& pixel = screen.PixelAt(x, y);
pixel.character = U'X';
pixel.bold = true;
pixel.foreground_color = Color::Red;

ftxui::Color

ftxui::Color 类用于定义每个 ftxui::Pixel 的前景色和背景色。

它支持各种颜色空间和预定义调色板。如果终端不支持请求的颜色,FTXUI 将 动态回退到终端中最接近的可用颜色。

颜色空间

注解
您可以使用 ftxui::Terminal::ColorSupport() 查询终端功能;

这可以使用 ftxui::Terminal::SetColorSupport() 手动设置。