Skip to content

Component Template

特徴

  • 非同期イベントに tokio を使用
    • vim などの別の TUI にシェルアウトするためのキー イベントの開始と停止
    • サスペンド シグナル フックをサポート
  • tracing を使用してログに記録
  • better-panic
  • color-eyre
  • human-panic
  • コマンドライン引数の解析に clap
  • Component trait と Home および Fps 例としてのコンポーネント

使用法

cargo-generate を使用することから始めることができます。

Terminal window
cargo install cargo-generate
cargo generate --git https://github.com/ratatui/templates component --name ratatui-hello-world
cd ratatui-hello-world

template.toml ファイルを使用して、プロンプトをスキップすることもできます。

Terminal window
$ cargo generate --git https://github.com/ratatui/templates component --template-values-file ./path/to/template.toml --name ratatui-hello-world
# OR generate from local clone
$ git clone https://github.com/ratatui/templates
$ cd templates
$ cargo generate --path ./component --template-values-file ./.github/workflows/template.toml --name ratatui-hello-world

走る

Terminal window
cargo run # Press `q` to exit

show help

Terminal window
$ cargo run -- --help
Hello World project using ratatui-template
Usage: ratatui-hello-world [OPTIONS]
Options:
-t, --tick-rate <FLOAT> Tick rate, i.e. number of ticks per second [default: 1]
-f, --frame-rate <FLOAT> Frame rate, i.e. number of frames per second [default: 60]
-h, --help Print help
-V, --version Print version

show version

direnv 変数なし:

Terminal window
$ cargo run -- --version
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/ratatui-hello-world --version`
ratatui-hello-world v0.1.0-47-eb0a31a
Authors: Dheepak Krishnamurthy
Config directory: /Users/kd/Library/Application Support/com.kdheepak.ratatui-hello-world
Data directory: /Users/kd/Library/Application Support/com.kdheepak.ratatui-hello-world

direnv 変数を使用:

Terminal window
$ direnv allow
direnv: loading ~/gitrepos/component-template/ratatui-hello-world/.envrc
direnv: export +RATATUI_HELLO_WORLD_CONFIG +RATATUI_HELLO_WORLD_DATA +RATATUI_HELLO_WORLD_LOG_LEVEL
$ # OR
$ export RATATUI_HELLO_WORLD_CONFIG=`pwd`/.config
$ export RATATUI_HELLO_WORLD_DATA=`pwd`/.data
$ export RATATUI_HELLO_WORLD_LOG_LEVEL=debug
$ cargo run -- --version
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/ratatui-hello-world --version`
ratatui-hello-world v0.1.0-47-eb0a31a
Authors: Dheepak Krishnamurthy
Config directory: /Users/kd/gitrepos/component-template/ratatui-hello-world/.config
Data directory: /Users/kd/gitrepos/component-template/ratatui-hello-world/.data

ドキュメント

テンプレートの設計上の決定に関するドキュメントは、こちらでご覧ください。 https://ratatui.rs/templates/component/

背景

ratatui は、リッチなターミナル ユーザー インターフェイス (TUI) とダッシュボードを構築するための Rust ライブラリです。これは、プロジェクトの維持と改善のために作成されたオリジナルの tui-rs のコミュニティ フォークです。

このプロジェクトのソース コード は、ratatui を起動して実行するための独自のテンプレートです。このテンプレートの各部分を、ニーズや感覚に合わせて選択できます。このドキュメントの残りの部分では、コードがこのように構成されている理由を詳しく説明し、必要に応じてコードを変更できるようにします。

ratatui は、中間バッファーを使用した即時レンダリングの原則に基づいています。つまり、新しいフレームごとに、UI の一部となるはずのすべてのウィジェットを構築する必要があります。つまり、ratatui ライブラリは主にターミナルへの描画のみを処理します。さらに、ライブラリは入力処理もイベント システムも提供しません。キーボード入力イベントを取得し、それらのイベントに基づいてアプリケーションの状態を変更し、どのウィジェットがアプリケーションの状態のビューを最もよく反映するかを判断する責任は、開発者にあります。

ratatui プロジェクトでは、基本事項を網羅したテンプレートが追加されました。こちらでご覧いただけます: https://github.com/ratatui/templates/tree/main/simple

私は、tokio を使用してコードを少し異なる方法で整理するテンプレートをもう一度試してみたいと思いました。これは、ratatui プロジェクトを整理する方法に関する独自の見解です。

このプロジェクトでは、ログ、コマンドライン引数、構成オプションなどのよく使用される依存関係も追加されます。

このドキュメントでは、機能するターミナル ユーザー インターフェイスを構築するために、コードとプロジェクトを整理するさまざまな方法について説明します。好きな部分を選択できます。

次のリンクも確認してください (おおよそ複雑さが増す順)。