Skip to content

Commit 3fcd16c

Browse files
authored
feat: add Output::wrap() (#10679)
1 parent 046c23d commit 3fcd16c

6 files changed

Lines changed: 75 additions & 34 deletions

File tree

‎system/CLI/CLI.php‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -520,32 +520,7 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
520520
*/
521521
public static function wrap(?string $string = null, int $max = 0, int $padLeft = 0): string
522522
{
523-
if ((string) $string === '') {
524-
return '';
525-
}
526-
527-
$width = static::getWidth();
528-
$max = ($max === 0 ? $width : min($max, $width)) - $padLeft;
529-
530-
$lines = wordwrap($string, $max, PHP_EOL);
531-
532-
if ($padLeft > 0) {
533-
$lines = explode(PHP_EOL, $lines);
534-
535-
$first = true;
536-
537-
array_walk($lines, static function (&$line) use ($padLeft, &$first): void {
538-
if (! $first) {
539-
$line = str_repeat(' ', $padLeft) . $line;
540-
} else {
541-
$first = false;
542-
}
543-
});
544-
545-
$lines = implode(PHP_EOL, $lines);
546-
}
547-
548-
return $lines;
523+
return static::getOutput()->wrap((string) $string, $max, $padLeft);
549524
}
550525

551526
// --------------------------------------------------------------------

‎system/CLI/Output.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ public function color(string $text, string $foreground, string $background = '',
185185
return $this->isDecorated() ? Color::apply($text, $foreground, $background, $format) : $text;
186186
}
187187

188+
/**
189+
* Returns the text wrapped at the terminal width, or at `$max` when narrower,
190+
* with every line after the first indented by `$padLeft` spaces.
191+
*/
192+
public function wrap(string $text, int $max = 0, int $padLeft = 0): string
193+
{
194+
$width = $this->terminal->getWidth();
195+
$max = ($max === 0 ? $width : min($max, $width)) - $padLeft;
196+
197+
return implode(PHP_EOL . str_repeat(' ', $padLeft), explode(PHP_EOL, wordwrap($text, $max, PHP_EOL)));
198+
}
199+
188200
/**
189201
* Whether the target stream supports colours.
190202
*/

‎system/CLI/Prompt.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,15 +600,12 @@ private function interrupt(): void
600600
private function writeKeysAndValues(array $options): void
601601
{
602602
$padLeft = max(array_map(mb_strwidth(...), array_keys($options))) + 6;
603-
$width = min(self::MAX_LINE_WIDTH, $this->output->getTerminal()->getWidth()) - $padLeft;
604603

605604
foreach ($options as $key => $description) {
606-
$lines = explode(PHP_EOL, wordwrap($description, $width, PHP_EOL));
607-
608605
$this->output->write(sprintf(
609606
'%s%s',
610607
$this->output->color(str_pad(sprintf(' [%s] ', $key), $padLeft), 'green'),
611-
implode(sprintf('%s%s', PHP_EOL, str_repeat(' ', $padLeft)), $lines),
608+
$this->output->wrap($description, self::MAX_LINE_WIDTH, $padLeft),
612609
));
613610
}
614611
}

‎tests/system/CLI/OutputTest.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,51 @@ public function testColorRejectsAnUnknownBackground(): void
227227
$output->color('test', 'white', 'Background');
228228
}
229229

230+
public function testWrapBreaksAtTheTerminalWidth(): void
231+
{
232+
$output = new Output(new RecordingInputOutput(), $this->createTerminalOfWidth(20));
233+
234+
$expected = <<<'EOT'
235+
the quick brown fox
236+
jumps over the lazy
237+
dog
238+
EOT;
239+
240+
$this->assertSame($expected, $output->wrap('the quick brown fox jumps over the lazy dog'));
241+
$this->assertSame($expected, $output->wrap('the quick brown fox jumps over the lazy dog', 999));
242+
}
243+
244+
public function testWrapBreaksAtANarrowerMax(): void
245+
{
246+
$output = new Output(new RecordingInputOutput(), $this->createTerminalOfWidth(20));
247+
248+
$this->assertSame(<<<'EOT'
249+
the quick
250+
brown fox
251+
jumps over
252+
the lazy
253+
dog
254+
EOT, $output->wrap('the quick brown fox jumps over the lazy dog', 10));
255+
}
256+
257+
public function testWrapIndentsTheLinesAfterTheFirst(): void
258+
{
259+
$output = new Output(new RecordingInputOutput(), $this->createTerminalOfWidth(20));
260+
261+
$this->assertSame(<<<'EOT'
262+
the quick brown
263+
fox jumps over
264+
the lazy dog
265+
EOT, $output->wrap('the quick brown fox jumps over the lazy dog', 0, 4));
266+
}
267+
268+
public function testWrapLeavesAnEmptyTextEmpty(): void
269+
{
270+
$output = new Output(new RecordingInputOutput(), $this->createTerminalOfWidth(20));
271+
272+
$this->assertSame('', $output->wrap('', 0, 4));
273+
}
274+
230275
public function testBeepRepeats(): void
231276
{
232277
$io = new RecordingInputOutput();
@@ -663,4 +708,12 @@ private function getWrittenText(RecordingInputOutput $io): string
663708
{
664709
return implode('', array_column($io->writes, 1));
665710
}
711+
712+
private function createTerminalOfWidth(int $width): Terminal
713+
{
714+
$terminal = $this->createStub(Terminal::class);
715+
$terminal->method('getWidth')->willReturn($width);
716+
717+
return $terminal;
718+
}
666719
}

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ Commands
271271
- Added ``key:rotate`` command to demote the current ``encryption.key`` to ``encryption.previousKeys`` in **.env** and generate a new key. See :ref:`spark-key-rotate`.
272272
- Added ``AbstractCommand::callSilently()`` to invoke another command with its output discarded, restoring the prior output afterwards. See :ref:`modern-commands-call-silently`.
273273
- The output side of the ``CLI`` class now lives in instance classes, with ``CLI`` delegating to them: :php:class:`Output <CodeIgniter\\CLI\\Output>`
274-
(``print()``, ``write()``, ``error()``, ``newLine()``, ``deferNewLine()``, ``beep()``, ``clearScreen()``, ``color()``), :php:class:`Terminal <CodeIgniter\\CLI\\Terminal>`
275-
(dimensions and colour support), :php:class:`ProgressBar <CodeIgniter\\CLI\\ProgressBar>`, :php:class:`Table <CodeIgniter\\CLI\\Table>`, and
276-
:php:class:`Color <CodeIgniter\\CLI\\Color>` (ANSI codes and ``Color::width()``). ``CLI::getOutput()`` and ``CLI::getTerminal()`` return the shared
277-
instances. See :ref:`cli-library-classes`.
274+
(``print()``, ``write()``, ``error()``, ``newLine()``, ``deferNewLine()``, ``beep()``, ``clearScreen()``, ``color()``, ``wrap()``),
275+
:php:class:`Terminal <CodeIgniter\\CLI\\Terminal>` (dimensions and colour support), :php:class:`ProgressBar <CodeIgniter\\CLI\\ProgressBar>`,
276+
:php:class:`Table <CodeIgniter\\CLI\\Table>`, and :php:class:`Color <CodeIgniter\\CLI\\Color>` (ANSI codes and ``Color::width()``).
277+
``CLI::getOutput()`` and ``CLI::getTerminal()`` return the shared instances. See :ref:`cli-library-classes`.
278278
- Modern commands can write through ``$this->getOutput()``, the same ``Output`` used by the static ``CLI`` methods. ``AbstractCommand::run()``
279279
and ``Commands::runCommand()`` accept an ``Output`` to run against, which ``Console`` passes down and sub-commands inherit.
280280
See :ref:`modern-commands-writing-output`.

‎user_guide_src/source/cli/cli_library.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ output of their run through ``$this->getPrompt()``:
372372
``print()``, ``write()``, and ``error()`` colour through this method, so a subclass overriding
373373
it changes the colouring of every write.
374374

375+
.. php:method:: wrap(string $text[, int $max = 0[, int $padLeft = 0]]): string
376+
377+
The same as ``CLI::wrap()``, measuring the terminal of this output.
378+
375379
.. php:method:: isDecorated(): bool
376380
377381
Whether the target stream supports colours.

0 commit comments

Comments
 (0)