Skip to content

Commit 2e8dd85

Browse files
committed
Console: Fix issue where $msg2 is not indented when prefix width is 2
1 parent 839ff42 commit 2e8dd85

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

src/Toolkit/Console/Console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ private function _write(
900900
$formatter = $target->getFormatter();
901901

902902
$indent = mb_strlen($formatter->getMessagePrefix($level, $type));
903-
$indent = max(0, strpos($msg1, "\n") !== false ? $indent : $indent - 4);
903+
$indent = $indent < 4 || strpos($msg1, "\n") !== false ? $indent : $indent - 4;
904904

905905
$_msg1 = $msg1 === '' ? '' : $formatter->format($msg1);
906906

tests/unit/Toolkit/Console/ConsoleTest.php

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public function testSetPrefix(): void
8181

8282
$this->assertSameTargetMessages([
8383
[5, '➤ Foo: bar'],
84-
[6, "- Foo:\n bar"],
84+
[6, "- Foo:\n bar"],
8585
[6, 'foo bar'],
8686
], $this->TtyTarget);
8787

8888
$this->assertSameTargetOutput(
8989
"\e[2mDRY RUN \e[22m\e[1m\e[36m➤ \e[39m\e[22m\e[1mFoo:\e[22m\e[36m bar\e[39m\n"
9090
. "\e[2mDRY RUN \e[22m\e[33m- \e[39mFoo:\e[33m\n"
91-
. "\e[2mDRY RUN \e[22m bar\e[39m\n"
91+
. "\e[2mDRY RUN \e[22m bar\e[39m\n"
9292
. "\e[2mDRY RUN \e[22mfoo bar\n",
9393
["\n" => '"\n"' . \PHP_EOL],
9494
);
@@ -279,6 +279,110 @@ public function testOutput(): void
279279
$this->assertSame(7, $console->warnings());
280280
}
281281

282+
public function testOutputWithNewlines(): void
283+
{
284+
$target3 = new MockTarget(null, false, false, false, null, new Formatter(null, null, null, [5 => '=> ', 6 => '-> '], [4 => '>> ']));
285+
$target4 = new MockTarget(null, false, false, false, null, new Formatter(null, null, null, [5 => '==> ', 6 => ' -> '], [4 => '>>> ']));
286+
$console = $this->Console;
287+
$console->registerTarget($target3);
288+
$console->registerTarget($target4);
289+
$console->info('foo:', "\nbar");
290+
$console->info('foo:', "bar\nbaz");
291+
$console->info("foo\nbar:", 'baz');
292+
$console->info("foo\nbar:", "\nbaz");
293+
$console->info("foo\nbar:", "baz\nqux");
294+
$console->log('foo:', "\nbar");
295+
$console->log('foo:', "bar\nbaz");
296+
$console->log("foo\nbar:", 'baz');
297+
$console->log("foo\nbar:", "\nbaz");
298+
$console->log("foo\nbar:", "baz\nqux");
299+
$console->group('foo');
300+
$console->group('bar');
301+
$console->info('foo:', "\nbar");
302+
$console->info('foo:', "bar\nbaz");
303+
$console->info("foo\nbar:", 'baz');
304+
$console->info("foo\nbar:", "\nbaz");
305+
$console->info("foo\nbar:", "baz\nqux");
306+
$console->log('foo:', "\nbar");
307+
$console->log('foo:', "bar\nbaz");
308+
$console->log("foo\nbar:", 'baz');
309+
$console->log("foo\nbar:", "\nbaz");
310+
$console->log("foo\nbar:", "baz\nqux");
311+
312+
$this->assertSameTargetMessages([
313+
[5, "➤ foo:\n bar"],
314+
[5, "➤ foo:\n bar\n baz"],
315+
[5, "➤ foo\n bar: baz"],
316+
[5, "➤ foo\n bar:\n baz"],
317+
[5, "➤ foo\n bar:\n baz\n qux"],
318+
[6, "- foo:\n bar"],
319+
[6, "- foo:\n bar\n baz"],
320+
[6, "- foo\n bar: baz"],
321+
[6, "- foo\n bar:\n baz"],
322+
[6, "- foo\n bar:\n baz\n qux"],
323+
[5, '» foo'],
324+
[5, ' » bar'],
325+
[5, " ➤ foo:\n bar"],
326+
[5, " ➤ foo:\n bar\n baz"],
327+
[5, " ➤ foo\n bar: baz"],
328+
[5, " ➤ foo\n bar:\n baz"],
329+
[5, " ➤ foo\n bar:\n baz\n qux"],
330+
[6, " - foo:\n bar"],
331+
[6, " - foo:\n bar\n baz"],
332+
[6, " - foo\n bar: baz"],
333+
[6, " - foo\n bar:\n baz"],
334+
[6, " - foo\n bar:\n baz\n qux"],
335+
], $this->TtyTarget);
336+
$this->assertSameTargetMessages([
337+
[5, "=> foo:\n bar"],
338+
[5, "=> foo:\n bar\n baz"],
339+
[5, "=> foo\n bar: baz"],
340+
[5, "=> foo\n bar:\n baz"],
341+
[5, "=> foo\n bar:\n baz\n qux"],
342+
[6, "-> foo:\n bar"],
343+
[6, "-> foo:\n bar\n baz"],
344+
[6, "-> foo\n bar: baz"],
345+
[6, "-> foo\n bar:\n baz"],
346+
[6, "-> foo\n bar:\n baz\n qux"],
347+
[5, '>> foo'],
348+
[5, ' >> bar'],
349+
[5, " => foo:\n bar"],
350+
[5, " => foo:\n bar\n baz"],
351+
[5, " => foo\n bar: baz"],
352+
[5, " => foo\n bar:\n baz"],
353+
[5, " => foo\n bar:\n baz\n qux"],
354+
[6, " -> foo:\n bar"],
355+
[6, " -> foo:\n bar\n baz"],
356+
[6, " -> foo\n bar: baz"],
357+
[6, " -> foo\n bar:\n baz"],
358+
[6, " -> foo\n bar:\n baz\n qux"],
359+
], $target3);
360+
$this->assertSameTargetMessages([
361+
[5, "==> foo:\n bar"],
362+
[5, "==> foo:\n bar\n baz"],
363+
[5, "==> foo\n bar: baz"],
364+
[5, "==> foo\n bar:\n baz"],
365+
[5, "==> foo\n bar:\n baz\n qux"],
366+
[6, " -> foo:\n bar"],
367+
[6, " -> foo:\n bar\n baz"],
368+
[6, " -> foo\n bar: baz"],
369+
[6, " -> foo\n bar:\n baz"],
370+
[6, " -> foo\n bar:\n baz\n qux"],
371+
[5, '>>> foo'],
372+
[5, ' >>> bar'],
373+
[5, " ==> foo:\n bar"],
374+
[5, " ==> foo:\n bar\n baz"],
375+
[5, " ==> foo\n bar: baz"],
376+
[5, " ==> foo\n bar:\n baz"],
377+
[5, " ==> foo\n bar:\n baz\n qux"],
378+
[6, " -> foo:\n bar"],
379+
[6, " -> foo:\n bar\n baz"],
380+
[6, " -> foo\n bar: baz"],
381+
[6, " -> foo\n bar:\n baz"],
382+
[6, " -> foo\n bar:\n baz\n qux"],
383+
], $target4);
384+
}
385+
282386
public function testLogProgress(): void
283387
{
284388
$console = $this->Console;

0 commit comments

Comments
 (0)