From 3fde60a838912e71c82ed0f48048685dc32dbc77 Mon Sep 17 00:00:00 2001 From: Jitendra A <2908547+adhocore@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:30:12 +0700 Subject: [PATCH] fix: negative $times for str_repeat, tests --- src/Output/Writer.php | 8 ++++---- tests/Output/ProgressBarTest.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Output/Writer.php b/src/Output/Writer.php index 0ebbbba..1048aab 100644 --- a/src/Output/Writer.php +++ b/src/Output/Writer.php @@ -321,9 +321,8 @@ public function justify(string $first, ?string $second = null, array $options = 'sep' => $options['sep'] ?? '.', ]; - $second = (string) $second; - - $dashWidth = $this->terminal->width() - (strlen($first) + strlen($second)); + $second = (string) $second; + $dashWidth = $this->terminal->width() - (strlen($first) + strlen($second)); // remove left and right margins because we're going to add 1 space on each side (after/before the text). // if we don't have a second element, we just remove the left margin $dashWidth -= $second === '' ? 1 : 2; @@ -333,7 +332,8 @@ public function justify(string $first, ?string $second = null, array $options = $second = $this->colorizer->line($second, $options['second']); } - $this->write($first . ' ' . str_repeat((string) $options['sep'], $dashWidth) . ' ' . $second); + $sep = $dashWidth >= 0 ? str_repeat((string) $options['sep'], $dashWidth) : ''; + $this->write($first . ' ' . $sep . ' ' . $second); return $this->eol(); } diff --git a/tests/Output/ProgressBarTest.php b/tests/Output/ProgressBarTest.php index ee9170b..3a4b5d8 100644 --- a/tests/Output/ProgressBarTest.php +++ b/tests/Output/ProgressBarTest.php @@ -34,7 +34,7 @@ public function test_progress_bar() } } - $this->assertBufferContains('===========================================> 100%'); + $this->assertBufferContains('==========================================> 100%'); $this->assertBufferContains('10 x label'); $this->expectException(UnexpectedValueException::class); @@ -51,7 +51,7 @@ public function test_progress_bar_option() $progress->advance(1, $label); } - $this->assertBufferContains('###########################################~ 100%'); + $this->assertBufferContains('##########################################~ 100%'); $this->assertBufferContains('#1 label'); $this->expectException(UnexpectedValueException::class); @@ -73,7 +73,7 @@ public function getIterator(): Traversable } }, fn ($v, $k) => "$k: $v"); - $this->assertBufferContains('===========================================> 100%'); + $this->assertBufferContains('==========================================> 100%'); $this->assertBufferContains('c: 3'); $this->assertNotNull((new Terminal)->height());