xico2k/termwind-plugin-live

Termwind 的实时插件。

dev-main 2023-03-06 16:54 UTC

This package is auto-updated.

Last update: 2024-09-06 20:12:12 UTC


README

Termwind logo

Termwind Live 插件

GitHub Workflow Status (master) Total Downloads Latest Version License

Termwind Live 插件 允许您使您的 cli 内容实时更新。

安装

需要 PHP 8.0+

使用 Composer 安装 Termwind Live 插件

composer require xico2k/termwind-plugin-live

使用方法

use function Termwind\Live\live;

live(function () {
    static $total = 0;

    return sprintf('The content was refreshed %d times.', ++$total);
})->refreshEvery(seconds: 1);

refreshEvery()

可以使用 refreshEvery 方法通过一定时间间隔更新内容。

可用方法

  • refreshEvery(milliseconds: 0, seconds: 0)
  • refreshEverySeconds($seconds)
  • refreshEveryMilliseconds($seconds)
// Seconds
live(fn () => 'foo')->refreshEverySeconds(1); // or
live(fn () => 'foo')->refreshEvery(seconds: 1);

// Milliseconds
live(fn () => 'foo')->refreshEveryMilliseconds(250); // or
live(fn () => 'foo')->refreshEvery(milliseconds: 1);

while(Closure $condition)

可以使用 while() 方法在条件为 true 时更新内容。

live(fn () => 'Loading...')
    ->while(fn () => $process->running());

showCursor()hideCursor()

可以使用 showCursor()hideCursor() 方法在您的 cli 上显示/隐藏光标。

live('Loading...')
    ->hideCursor()
    ->while(fn () => $process->running())
    ->showCursor();

clear()

可以使用 clear() 方法清除实时输出。

live('Loading...')
    ->while(fn () => $process->running())
    ->clear();

RefreshEvent

RefreshEvent 被传递到 Closure 中,并提供了一种停止执行的方式。

use Termwind\Live\Events\RefreshEvent;

live(function (RefreshEvent $event) {
    $shouldStop = true; // Call to check something...

    if ($shouldStop) {
        $event->stop();
    }

    return 'foo';
})->refreshEvery(seconds: 1);

Termwind Live 插件是一个开源软件,采用 MIT 许可证