Web Analytics

AppCUI-rs

⭐ 376 stars Simplified Chinese by gdt050579

🌐 语言

AppCUI-rs

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


Windows 构建状态
Linux 构建状态
macOS 构建状态
代码覆盖率
许可证
Crates.io
Docs.rs
Gallery

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 序列和 macOS 的低级 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