此包最新版本(0.1.5)没有可用的许可证信息。

0.1.5 2024-09-20 00:09 UTC

This package is auto-updated.

Last update: 2024-09-20 00:09:26 UTC


README

Chewie 是一个帮助您使用 Laravel Prompts(Laravel 提示)构建基于文本的用户界面(TUIs)的包。它有助于减少一些样板代码,并提供了一些对齐、动画等辅助函数。

警告

此包目前正在积极开发中。API 可能会发生变化。文档也会随着时间的推移而改进。

安装

composer require joetannenbaum/chewie

注册渲染器

use App\Renderers\DemoRenderer;
use Chewie\Concerns\RegistersRenderers;

class Demo extends Prompt
{
    use RegistersRenderers;

    public function __construct()
    {
        $this->registerRenderer(DemoRenderer::class);
    }
}

您也可以告诉 Chewie 所有渲染器都位于特定的命名空间中,然后 Chewie 将自动解析您的渲染器。

例如,如果您在 AppServiceProvider 中调用以下内容:

use Chewie\Renderer;

class AppServiceProvider
{
    public function boot()
    {
        Renderer::setNamespace('App\\Renderers');
    }
}

然后,在注册渲染器时,您可以简单地做以下操作。Chewie 假设您的渲染器类将是您的应用类 + Renderer

use Chewie\Concerns\RegistersRenderers;

class Demo extends Prompt
{
    use RegistersRenderers;

    public function __construct()
    {
        // Will register App\Renderers\DemoRenderer
        $this->registerRenderer();
    }
}

绘制艺术作品

您可以在渲染器中将文件中的 ASCII 艺术打印到终端

use Chewie\Concerns\DrawsArt;

class DemoRenderer extends Renderer
{
    use DrawsArt;

    public function __invoke(Demo $prompt): string
    {
        // Returns a collection of the lines from your art,
        // assumes a ".txt" extension
        $this->artLines(storage_path('my-art/horse'))
            ->each($this->line(...));

        return $this;
    }
}

您还可以告诉 Chewie 所有艺术文件所在的位置

use Chewie\Art;

class AppServiceProvider
{
    public function boot()
    {
        Art::setDirectory(storage_path('my-art'));
    }
}

这使得您可以将 artLines 调用简化为

use Chewie\Concerns\DrawsArt;

class DemoRenderer extends Renderer
{
    use DrawsArt;

    public function __invoke(Demo $prompt): string
    {
        $this->artLines('horse')->each($this->line(...));

        return $this;
    }
}

对齐

Chewie 提供了一些帮助在终端内对齐内容的方法。

use Chewie\Concerns\Aligns;

class DemoRenderer extends Renderer
{
    use Aligns;

    public function __invoke(Demo $prompt): string
    {
        $width = $prompt->terminal()->cols();
        $height = $prompt->terminal()->lines();

        $lines = [
            'Hello!',
            'My name is Joe',
        ];

        $this->centerHorizontally($lines, $width)
            ->each($this->line(...));

        $this->centerVertically($lines, $height)
            ->each($this->line(...));

        $this->center($lines, $width, $height)
            ->each($this->line(...));

        $this->line($this->spaceBetween($width, ...$lines));

        $this->pinToBottom($height, function() {
            $this->newLine();
            $this->line('This is pinned to the bottom!');
        });

        return $this;
    }
}