php-tui/term 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

php-tui/term

Composer 安装命令:

composer require php-tui/term

包简介

comprehensive low level terminal control

README 文档

README

CI

Term Logo

Low-level terminal control library heavily inspired by crossterm.

Table of Contents

Installation

$ composer require php-tui/term

Requirements

I have only tested this library on Linux. It currently requires stty to enable the raw mode and detect the current window size. It should work on MacOS and WSL.

Native Windows is currently not supported as I cannot test on Windows, the architecture should support Windows however, so if you'd like to make a start look at crossterm for inspiration and start a PR.

Usage

Actions

You can send data to the terminal using actions.

<?php

$terminal = Terminal::new();

// queue an action
$terminal->queue(Actions::printString('Hello World'));

// flush the queue to the terminal
$terminal->flush();

// or you can execute it directly
$terminal->execute(Actions::printString('Hello World'));

All actions are made available via. the Actions factory:

method description
Actions::requestCursorPosition Request the cursor position.

This will (hopefully) be returned by the terminal and will be provided
as an PhpTui\Term\Event\CursorPositionEvent.
Actions::alternateScreenEnable Enable the alternate screen.

Allows switching back to the users previous "screen" later.
Actions::alternateScreenDisable Disable the alternate screen
Actions::printString Echo a standard string to the terminal
Actions::cursorShow Show the cursor
Actions::cursorHide Hide the cursor
Actions::setRgbForegroundColor Set the foreground color using RGB
Actions::setRgbBackgroundColor Set the background color using RGB
Actions::setForegroundColor Set the foreground color to one of the ANSI base colors
Actions::setBackgroundColor Set the background color to one of the ANSI base colors
Actions::moveCursor Move the cursor to an absolute position.

The top left cell is 0,0.
Actions::reset Reset all modes (styles and colors)
Actions::bold Enable or disable the bold styling
Actions::dim Enable or disable the dim styling
Actions::italic Enable or disable the italic styling
Actions::underline Enable or disable the underline styling
Actions::slowBlink Enable or disable the slow blink styling
Actions::rapidBlink Enable or disable the rapid blink styling
Actions::reverse Enable or disable the reverse blink styling
Actions::hidden Enable or disable the hidden styling - useful for passwords.
Actions::strike Enable or disable the strike-through styling
Actions::clear Perform a clear operation.

The type of clear operation is given with the Enum for example

Actions::clear(ClearType::All)

Will clear the entire screen.
Actions::enableMouseCapture Enable mouse capture.

Once this action has been issued mouse events will be made available.
Actions::disableMouseCapture Disable mouse capture
Actions::scrollUp Scroll the terminal up the given number of rows
Actions::scrollDown Scroll the terminal down the given number of rows
Actions::setTitle Set the title of the terminal for the current process.
Actions::lineWrap Enable or disable line wrap
Actions::moveCursorNextLine Move the cursor down and to the start of the next line (or the given number of lines)
Actions::moveCursorPreviousLine Move the cursor up and to the start of the previous line (or the given number of lines)
Actions::moveCursorToColumn Move the cursor to the given column (0 based)
Actions::moveCursorToRow Move the cursor to the given row (0 based)
Actions::moveCursorUp Move cursor up 1 or the given number of rows.
Actions::moveCursorRight Move cursor right 1 or the given number of columns.
Actions::moveCursorDown Move cursor down 1 or the given number of rows.
Actions::moveCursorLeft Move cursor left 1 or the given number of columns.
Actions::saveCursorPosition Save the cursor position
Actions::restoreCursorPosition Restore the cursor position
Actions::enableCusorBlinking Enable cursor blinking
Actions::disableCursorBlinking Disable cursor blinking
Actions::setCursorStyle Set the cursor style

Events

Term provides user events:

while (true) {
    while ($event = $terminal->events()->next()) {
        if ($event instanceof CodedKeyEvent) {
            if ($event->code === KeyCode::Esc) {
                // escape pressed
            }
        }
        if ($event instanceof CharKeyEvent) {
            if ($event->char === 'c' && $event->modifiers === KeyModifiers::CONTROL) {
                // ctrl-c pressed
            }
        }
    }
    usleep(10000);
}

The events are as follows:

  • PhpTui\Term\Event\CharKeyEvent: Standard character key
  • PhpTui\Term\Event\CodedKeyEvent: Special key, e.g. escape, control, page up, arrow down, etc
  • PhpTui\Term\Event\CursorPositionEvent: as a response to Actions::requestCursorPosition.
  • PhpTui\Term\Event\FocusEvent: for when focus has been gained or lost
  • PhpTui\Term\Event\FunctionKeyEvent: when a function key is pressed
  • PhpTui\Term\Event\MouseEvent: When the Actions::enableMouseCapture has been called, provides mouse event information.
  • PhpTui\Term\Event\TerminalResizedEvent: The terminal was resized.

Terminal Size

You can request the terminal size:

<?php

$terminal = Terminal::new();

$size = $terminal->info(Size::class);
if (null !== $size) {
    echo $size->__toString() . "\n";
} else {
    echo 'Could not determine terminal size'."\n";
}

Raw Mode

Raw mode disables all the default terminal behaviors and is what you typically want to enable when you want a fully interactive terminal.

<?php

$terminal = Terminal::new();
$terminal->enableRawMode();
$terminal->disableRawMode();

Always be sure to disable raw mode as it will leave the terminal in a barely useable state otherwise!

Parsing

In addition Term provides a parser which can parse any escape code emitted by the actions.

This is useful if you want to capture the output from a terminal application and convert it to a set of Actions which can then be redrawn in another medium (e.g. plain text or HTML).

use PhpTui\Term\AnsiParser;
$actions = AnsiParser::parseString($rawAnsiOutput, true);

Testing

The Terminal has testable versions of all it's dependencies:

<?php

$painter = ArrayPainter::new();
$eventProvider = ArrayEventProvider::fromEvents(
    CharKeyEvent::new('c')
);
$infoProvider = ClosureInformationProvider::new(
    function (string $classFqn): TerminalInformation {
        return new class implements TerminalInformation {};
    }
);
$rawMode = new TestRawMode();

$term = Terminal::new(
    painter: $painter,
    infoProvider: $infoProvider,
    eventProvider: $eventProvider,
    rawMode: $rawMode
);
$term->execute(
    Actions::printString('Hello World'),
    Actions::setTitle('Terminal Title'),
);

echo implode("\n", array_map(
    fn (Action $action) => $action->__toString(),
    $painter->actions()
)). "\n";

See the example testable.php in examples/.

Contributing

PRs for missing functionalities and improvements are charactr.

php-tui/term 适用场景与选型建议

php-tui/term 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.04M 次下载、GitHub Stars 达 45, 最近一次更新时间为 2023 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.04M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 45
  • 点击次数: 26
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 3
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-27