Web Analytics

AppCUI-rs

⭐ 358 stars Simplified Chinese by gdt050579

🌐 语言

AppCUI-rs

`` ⯈ 𝗔𝗽𝗽𝗖𝗨𝗜-𝗿𝘀 🖳

Windows 构建状态
Linux 构建状态
MacOS 构建状态
代码覆盖率
许可证
Crates.io
Docs.rs
图库

AppCUI-rs 是一个快速的跨平台 Rust 库,用于构建现代的基于文本的用户界面(TUI),拥有丰富的控件、主题和完整的 Unicode 支持——是 ncurses 和其他终端 UI 框架的替代方案。

✨ 特性

  • [x] 多种开箱即用的控件(按钮、标签、文本框、复选框、单选按钮、列表视图、树视图、组合框、日期/时间选择器、颜色选择器、标签页、手风琴等)。完整控件列表见这里
  • [x] 强大的布局系统,允许使用绝对坐标、相对坐标、停靠、对齐、锚点或枢轴定位来定位控件(详情见这里
  • [x] 菜单和工具栏
  • [x] 多平台支持(Windows 通过 API 和虚拟终端,Linux 通过 ncurses,MacOS 通过 termios)
  • [x] 多线程支持,允许后台任务
  • [x] 计时器
  • [x] 鼠标支持
  • [x] 剪贴板支持
  • [x] 颜色主题
  • [x] 支持 Unicode 字符
  • [x] 预定义对话框(消息框、输入框、颜色选择器、保存和打开对话框、文件夹导航等)
  • [x] 真彩色支持(每像素 24 位)针对支持的终端。

📸 截图

👉 查看图库以获取所有控件的完整演示!

🖥️ 后端

AppCUI 根据使用的操作系统支持多种后端:

  • Windows 控制台 - 基于 Win32 低级 API,设计用于经典 Windows 控制台
  • Windows VT - 基于 ANSI 序列,设计用于现代 Windows 虚拟终端
  • NCurses - 基于 Linux 环境下的 NCurses API
  • Termios - 基于 ANSI 序列和 MAC OSX 低级 API
  • Web 终端 - 设计用于 Web 实现(基于 webgl)
  • CrossTerm - 基于 crossterm crate,但通过功能标志启用
更多支持的后端信息请见 这里

🚀 快速开始

将以下内容添加到您的 Cargo.toml

toml [dependencies] appcui = "*"

然后创建一个新的 Rust 项目并添加以下代码:
rust use appcui::prelude::*;

fn main() -> Result<(), appcui::system::Error> { let mut app = App::new().build()?; let mut win = Window::new( "Test", LayoutBuilder::new().alignment(Alignment::Center).width(30).height(9).build(), window::Flags::Sizeable, ); win.add(Label::new( "Hello World !", LayoutBuilder::new().alignment(Alignment::Center).width(13).height(1).build(), )); app.add_window(win); app.run(); Ok(()) }

或者使用过程宏的更简洁版本:

rs use appcui::prelude::*;

fn main() -> Result<(), appcui::system::Error> { let mut app = App::new().build()?; let mut win = window!("Test,a:c,w:30,h:9"); win.add(label!("'Hello World !',a:c,w:13,h:1")); app.add_window(win); app.run(); Ok(()) }

然后运行项目,使用 cargo run。你应该会看到一个标题为 Test 的窗口,中央显示文本 Hello World !

🧪 示例

AppCUI-rs 附带了一组示例,帮助你快速入门。你可以在 examples 文件夹中找到它们,包括:

🛠️ 一个更复杂的示例

一个创建带按钮的窗口的示例,按下按钮时计数器会增加。

rust use appcui::prelude::*;

// Create a window that handles button events and has a counter #[Window(events = ButtonEvents)] struct CounterWindow { counter: i32 }

impl CounterWindow { fn new() -> Self { let mut w = Self { // set up the window title and position base: window!("'Counter window',a:c,w:30,h:5"), // initial counter is 1 counter: 1 }; // add a single button with the caption "1" (like the counter) w.add(button!("'1',d:b,w:20")); w } } impl ButtonEvents for CounterWindow { // When the button is pressed, this function will be called // with the handle of the button that was pressed // Since we only have one button, we don't need to store its handle // in the struct, as we will receive the handle via the on_pressed method fn on_pressed(&mut self, handle: Handle