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
座標が範囲外の場合、ダミーのセルが返されます。

画面は 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 列または n 行を使用

これらの値は ftxui::Screen::Create() に渡されます。

ftxui::Screen::Create() は2つのオーバーロードを提供します。

  • 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() を使って手動で 設定することもできます。