【Laravel Prompts】v0.3.17までに追加された関数諸々

Laravel

前回、v0.3.15で追加された関数についての記事を書きました。

【Laravel Prompts】v0.3.15で追加された関数諸々
Laravel13リリースに向けての機能追加と思われますが、日本時間2026/03/17深夜(Laravel13リリース直前)にリリースされたLaravel Prompts v0.3.15にいくつかの新しい関数が追加されました。(Title/Stream/Task/Autocomplete/Notify)ひとつひとつ使い方を見ていきます。

今回は、v0.3.16/v0.3.17で追加された関数について書いていきます。

DataTable Prompt by joetannenbaum ?? Pull Request #231 ?? laravel/prompts
Adds a new datatable() prompt that lets users browse, search, and select from tabular data.$selected = datatable( label:…
Added `keepSummary`, `subLabel`, truncated labels and stable messages by joetannenbaum ツキ Pull Request #235 ツキ laravel/prompts
A few additions to the task() prompt:keepSummary: true keeps the task's stable success/warning/error messages on screen …

この記事で確認する機能

  • Datatable Prompt
  • Task Promptの追加機能(keepSummary, subLable)

Datatable Prompt

Datatable Prompt は、表形式で表示されたデータから選択したデータを取得するプロンプトです。

DataTable Prompt by joetannenbaum ?? Pull Request #231 ?? laravel/prompts
Adds a new datatable() prompt that lets users browse, search, and select from tabular data.$selected = datatable( label:…

プルリクの内容を見てみましょう。

新しいdatatable()プロンプトを追加し、ユーザーが表形式のデータからブラウズ、検索、選択ができるようにしました。

$selected = datatable(
    label: 'Select a team member',
    headers: ['Name', 'Twitter', 'Role'],
    rows: [
        'taylor' => ['Taylor Otwell', '@taylorotwell', 'CEO'],
        'jess'   => ['Jess Archer', '@jessarchercodes', 'Developer'],
        // ...
    ],
);
  • キーボード操作: 矢印キー、Page Up/Down、Home/End(ラップ付き)
  • 組み込み検索: /を押すと全列で一致する部分文字列の行をフィルタリング
  • カスタムフィルタリング: カスタム検索ロジック用のフィルタークロージャーを渡す

関数定義を見てみます。

▼「vendor/laravel/prompts/src/helpers.php」

    function datatable(
        array|Collection $headers = [],
        array|Collection|null $rows = null,
        int $scroll = 10,
        string $label = '',
        string $hint = '',
        bool|string $required = false,
        mixed $validate = null,
        ?Closure $transform = null,
        ?Closure $filter = null,
    ): mixed {
        return (new DataTablePrompt(
            headers: $headers,
            rows: $rows,
            scroll: $scroll,
            label: $label,
            hint: $hint,
            required: $required,
            validate: $validate,
            transform: $transform,
            filter: $filter,
        ))->prompt();
    }

▼コード例

<?php

require __DIR__ . '/../vendor/autoload.php';

use function Laravel\Prompts\datatable;

$members = [
    [1, 'John Doe', 'john.doe@example.com', 'Developer'],
    [2, 'Jane Smith', 'jane.smith@example.com', 'Designer'],
    [3, 'Alice Johnson', 'alice.johnson@example.com', 'Manager'],
    [4, 'Bob Brown', 'bob.brown@example.com', 'Tester'],
    [5, 'Charlie Davis', 'charlie.davis@example.com', 'Developer'],
    [6, 'Eve Wilson', 'eve.wilson@example.com', 'Designer'],
    [7, 'Frank Miller', 'frank.miller@example.com', 'Manager'],
    [8, 'Grace Lee', 'grace.lee@example.com', 'Developer'],
    [9, 'Hank Taylor', 'hank.taylor@example.com', 'Tester'],
    [10, 'Ivy Anderson', 'ivy.anderson@example.com', 'Manager'],
];

$selected = datatable(
    label: 'Select a team member:',
    headers: ['ID', 'Name', 'Email', 'Role'],
    rows: $members,
    scroll: 5,
    filter: fn ($row, $query) => str_contains(strtolower($row[1]), strtolower($query))
        || str_contains(strtolower($row[2]), strtolower($query))
        || str_contains(strtolower($row[3]), strtolower($query)),
);

$member = $members[$selected];

echo "You selected: {$member[0]}: {$member[1]} ({$member[2]}) [{$member[3]}]\n";

▼実行結果

※途中で、「/Devel」で選択肢のフィルタリングをしています。

Task Promptの追加機能

Task Promptはv0.3.15で追加されたものですが、今回のアップデートで機能が追加されています。

Added `keepSummary`, `subLabel`, truncated labels and stable messages by joetannenbaum ツキ Pull Request #235 ツキ laravel/prompts
A few additions to the task() prompt:keepSummary: true keeps the task's stable success/warning/error messages on screen …

プルリクの内容を見てみましょう。

task()プロンプトへのいくつかの追加点:

  • keepSummary: trueは、コールバック終了後もタスクの安定した成功/警告/エラーメッセージを画面上に残し、ブロック全体を消去することはありません。何が起こったかの記録を残したいときに役立ちます。
  • 新しいsubLabel引数は、$log->subLabel(‘…’)で更新可能で、メインラベルの下に暗い線を描き、空文字列に設定されるとクリアされます。「アセットを構築中…」のような一時的な状態や、安定した概要を汚染せずに実行するコマンドに有効です。
  • ラベルや安定メッセージは終端幅に切り詰められ、長い文字列が再描画を壊さないようにしています。
task(
    label: 'Deploying',
    callback: function ($log) {
        $log->subLabel('Building assets…');
        runBuild($log);
        $log->success('Assets built');

        $log->subLabel('Running migrations…');
        runMigrations($log);
        $log->success('Migrations complete');

        $log->subLabel('');
    },
    keepSummary: true,
);

関数定義を見てみます。

▼「vendor/laravel/prompts/src/helpers.php」

function task(string $label, Closure $callback, ?int $limit = null, bool $keepSummary = false, ?string $subLabel = null): mixed
{
    return (new Task($label, $limit ?? 10, $keepSummary, $subLabel))->run($callback);
}

▼コード例

<?php

require __DIR__ . '/../vendor/autoload.php';

use function Laravel\Prompts\task;

task(
    label: 'Deploying',
    callback: function ($log) {
        $log->subLabel('Building assets…');
        sleep(1);
        $log->success('Assets built');

        $log->subLabel('Running migrations…');
        sleep(1);
        $log->success('Migrations complete');

        $log->subLabel('');
    },
    keepSummary: true,
);

▼実行結果

あ、終了したら消えちゃうのね。。

まとめ

Joeさん、頑張ってますね。

datatable() はキーボードナビゲーションを頑張った感があります。

現在は単一選択ですが、いずれmultiselect()同様に複数選択が出てくるかもしれません。

task() の keepSummary は結局最後は消えてしまいますね。。

  • 0
  • 0
  • 0
  • 0

コメント

Copied title and URL