joetannenbaum / terminalia
为Laravel提供美观的命令行输出。
Requires
- illuminate/collections: ^10.10
- illuminate/console: ^10.10
- illuminate/contracts: ^10.10
- illuminate/support: ^10.10
- spatie/fork: ^1.1
Requires (Dev)
- mockery/mockery: ^1.5
- pestphp/pest: ^2.6
- spatie/ray: ^1.37
- tightenco/duster: ^2.0
This package is auto-updated.
Last update: 2024-09-18 15:20:53 UTC
README
重要
嘿!这是一个有趣的项目,我学到了很多东西。不过它并不是最稳定的。我建议使用Laravel Prompts。它做得更好,经过良好的测试,并且很稳定。
Clack的用户体验,Laravel的DX,用于您的Artisan命令。
功能
- 使用Laravel内置验证器进行内联输入验证
- 文本、选择和确认的交互式提示
- 用于长时间运行进程的旋转器
演示
安装
composer require joetannenbaum/terminalia
此包实现了一个Console mixin,当安装此包时,应自动注册。
如果服务提供程序没有自动注册(即如果您正在使用Laravel Zero),请将以下内容添加到您的config/app.php
文件中
'providers' => [ // ... Terminalia\Providers\TerminaliaServiceProvider::class, ],
获取输入
输入验证
这些方法的rules
参数使用Laravel的内置验证器,因此它接受您可以向Validator::make
传递的任何内容。
注意:如果您在Laravel Zero应用程序中使用验证,请记住在您的config/app.php
文件中注册ValidationServiceProvider::class
和TranslationServiceProvider::class
,并在项目根目录中包含一个lang
目录。
termAsk
termAsk
方法提示用户输入并返回响应。它接受以下参数
question
(string): 要向用户提出的问题rules
(string|array): 应用到响应的验证规则数组hidden
(bool): 是否隐藏用户的输入(对密码很有用)default
(string): 默认值
$answer = $this->termAsk( question: 'What is your favorite color?', rules: ['required'], ); $password = $this->termAsk( question: 'What is your password?', rules: ['required'], hidden: true, );
termChoice
termChoice
方法提示用户从一系列选项中选择一个或多个项目。它接受以下参数
question
(string): 要向用户提出的问题choices
(array|Collection
|Helpers\Choices
): 显示给用户的选项数组multiple
(bool): 是否允许用户选择多个选项rules
(string|array): 应用到响应的验证规则数组filterable
(bool): 是否允许用户筛选选项minFilterLength
(int, 默认为5
): 在启用筛选之前列表中所需的最小选项数default
(string|array): 默认值(s)
如果multiple
为true
并且您将Collection
作为choices
参数传递,则返回的choices
也将作为Collection
返回,否则返回一个数组。
$answer = $this->termChoice( question: 'What is your favorite color?', choices: ['red', 'green', 'blue'], rules: ['required'], ); $favoriteThings = $this->termChoice( question: 'Which are your favorite things:', choices: [ 'raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens', 'brown paper packages tied up with strings', 'cream colored ponies', 'crisp apple strudels', 'doorbells', 'sleigh bells', 'schnitzel with noodles', ], multiple: true, rules: ['required'], filterable: true, );
除了简单地作为choices
参数传递一个简单数组外,您还可以选择使用Choices
辅助函数传递嵌套数组或集合。这允许您为列表中的每个项目指定一个标签和值。标签将显示给用户,而当用户选择项目时将返回值(s)。
use Terminalia\Helpers\Choices; $users = User::all(); // Choices will display the user's name and return a User model $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from($users, 'name'), ); // Choices will display the user's name and return the user ID $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from($users, 'name', 'id'), ); // Choices will be displayed with the user's full name, will return a User model $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from($users, fn($user) => "{$user->firstName} {$user->lastName}"), ); // Choices will be displayed with the user's full name, will return the user ID $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from( $users, fn($user) => "{$user->firstName} {$user->lastName}", fn($user) => $user->id, ), ); // Defaults will be determined by the display value when no return value is specified $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from($users, 'name'), default: 'Joe', ); // Defaults will be determined by the return value if it is specified $user = $this->termChoice( question: 'Which user would you like to edit?', choices: Choices::from($users, 'name', 'id'), default: 123, );
termConfirm
termConfirm
方法提示用户确认一个问题。它接受以下参数
question
(string): 要向用户提出的问题default
(bool): 问题的默认答案
$answer = $this->termConfirm( question: 'Are you sure you want to do this?', );
编写输出
termIntro
《termIntro》方法将介绍信息写入输出。它接受以下参数
text
(字符串):要写入输出的信息
$this->termIntro("Welcome! Let's get started.");
termOutro
《termOutro》方法将结束语信息写入输出。它接受以下参数
text
(字符串):要写入输出的信息
$this->termOutro('All set!');
termInfo
、termComment
、termError
、termWarning
与 Laravel 内置的输出方法保持一致,Terminalia 提供了用于以一致样式在不同颜色中写入输出的方法。它们接受以下参数
text
(字符串|数组):要写入输出的信息
$this->termInfo('Here is the URL: https://bellows.dev'); $this->termComment([ 'This is a multi-line comment! I have a lot to say, and it is easier to write as an array.', 'Here is the second part of what I have to say. Not to worry, Terminalia will handle all of the formatting.', ]); $this->termError('Whoops! That did not go so well.'); $this->termWarning('Heads up! Output may be *too* beautiful.');
termNote
《termNote》方法允许您向用户显示更长的信息。您可以将可选的标题作为第二个参数,如果您有多个行,可以可选地传入一个字符串数组作为第一个参数。
// Regular note $this->termNote( "You really did it. We are so proud of you. Thank you for telling us all about yourself. We can't wait to get to know you better.", 'Congratulations', ); // Multiple lines via an array $this->termNote( [ 'You really did it. We are so proud of you. Thank you for telling us all about yourself.', "We can't wait to get to know you better." ], 'Congratulations', ); // No title $this->termNote( [ 'You really did it. We are so proud of you. Thank you for telling us all about yourself.', "We can't wait to get to know you better." ], );
termSpinner
《termSpinner》方法允许您在不确定的过程运行时显示一个旋转器。它允许自定义,以便您可以在过程运行时通知用户正在发生的事情。旋转器的结果将是来自 task
参数的任何返回值。
重要:请注意,task
在一个派生的进程中运行,因此任务本身不应该在您的应用程序中创建任何副作用。它应该只处理一些事情并返回一个结果。
简单
$site = $this->termSpinner( title: 'Creating site...', task: function () { // Do something here that takes a little while $site = Site::create(); $site->deploy(); return $site; }, message: 'Site created!', );
根据任务的返回结果显示基于结果的变量最终消息
$site = $this->termSpinner( title: 'Creating site...', task: function () { // Do something here that takes a little while $site = Site::create(); $site->deploy(); return $site->wasDeployed; }, message: fn($result) => $result ? 'Site created!' : 'Error creating site.', );
在工作过程中更新用户进度
$site = $this->termSpinner( title: 'Creating site...', task: function (SpinnerMessenger $messenger) { // Do something here that takes a little while $site = Site::create(); $messenger->send('Site created, deploying'); $site->deploy(); $messenger->send('Verifying deployment'); $site->verifyDeployment(); return $site->wasDeployed; }, message: fn($result) => $result ? 'Site created!' : 'Error creating site.', );
在用户等待时发送鼓励的消息
$site = $this->termSpinner( title: 'Creating site...', task: function () { // Do something here that takes a little while $site = Site::create(); $site->deploy(); $site->verifyDeployment(); return $site->wasDeployed; }, // seconds => message longProcessMessages: [ 3 => 'One moment', 7 => 'Almost done', 11 => 'Wrapping up', ], );
进度条
进度条具有与Laravel 控制台进度条非常相似的 API,唯一的一个小的补充是:您可以为进度条传入一个可选的标题。
$this->withTermProgressBar(collect(range(1, 20)), function () { usleep(300_000); }, 'Progress is being made...');
$items = range(1, 10); $progress = $this->createTermProgressBar(count($items), 'Updating users...'); $progress->start(); foreach ($items as $item) { $progress->advance(); usleep(300_000); } $progress->finish();
$this->withTermProgressBar(collect(range(1, 20)), function () { usleep(300_000); });