ZigTUI
Thư viện TUI đa nền tảng cho Zig, lấy cảm hứng từ Ratatui.

Tính năng
- Đa nền tảng — Windows, Linux, macOS
- Kết xuất hiệu quả — So sánh theo ô, chỉ vẽ lại những gì thay đổi
- Widget — Khối, Đoạn, Danh sách, Đồng hồ đo, Bảng
- 15 chủ đề tích hợp sẵn — Nord, Dracula, Gruvbox, Catppuccin, Tokyo Night...
- Giao thức Đồ họa Kitty — Hiển thị hình ảnh (hỗ trợ thay thế bằng Unicode)
- Không cấp phát ẩn — Quản lý bộ nhớ rõ ràng
Yêu cầu
- Zig 0.15.0+
- Windows 10+ / Linux / macOS
Cài đặt
zig fetch --save git+https://github.com/adxdits/zigtui.git// build.zig
const zigtui = b.dependency("zigtui", .{ .target = target, .optimize = optimize });const exe = b.addExecutable(.{
.name = "myapp",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zigtui", .module = zigtui.module("zigtui") },
},
}),
});
Phương án thay thế: Git submodule
git submodule add https://github.com/adxdits/zigtui.git libs/zigtuiconst zigtui_module = b.addModule("zigtui", .{
.root_source_file = b.path("libs/zigtui/src/lib.zig"),
});
Khởi động nhanh
const std = @import("std");
const tui = @import("zigtui");pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var backend = try tui.backend.init(allocator);
defer backend.deinit();
var terminal = try tui.terminal.Terminal.init(allocator, backend.interface());
defer terminal.deinit();
try terminal.hideCursor();
defer terminal.showCursor() catch {};
var running = true;
while (running) {
const event = try backend.interface().pollEvent(100);
if (event == .key) {
if (event.key.code == .esc or (event.key.code == .char and event.key.code.char == 'q'))
running = false;
}
try terminal.draw({}, struct {
fn render(_: void, buf: *tui.render.Buffer) !void {
tui.widgets.Block{
.title = "Hello ZigTUI — press 'q' to quit",
.borders = tui.widgets.Borders.all(),
.border_style = .{ .fg = .cyan },
}.render(buf.getArea(), buf);
}
}.render);
}
}
Tiện ích
// Block — container with border/title
tui.widgets.Block{ .title = "Panel", .borders = tui.widgets.Borders.all() }// Paragraph — text display
tui.widgets.Paragraph{ .text = "Hello!", .wrap = true }
// List — scrollable items
tui.widgets.List{ .items = &items, .selected = 0, .highlight_style = .{ .bg = .blue } }
// Gauge — progress bar
tui.widgets.Gauge{ .ratio = 0.75, .label = "75%" }
// Table — tabular data
tui.widgets.Table{ .header = &columns, .rows = &rows }
Themes

const theme = tui.themes.catppuccin_mocha;tui.widgets.Block{
.title = "Dashboard",
.style = theme.baseStyle(),
.border_style = theme.borderFocusedStyle(),
};
Có sẵn: default, nord, dracula, monokai, gruvbox_dark, gruvbox_light, solarized_dark, solarized_light, tokyo_night, catppuccin_mocha, catppuccin_latte, one_dark, cyberpunk, matrix, high_contrast
Chạy lệnh zig build run-themes để xem trước tất cả các chủ đề.
Ví dụ
zig build run-dashboard # System monitor demo
zig build run-kitty # Image display demo
zig build run-themes # Theme showcaseĐồ họa Kitty
Hiển thị hình ảnh trên các terminal hỗ trợ Giao thức Đồ họa Kitty. Tự động chuyển sang các khối Unicode nếu không hỗ trợ.
var gfx = tui.Graphics.init(allocator);
defer gfx.deinit();var bmp = try tui.graphics.bmp.loadFile(allocator, "image.bmp");
const image = tui.Image{ .data = bmp.data, .width = bmp.width, .height = bmp.height, .format = .rgba };
if (gfx.supportsImages()) {
if (try gfx.drawImage(image, .{ .x = 0, .y = 0 })) |seq| try backend.write(seq);
} else {
gfx.renderImageToBuffer(image, buffer, area); // Unicode fallback
}
Được hỗ trợ: Kitty, WezTerm, foot, Konsole (một phần) Dự phòng: Windows Terminal, iTerm2, Terminal.app
Hỗ trợ nền tảng
| Nền tảng | Terminal | Ghi chú | |----------|----------|--------| | Windows 10+ | Windows Terminal, WezTerm | Native Console API | | Linux | Bất kỳ tương thích ANSI | POSIX termios | | macOS | Kitty, WezTerm, Terminal.app | POSIX termios |
Giấy phép
MIT
Đóng góp
Hoan nghênh các vấn đề và PR.
--- Tranlated By Open Ai Tx | Last indexed: 2026-02-06 ---