定制 xocdr/ext-tui 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xocdr/ext-tui

Composer 安装命令:

pie install xocdr/ext-tui

包简介

Terminal UI extension with Yoga layout engine for PHP (inspired by React INK)

README 文档

README

ext-tui logo

ext-tui

A PHP C extension for building terminal user interfaces with component-based architecture and Yoga flexbox layout.

Features

  • Flexbox Layout: Facebook's Yoga layout engine for CSS-like flexbox positioning
  • Component-Based: Build UIs with ContainerNode and ContentNode components
  • Full UTF-8 Support: Proper handling of Unicode text including CJK wide characters
  • Rich Text Styling: Bold, italic, underline, colors (RGB), and more
  • Keyboard Input: Full keyboard event handling with modifiers (Ctrl, Alt, Shift)
  • Mouse Support: Click, scroll, and drag events with hit testing
  • Terminal Resize: Automatic layout recalculation on terminal resize
  • Clipboard: OSC 52 clipboard read/write (works over SSH)
  • Hyperlinks: Clickable OSC 8 terminal hyperlinks
  • Bracketed Paste: Detect and handle pasted text
  • Focus Management: Tab navigation, focus groups, focus traps
  • Drawing Primitives: Lines, rectangles, circles, ellipses, triangles
  • Canvas Graphics: High-resolution drawing with Braille, block, or ASCII modes
  • Animation: Easing functions, interpolation, color gradients
  • Sprites: Frame-based animated sprites with collision detection
  • Tables: Formatted table rendering with alignment and borders
  • Progress Indicators: Progress bars, busy bars, and spinners
  • Timers: Interval-based callbacks for animations and updates
  • High Performance: All rendering done in C with minimal PHP overhead

Requirements

  • PHP 8.0+
  • C compiler with C++20 support (for Yoga)
  • Unix-like operating system (macOS, Linux)
  • Terminal with UTF-8 support

Installation

From Source

# Clone the repository
git clone https://github.com/xocoder/ext-tui.git
cd ext-tui

# Build the extension
phpize
./configure --enable-tui
make

# Run tests
make test

# Install (requires root)
sudo make install

# Add to php.ini
echo "extension=tui.so" >> $(php -i | grep "Loaded Configuration File" | cut -d' ' -f5)

Verify Installation

php -m | grep tui
# Output: tui

php -r "var_dump(tui_get_terminal_size());"
# Output: array(2) { [0]=> int(80) [1]=> int(24) }

Quick Start

Hello World

<?php
use Xocdr\Tui\Ext\ContainerNode;
use Xocdr\Tui\Ext\ContentNode;

$instance = tui_render(function() {
    $box = new ContainerNode(['padding' => 1, 'borderStyle' => 'round']);
    $box->addChild(new ContentNode(['content' => 'Hello, World!', 'bold' => true, 'color' => [100, 255, 100]]));
    return $box;
});

$instance->waitUntilExit();

Interactive Counter

<?php
use Xocdr\Tui\Ext\ContainerNode;
use Xocdr\Tui\Ext\ContentNode;

$count = 0;

$instance = tui_render(function() use (&$count) {
    $box = new ContainerNode([
        'flexDirection' => 'column',
        'padding' => 2,
        'gap' => 1
    ]);

    $box->addChild(new ContentNode(['content' => "Counter: $count", 'bold' => true]));
    $box->addChild(new ContentNode(['content' => "Press UP/DOWN to change, Ctrl+C to exit", 'dim' => true]));

    return $box;
}, [
    'fullscreen' => true,
    'exitOnCtrlC' => true
]);

tui_set_input_handler($instance, function($key) use (&$count, $instance) {
    if ($key->upArrow) $count++;
    if ($key->downArrow) $count--;
    $instance->rerender();
});

$instance->waitUntilExit();

Drawing with Canvas

<?php
// Create a high-resolution canvas using Braille characters
$canvas = tui_canvas_create(80, 40, "braille");

// Draw shapes
tui_canvas_circle($canvas, 40, 20, 15);
tui_canvas_line($canvas, 0, 0, 80, 40);

// Render to strings
$lines = tui_canvas_render($canvas);
foreach ($lines as $line) {
    echo $line . "\n";
}

Progress Bar with Buffer

<?php
$buffer = tui_buffer_create(80, 24);

for ($i = 0; $i <= 100; $i += 5) {
    tui_buffer_clear($buffer);
    tui_render_progress_bar($buffer, 0, 0, 40, $i / 100);
    tui_buffer_render($buffer);
    usleep(100000);
}

Documentation

Full documentation is available in the docs/ folder:

Manual (Tutorials & Guides)

Reference

  • Functions - Complete function reference
  • Classes - ContainerNode, ContentNode, Key, Instance
  • Constants - TUI_EASE_, TUI_CANVAS_, etc.

Specifications

API Overview

Application Lifecycle

tui_render(callable $component, array $options = []): TuiInstance
tui_rerender(TuiInstance $instance): void
tui_unmount(TuiInstance $instance): void
tui_wait_until_exit(TuiInstance $instance): void

Event Handlers

tui_set_input_handler(TuiInstance $instance, callable $handler): void
tui_set_focus_handler(TuiInstance $instance, callable $handler): void
tui_set_resize_handler(TuiInstance $instance, callable $handler): void
tui_set_tick_handler(TuiInstance $instance, callable $handler): void

Focus Management

tui_focus_next(TuiInstance $instance): void
tui_focus_prev(TuiInstance $instance): void
tui_get_focused_node(TuiInstance $instance): ?array

Timers

tui_add_timer(TuiInstance $instance, int $intervalMs, callable $callback): int
tui_remove_timer(TuiInstance $instance, int $timerId): void

Terminal Info

tui_get_terminal_size(): array           // [width, height]
tui_get_size(TuiInstance $instance): ?array
tui_is_interactive(): bool
tui_is_ci(): bool

Text Utilities

tui_string_width(string $text): int
tui_wrap_text(string $text, int $width): array
tui_truncate(string $text, int $width, string $ellipsis = "...", string $position = "end"): string
tui_pad(string $text, int $width, string $align = "l", string $pad_char = " "): string

Drawing Buffers

tui_buffer_create(int $width, int $height): resource
tui_buffer_clear(resource $buffer): void
tui_buffer_render(resource $buffer): void

Drawing Primitives

tui_draw_line(resource $buffer, int $x1, int $y1, int $x2, int $y2, array $style): void
tui_draw_rect(resource $buffer, int $x, int $y, int $w, int $h, array $style): void
tui_fill_rect(resource $buffer, int $x, int $y, int $w, int $h, array $style): void
tui_draw_circle(resource $buffer, int $cx, int $cy, int $r, array $style): void
tui_fill_circle(resource $buffer, int $cx, int $cy, int $r, array $style): void
tui_draw_ellipse(resource $buffer, int $cx, int $cy, int $rx, int $ry, array $style): void
tui_fill_ellipse(resource $buffer, int $cx, int $cy, int $rx, int $ry, array $style): void
tui_draw_triangle(resource $buffer, int $x1, int $y1, int $x2, int $y2, int $x3, int $y3, array $style): void
tui_fill_triangle(resource $buffer, int $x1, int $y1, int $x2, int $y2, int $x3, int $y3, array $style): void

Canvas (High-Resolution Drawing)

tui_canvas_create(int $width, int $height, string $mode = "braille"): resource
tui_canvas_set(resource $canvas, int $x, int $y): void
tui_canvas_unset(resource $canvas, int $x, int $y): void
tui_canvas_toggle(resource $canvas, int $x, int $y): void
tui_canvas_get(resource $canvas, int $x, int $y): bool
tui_canvas_clear(resource $canvas): void
tui_canvas_line(resource $canvas, int $x1, int $y1, int $x2, int $y2): void
tui_canvas_rect(resource $canvas, int $x, int $y, int $w, int $h): void
tui_canvas_fill_rect(resource $canvas, int $x, int $y, int $w, int $h): void
tui_canvas_circle(resource $canvas, int $cx, int $cy, int $r): void
tui_canvas_fill_circle(resource $canvas, int $cx, int $cy, int $r): void
tui_canvas_set_color(resource $canvas, int $r, int $g, int $b): void
tui_canvas_get_resolution(resource $canvas): array
tui_canvas_render(resource $canvas): array

Animation

tui_ease(float $t, string $easing = "linear"): float
tui_lerp(float $a, float $b, float $t): float
tui_lerp_color(array $a, array $b, float $t): string
tui_gradient(array $from, array $to, int $steps): array
tui_color_from_hex(string $hex): array

Tables

tui_table_create(array $headers): resource
tui_table_add_row(resource $table, array $cells): void
tui_table_set_align(resource $table, int $column, bool $right_align): void
tui_table_render_to_buffer(resource $buffer, resource $table, int $x, int $y, string $border = "single"): int

Progress Indicators

tui_render_progress_bar(resource $buffer, int $x, int $y, int $width, float $progress): void
tui_render_busy_bar(resource $buffer, int $x, int $y, int $width, int $frame, string $style_name = "pulse"): void
tui_spinner_frame(string $type, int $frame): string
tui_spinner_frame_count(string $type): int
tui_render_spinner(resource $buffer, int $x, int $y, string $type, int $frame): void

Sprites

tui_sprite_create(array $frames, string $name = "default", bool $loop = true): resource
tui_sprite_update(resource $sprite, int $delta_ms): void
tui_sprite_set_animation(resource $sprite, string $name): bool
tui_sprite_set_position(resource $sprite, int $x, int $y): void
tui_sprite_flip(resource $sprite, bool $flipped): void
tui_sprite_set_visible(resource $sprite, bool $visible): void
tui_sprite_render(resource $buffer, resource $sprite): void
tui_sprite_get_bounds(resource $sprite): array
tui_sprite_collides(resource $a, resource $b): bool

Classes

All classes are in the Xocdr\Tui\Ext namespace.

ContainerNode

Flexbox container component.

use Xocdr\Tui\Ext\ContainerNode;

$box = new ContainerNode([
    'flexDirection' => 'column',     // 'row', 'column', 'row-reverse', 'column-reverse'
    'alignItems' => 'center',        // 'flex-start', 'center', 'flex-end', 'stretch'
    'justifyContent' => 'center',    // 'flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'
    'width' => '100%',
    'height' => 10,
    'padding' => 1,
    'gap' => 1,
    'borderStyle' => 'round',        // 'single', 'double', 'round', 'bold'
    'borderColor' => [100, 150, 255],
    'focusable' => true,
]);
$box->addChild($child);

ContentNode

Text display component.

use Xocdr\Tui\Ext\ContentNode;

$text = new ContentNode([
    'content' => 'Hello!',
    'color' => '#00ff00',            // or [0, 255, 0]
    'backgroundColor' => [50, 50, 50],
    'bold' => true,
    'italic' => true,
    'underline' => true,
    'dim' => false,
    'inverse' => false,
    'strikethrough' => false,
]);

Instance

Running TUI application handle.

$instance->rerender();        // Force re-render
$instance->unmount();         // Stop and cleanup
$instance->waitUntilExit();   // Block until exit
$instance->exit(0);           // Request exit with code

Key

Keyboard event object (passed to input handlers).

$key->key           // string: character pressed
$key->upArrow       // bool
$key->downArrow     // bool
$key->leftArrow     // bool
$key->rightArrow    // bool
$key->return        // bool (enter key)
$key->escape        // bool
$key->backspace     // bool
$key->delete        // bool
$key->tab           // bool
$key->ctrl          // bool
$key->alt           // bool
$key->meta          // bool
$key->shift         // bool

Constants

Mouse Tracking Modes

TUI_MOUSE_MODE_OFF, TUI_MOUSE_MODE_CLICK, TUI_MOUSE_MODE_BUTTON, TUI_MOUSE_MODE_ALL

String-Based Options

Many functions use string values instead of constants:

  • Easing: "linear", "in_quad", "out_quad", "in_out_quad", "in_cubic", "out_cubic", etc.
  • Canvas modes: "braille", "block", "ascii"
  • Spinner types: "dots", "line", "bounce", "circle"
  • Clipboard targets: "clipboard", "primary", "secondary"

See Constants Reference for complete list.

Known Limitations

  • macOS/Linux only: Uses POSIX APIs (termios, poll, signals)
  • No Windows support: Consider WSL for Windows users

See docs/limitations.md for full details.

Related Projects

  • xocdr/tui - PHP library with hooks wrapping ext-tui
  • xocdr/tui-widgets - Pre-built widget components

License

MIT License - see LICENSE

Credits

  • Yoga - Facebook's flexbox layout engine
  • Inspired by Ink for Node.js

xocdr/ext-tui 适用场景与选型建议

xocdr/ext-tui 是一款 基于 C 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「cli」 「extension」 「layout」 「terminal」 「flexbox」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 xocdr/ext-tui 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 xocdr/ext-tui 我们能提供哪些服务?
定制开发 / 二次开发

基于 xocdr/ext-tui 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: C

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-28