Skip to content

Commit

Permalink
Improving spacing between components
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored and nunomaduro committed May 22, 2023
1 parent 4e4cdea commit 68d5109
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Console/Command.php
Expand Up @@ -13,7 +13,7 @@
class Command extends SymfonyCommand
{
use Concerns\CallsCommands,
Concerns\ConfiguresPromptFallbacks,
Concerns\ConfiguresPrompts,
Concerns\HasParameters,
Concerns\InteractsWithIO,
Concerns\InteractsWithSignals,
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -11,16 +11,18 @@
use Laravel\Prompts\TextPrompt;
use Symfony\Component\Console\Input\InputInterface;

trait ConfiguresPromptFallbacks
trait ConfiguresPrompts
{
/**
* Configure the prompt fallbacks.
*
* @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(
Expand Down
6 changes: 0 additions & 6 deletions src/Illuminate/Console/ConfirmableTrait.php
Expand Up @@ -2,8 +2,6 @@

namespace Illuminate\Console;

use Laravel\Prompts\ConfirmPrompt;

use function Laravel\Prompts\confirm;

trait ConfirmableTrait
Expand Down Expand Up @@ -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();
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Console/Contracts/NewLineAware.php
Expand Up @@ -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();
}
61 changes: 60 additions & 1 deletion src/Illuminate/Console/OutputStyle.php
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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}
*/
Expand All @@ -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).
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Console/View/Components/Line.php
Expand Up @@ -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);
}
Expand Down

0 comments on commit 68d5109

Please sign in to comment.