FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
ftxui / screen

title-img

ftxui::screen 模組是低階基礎。它可以單獨使用,但主要設計為與 ftxui::dom 和 ftxui::component 模組一起使用。

它提供了一個 ftxui::Screen。


ftxui::Screen

ftxui::Screen 類別表示一個由已設定樣式的字元組成的 2D 網格,可以 被渲染到終端機。
它提供了建立畫面、存取儲存格與渲染元素的方法。

你可以使用 ftxui::Screen::CellAt 方法存取畫面上個別的儲存格(ftxui::Cell), 此方法會回傳指定座標處儲存格的參考。

範例

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 cell at (10, 5)
auto& cell = screen.CellAt(10, 5);
// Set properties of the cell.
cell.character = "X";
cell.foreground_color = ftxui::Color::Red;
cell.background_color = ftxui::Color::RGB(0, 255, 0);
cell.bold = true; // Set bold style
screen.Print(); // Print the screen to the terminal
}
Note
如果座標超出範圍,會回傳一個虛設(dummy)儲存格。

螢幕可以使用 ftxui::Screen::Print() 列印到終端機,或使用 ftxui::Screen::ToString() 轉換為 std::string。

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

注意,你可以在列印後透過呼叫 ftxui::Screen::ResetPosition() 將游標位置重設回畫面的左上角。

範例

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.ResetPosition(/*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
);

建立完成後,渲染一個元素並顯示結果:

ftxui::Render(screen, element);
screen.Print();
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105

ftxui::Cell

畫面網格中的每個儲存格都是一個 ftxui::Cell,它保存了:

  • 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& cell = screen.CellAt(3, 3);
cell.character = "X";
cell.bold = true;
cell.foreground_color = ftxui::Color::Red;
cell.background_color = ftxui::Color::RGB(0, 255, 0);
screen.Print();
Note
CellAt(x, y) 會執行邊界檢查,並回傳指定座標處 儲存格的參考。如果超出範圍,則會回傳一個虛設 儲存格的參考。

畫面中的每個儲存格都是一個 ftxui::Cell。你可以使用以下方式修改它們:

auto& cell = screen.CellAt(x, y);
cell.character = "X";
cell.bold = true;
cell.foreground_color = Color::Red;

ftxui::Color

ftxui::Color 類別用於定義每個 ftxui::Cell 的前景色與背景色。

它支援各種色彩空間和預定義調色板。如果終端機不支援請求的顏色,FTXUI 將 動態回退到終端機中最接近的可用顏色。

色彩空間

Note
你可以使用 ftxui::Terminal::ColorSupport() 查詢終端機的能力;

這也可以透過 ftxui::Terminal::SetColorSupport() 手動設定。