Web Analytics

zigtui

⭐ 90 stars Traditional Chinese by adxdits

🌐 語言

ZigTUI

跨平台 Zig TUI 函式庫,靈感來自 Ratatui

ZigTUI Dashboard

特色

系統需求

安裝

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") }, }, }), });

替代方案:Git 子模組

git submodule add https://github.com/adxdits/zigtui.git libs/zigtui

const zigtui_module = b.addModule("zigtui", .{
    .root_source_file = b.path("libs/zigtui/src/lib.zig"),
});

快速開始

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); } }

小工具

// 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

Themes

const theme = tui.themes.catppuccin_mocha;

tui.widgets.Block{ .title = "Dashboard", .style = theme.baseStyle(), .border_style = theme.borderFocusedStyle(), };

可用主題: defaultnorddraculamonokaigruvbox_darkgruvbox_lightsolarized_darksolarized_lighttokyo_nightcatppuccin_mochacatppuccin_latteone_darkcyberpunkmatrixhigh_contrast

執行 zig build run-themes 以預覽所有主題。

範例

zig build run-dashboard   # System monitor demo
zig build run-kitty       # Image display demo  
zig build run-themes      # Theme showcase

Kitty 圖形

在支援 Kitty 圖形協議 的終端機顯示圖片。會自動回退到 Unicode 區塊。

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 }

支援: Kitty、WezTerm、foot、Konsole(部分支援) 備用: Windows Terminal、iTerm2、Terminal.app

平台支援

| 平台 | 終端機 | 備註 | |-------------|--------------------------|--------------------| | Windows 10+ | Windows Terminal、WezTerm | 原生 Console API | | Linux | 任何 ANSI 相容終端機 | POSIX termios | | macOS | Kitty、WezTerm、Terminal.app | POSIX termios |

授權條款

MIT

貢獻指南

歡迎提出問題和 PR。

--- Tranlated By Open Ai Tx | Last indexed: 2026-02-06 ---