From 68d5109e214fce493f593ba6f323ec331124f2e6 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 19 Apr 2023 15:30:28 +1000 Subject: [PATCH] Improving spacing between components --- src/Illuminate/Console/Command.php | 4 +- ...mptFallbacks.php => ConfiguresPrompts.php} | 6 +- src/Illuminate/Console/ConfirmableTrait.php | 6 -- .../Console/Contracts/NewLineAware.php | 9 +++ src/Illuminate/Console/OutputStyle.php | 61 ++++++++++++++++++- .../Console/View/Components/Line.php | 2 +- 6 files changed, 76 insertions(+), 12 deletions(-) rename src/Illuminate/Console/Concerns/{ConfiguresPromptFallbacks.php => ConfiguresPrompts.php} (96%) diff --git a/src/Illuminate/Console/Command.php b/src/Illuminate/Console/Command.php index 497ee434b48a..95f78389dad0 100755 --- a/src/Illuminate/Console/Command.php +++ b/src/Illuminate/Console/Command.php @@ -13,7 +13,7 @@ class Command extends SymfonyCommand { use Concerns\CallsCommands, - Concerns\ConfiguresPromptFallbacks, + Concerns\ConfiguresPrompts, Concerns\HasParameters, Concerns\InteractsWithIO, Concerns\InteractsWithSignals, @@ -174,7 +174,7 @@ public function run(InputInterface $input, OutputInterface $output): int $this->components = $this->laravel->make(Factory::class, ['output' => $this->output]); - $this->configurePromptFallbacks($input); + $this->configurePrompts($input); try { return parent::run( diff --git a/src/Illuminate/Console/Concerns/ConfiguresPromptFallbacks.php b/src/Illuminate/Console/Concerns/ConfiguresPrompts.php similarity index 96% rename from src/Illuminate/Console/Concerns/ConfiguresPromptFallbacks.php rename to src/Illuminate/Console/Concerns/ConfiguresPrompts.php index bcb9c41a9ed7..86cb5cc94204 100644 --- a/src/Illuminate/Console/Concerns/ConfiguresPromptFallbacks.php +++ b/src/Illuminate/Console/Concerns/ConfiguresPrompts.php @@ -11,7 +11,7 @@ use Laravel\Prompts\TextPrompt; use Symfony\Component\Console\Input\InputInterface; -trait ConfiguresPromptFallbacks +trait ConfiguresPrompts { /** * Configure the prompt fallbacks. @@ -19,8 +19,10 @@ trait ConfiguresPromptFallbacks * @param \Symfony\Component\Console\Input\InputInterface $input * @return void */ - protected function configurePromptFallbacks(InputInterface $input) + protected function configurePrompts(InputInterface $input) { + Prompt::setOutput($this->output); + Prompt::fallbackWhen(! $input->isInteractive() || windows_os() || app()->runningUnitTests()); TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid( diff --git a/src/Illuminate/Console/ConfirmableTrait.php b/src/Illuminate/Console/ConfirmableTrait.php index 51143381ecc9..813492720645 100644 --- a/src/Illuminate/Console/ConfirmableTrait.php +++ b/src/Illuminate/Console/ConfirmableTrait.php @@ -2,8 +2,6 @@ namespace Illuminate\Console; -use Laravel\Prompts\ConfirmPrompt; - use function Laravel\Prompts\confirm; trait ConfirmableTrait @@ -33,13 +31,9 @@ public function confirmToProceed($warning = 'Application In Production', $callba $confirmed = confirm('Are you sure you want to run this command?', default: false); if (! $confirmed) { - $this->newLine(); - $this->components->warn('Command cancelled.'); return false; - } elseif (! ConfirmPrompt::shouldFallback()) { - $this->newLine(); } } diff --git a/src/Illuminate/Console/Contracts/NewLineAware.php b/src/Illuminate/Console/Contracts/NewLineAware.php index 135cecba8062..86edc0bd0d41 100644 --- a/src/Illuminate/Console/Contracts/NewLineAware.php +++ b/src/Illuminate/Console/Contracts/NewLineAware.php @@ -8,6 +8,15 @@ interface NewLineAware * Whether a newline has already been written. * * @return bool + * + * @deprecated use newLinesWritten */ public function newLineWritten(); + + /** + * How many trailing newlines were written. + * + * @return int + */ + public function newLinesWritten(); } diff --git a/src/Illuminate/Console/OutputStyle.php b/src/Illuminate/Console/OutputStyle.php index 2c159ca565ee..bb9db1afcf10 100644 --- a/src/Illuminate/Console/OutputStyle.php +++ b/src/Illuminate/Console/OutputStyle.php @@ -5,6 +5,7 @@ use Illuminate\Console\Contracts\NewLineAware; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; class OutputStyle extends SymfonyStyle implements NewLineAware @@ -16,10 +17,21 @@ class OutputStyle extends SymfonyStyle implements NewLineAware */ private $output; + /** + * The number of trailing new lines written by the last output. + * + * This is initialized as 1 to account for the new line written by the shell after executing a command. + * + * @var int + */ + protected $newLinesWritten = 1; + /** * If the last output written wrote a new line. * * @var bool + * + * @deprecated use $newLinesWritten */ protected $newLineWritten = false; @@ -42,9 +54,22 @@ public function __construct(InputInterface $input, OutputInterface $output) * * @return void */ + public function askQuestion(Question $question): mixed + { + try { + return parent::askQuestion($question); + } finally { + $this->newLinesWritten++; + } + } + + /** + * {@inheritdoc} + */ public function write(string|iterable $messages, bool $newline = false, int $options = 0) { - $this->newLineWritten = $newline; + $this->newLinesWritten = $this->trailingNewLineCount($messages) + (int) $newline; + $this->newLineWritten = $this->newLinesWritten > 0; parent::write($messages, $newline, $options); } @@ -56,6 +81,7 @@ public function write(string|iterable $messages, bool $newline = false, int $opt */ public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL) { + $this->newLinesWritten = $this->trailingNewLineCount($messages) + 1; $this->newLineWritten = true; parent::writeln($messages, $type); @@ -68,11 +94,24 @@ public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORM */ public function newLine(int $count = 1) { + $this->newLinesWritten += $count; $this->newLineWritten = $count > 0; parent::newLine($count); } + /** + * {@inheritdoc} + */ + public function newLinesWritten() + { + if ($this->output instanceof static) { + return $this->output->newLinesWritten(); + } + + return $this->newLinesWritten; + } + /** * {@inheritdoc} */ @@ -85,6 +124,26 @@ public function newLineWritten() return $this->newLineWritten; } + /* + * Count the number of trailing new lines in a string. + * + * @param string|iterable $messages + * @return int + */ + private function trailingNewLineCount($messages) + { + if (is_iterable($messages)) { + $string = ''; + foreach ($messages as $message) { + $string .= $message.PHP_EOL; + } + } else { + $string = $messages; + } + + return strlen($string) - strlen(rtrim($string, PHP_EOL)); + } + /** * Returns whether verbosity is quiet (-q). * diff --git a/src/Illuminate/Console/View/Components/Line.php b/src/Illuminate/Console/View/Components/Line.php index 5c701ee5aa51..0153d1f67580 100644 --- a/src/Illuminate/Console/View/Components/Line.php +++ b/src/Illuminate/Console/View/Components/Line.php @@ -47,7 +47,7 @@ public function render($style, $string, $verbosity = OutputInterface::VERBOSITY_ ]); $this->renderView('line', array_merge(static::$styles[$style], [ - 'marginTop' => ($this->output instanceof NewLineAware && $this->output->newLineWritten()) ? 0 : 1, + 'marginTop' => $this->output instanceof NewLineAware ? max(0, 2 - $this->output->newLinesWritten()) : 1, 'content' => $string, ]), $verbosity); }