aspectus/terminal
Composer 安装命令:
composer require aspectus/terminal
包简介
A terminal abstraction
README 文档
README
This is an open-source PHP package providing a small abstraction for the terminal. The idea is to see the terminal as a device, which can perform both input and output. The package relies on Amphp to provide non-blocking handling of the streams and an event dispatcher to handle input.
Furthermore, this package also provides an abstraction for Xterm that is using the terminal device, supporting most of the escape control sequences in the manual (patch #379) as well dispatching events for handling mouse movement.
Usage
Basic Output
use Aspectus\Terminal\TerminalDevice; $device = new TerminalDevice(); $device->write("This and that"); // will output "This and that" in STDOUT $device->error("There was some error"); // will output in STDERR
Basic Input
For handling the input you can rely on the supplied EventDispatcher.
$device->subscribe( InputEvent::class, function (InputEvent $event) { $event->device->write('Read: ' . bin2hex($event->data) . PHP_EOL); } ); $device->subscribe( InputEvent::class, function (InputEvent $event) { if ($event->data === 'q') { exit(); } } );
Device Input Events
The current implementation will distinguish between normal input events (InputEvent) and input that is an escape sequence (EscapeSequenceEvent), so your listeners can attach to the correct one. Unless you need to read some escape sequence that the terminal sends back to you, you probably need to listen to InputEvent.
InputEvent - the default event that is emitted.
EscapeSequenceEvent - this is emitted when input starts with an escape sequence
All events hold a reference to the device that dispatched them into the $event->device property.
For Xterm events see below.
Event Factory
The device by default has a minimal implementation for creating new events. You
can supply your own by implementing the EventFactoryInterface.
interface EventFactoryInterface { /** * Optionally creates an event to be dispatched * * @param string $received * @return InputEvent|null */ public function createEvent(string $received): ?InputEvent; }
Xterm abstraction
The package comes with an abstraction for Xterm that provides named methods to escape sequences in
a fluent interface. Xterm will buffer every call and write it to the device when flush() is called
or return it as a string when getBuffered() is called.
$xterm = new \Aspectus\Terminal\Xterm(); $xterm // we reset colors in the beginning ->default() ->bgDefault() ->normal() ->eraseDisplay() // position for first message ->moveCursorTo(5,5) // set style ->red() ->bgWhite() // write a message ->write('Hello world!') // position for second message ->moveCursorTo(8, 10) // set style ->bold() ->brightYellow() ->bgBlue() // return instructions ->write('This is an Xterm abstraction!') // write the whole buffer ->flush()
Xterm Input events
Using the Xterm abstraction will add a different EventFactory implementation which will emit some
additional events when they are received.
SpecialKeyEvent is emitted when function or arrow keys are pressed. The data property of
the event is mapped to a generic format, indicating function keys like <F2> and arrow keys
like <LEFT>, <RIGHT>.
MouseInputEvent - is emitted when mouse tracking is enabled and provides properties like x and
y as well as methods for the buttons (like button1()) to get information about which buttons
were pressed.
MouseFocusEvent - is emitted when mouse tracking and focus tracking have been enabled and provides
a way to attach to an event that will trigger when the window loses focus or gains it again.
More usage examples
More usage examples can be found in the examples/ directory.
Contribute
Everyone is welcome to contribute, please see CONTRIBUTE.md for more information.
Licence
MIT
aspectus/terminal 适用场景与选型建议
aspectus/terminal 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「async」 「terminal」 「asynchronous」 「ansi」 「vt100」 「xterm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aspectus/terminal 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aspectus/terminal 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aspectus/terminal 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Asynchronous coroutines for PHP 7.
A fork of runcmf/runtracy
PHP Terminal emulator controller utilizing ANSI escape sequence coding.
The bundle for easy using json-rpc api on your project
An async event for hyperf.
Symfony ResqueBundle
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-03