From 7f6e8b4d48d3d64fda5c77e3cecb17849514b8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederic=20G=2E=20=C3=98stby?= Date: Sun, 27 Oct 2024 18:37:20 +0100 Subject: [PATCH] Made it easier to customize the spinner and table rendering --- src/mako/cli/output/helpers/Spinner.php | 23 +-- src/mako/cli/output/helpers/Table.php | 71 ++------ .../output/helpers/spinner/AsciiFrames.php | 24 +++ .../cli/output/helpers/spinner/Frames.php | 40 +++++ .../cli/output/helpers/table/AsciiBorder.php | 69 ++++++++ src/mako/cli/output/helpers/table/Border.php | 157 ++++++++++++++++++ .../reactor/traits/CommandHelperTrait.php | 8 +- tests/unit/cli/output/helpers/TableTest.php | 23 +++ 8 files changed, 337 insertions(+), 78 deletions(-) create mode 100644 src/mako/cli/output/helpers/spinner/AsciiFrames.php create mode 100644 src/mako/cli/output/helpers/spinner/Frames.php create mode 100644 src/mako/cli/output/helpers/table/AsciiBorder.php create mode 100644 src/mako/cli/output/helpers/table/Border.php diff --git a/src/mako/cli/output/helpers/Spinner.php b/src/mako/cli/output/helpers/Spinner.php index ad576d7b1..be51634a1 100644 --- a/src/mako/cli/output/helpers/Spinner.php +++ b/src/mako/cli/output/helpers/Spinner.php @@ -7,6 +7,7 @@ namespace mako\cli\output\helpers; +use mako\cli\output\helpers\spinner\Frames; use mako\cli\output\Output; use function count; @@ -23,16 +24,6 @@ */ class Spinner { - /** - * Spinner frames. - */ - public const array FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; - - /** - * Time between redraw in microseconds. - */ - protected const int TIME_BETWEEN_REDRAW = 100000; - /** * Can we fork the process? */ @@ -43,7 +34,7 @@ class Spinner */ public function __construct( protected Output $output, - protected array $frames = Spinner::FRAMES, + protected Frames $frames = new Frames, ) { $this->canFork = $this->canFork(); } @@ -63,10 +54,14 @@ protected function spinner(string $message, string $template): void { $i = 0; - $frames = count($this->frames); + $frames = $this->frames->getFrames(); + + $frameCount = count($frames); + + $timeBetweenRedraw = $this->frames->getTimeBetweenRedraw(); while (true) { - $this->output->write("\r" . sprintf($template, $this->frames[$i++ % $frames]) . " {$message}"); + $this->output->write("\r" . sprintf($template, $frames[$i++ % $frameCount]) . " {$message}"); if (posix_kill(posix_getpid(), 0) === false) { break; @@ -76,7 +71,7 @@ protected function spinner(string $message, string $template): void posix_kill(posix_getpid(), SIGKILL); } - usleep(static::TIME_BETWEEN_REDRAW); + usleep($timeBetweenRedraw); } } diff --git a/src/mako/cli/output/helpers/Table.php b/src/mako/cli/output/helpers/Table.php index 486fe70a3..e92bb270a 100644 --- a/src/mako/cli/output/helpers/Table.php +++ b/src/mako/cli/output/helpers/Table.php @@ -9,6 +9,7 @@ use mako\cli\exceptions\CliException; use mako\cli\output\formatter\FormatterInterface; +use mako\cli\output\helpers\table\Border; use mako\cli\output\helpers\traits\HelperTrait; use mako\cli\output\Output; @@ -24,61 +25,6 @@ class Table { use HelperTrait; - /** - * Horizontal line. - */ - protected const string HORIZONTAL_LINE = '━'; - - /** - * Vertical line. - */ - protected const string VERTICAL_LINE = '┃'; - - /** - * Top left corner. - */ - protected const string CORNER_TOP_LEFT = '┏'; - - /** - * Top right corner. - */ - protected const string CORNER_TOP_RIGHT = '┓'; - - /** - * Down t-junction. - */ - protected const string T_JUNCTION_DOWN = '┳'; - - /** - * Up t-junction. - */ - protected const string T_JUNCTION_UP = '┻'; - - /** - * Left t-junction. - */ - protected const string T_JUNCTION_LEFT = '┣'; - - /** - * Right t-junction. - */ - protected const string T_JUNCTION_RIGHT = '┫'; - - /** - * Junction. - */ - protected const string JUNCTION = '╋'; - - /** - * Bottom left corner. - */ - protected const string CORNER_BOTTOM_LEFT = '┗'; - - /** - * Bottom right corner. - */ - protected const string CORNER_BOTTOM_RIGHT = '┛'; - /** * Formatter. */ @@ -88,7 +34,8 @@ class Table * Constructor. */ public function __construct( - protected Output $output + protected Output $output, + protected Border $borderStyle = new Border ) { $this->formatter = $output->getFormatter(); } @@ -151,7 +98,7 @@ protected function buildRowSeparator(array $columnWidths, string $junction, stri $separator = $leftCorner; for ($i = 0; $i < $columns; $i++) { - $separator .= str_repeat(static::HORIZONTAL_LINE, $columnWidths[$i] + 2) . ($i < $columns - 1 ? $junction : ''); + $separator .= str_repeat($this->borderStyle->getHorizontalLine(), $columnWidths[$i] + 2) . ($i < $columns - 1 ? $junction : ''); } return $separator . $rightCorner . PHP_EOL; @@ -168,7 +115,9 @@ protected function buildTableRow(array $colums, array $columnWidths): string $cells[] = $value . str_repeat(' ', $columnWidths[$key] - $this->stringWidthWithoutFormatting($value)); } - return static::VERTICAL_LINE . ' ' . implode(' ' . static::VERTICAL_LINE . ' ', $cells) . ' ' . static::VERTICAL_LINE . PHP_EOL; + return $this->borderStyle->getVerticalLine() + . ' ' . implode(' ' . $this->borderStyle->getVerticalLine() . ' ', $cells) + . ' ' . $this->borderStyle->getVerticalLine() . PHP_EOL; } /** @@ -184,9 +133,9 @@ public function render(array $columnNames, array $rows): string // Build table header - $table = $this->buildRowSeparator($columnWidths, static::T_JUNCTION_DOWN, static::CORNER_TOP_LEFT, static::CORNER_TOP_RIGHT) + $table = $this->buildRowSeparator($columnWidths, $this->borderStyle->getTJunctionDown(), $this->borderStyle->getTopLeftCorner(), $this->borderStyle->getTopRightCorner()) . $this->buildTableRow($columnNames, $columnWidths) - . $this->buildRowSeparator($columnWidths, static::JUNCTION, static::T_JUNCTION_LEFT, static::T_JUNCTION_RIGHT); + . $this->buildRowSeparator($columnWidths, $this->borderStyle->getJunction(), $this->borderStyle->getTJunctionLeft(), $this->borderStyle->getTJunctionRight()); // Add table rows @@ -196,7 +145,7 @@ public function render(array $columnNames, array $rows): string // Add bottom border - $table .= $this->buildRowSeparator($columnWidths, static::T_JUNCTION_UP, static::CORNER_BOTTOM_LEFT, static::CORNER_BOTTOM_RIGHT); + $table .= $this->buildRowSeparator($columnWidths, $this->borderStyle->getTJunctionUp(), $this->borderStyle->getBottomLeftCorner(), $this->borderStyle->getBottomRightCorner()); // Return table diff --git a/src/mako/cli/output/helpers/spinner/AsciiFrames.php b/src/mako/cli/output/helpers/spinner/AsciiFrames.php new file mode 100644 index 000000000..22e87563a --- /dev/null +++ b/src/mako/cli/output/helpers/spinner/AsciiFrames.php @@ -0,0 +1,24 @@ +output, $frames))->spin($message, $callback, $template); } @@ -120,9 +122,9 @@ protected function spinner(string $message, callable $callback, string $template /** * Draws a table. */ - protected function table(array $columnNames, array $rows, int $writer = Output::STANDARD): void + protected function table(array $columnNames, array $rows, int $writer = Output::STANDARD, Border $borderStyle = new Border): void { - (new Table($this->output))->draw($columnNames, $rows, $writer); + (new Table($this->output, $borderStyle))->draw($columnNames, $rows, $writer); } /** diff --git a/tests/unit/cli/output/helpers/TableTest.php b/tests/unit/cli/output/helpers/TableTest.php index c15020da8..1c9e01053 100644 --- a/tests/unit/cli/output/helpers/TableTest.php +++ b/tests/unit/cli/output/helpers/TableTest.php @@ -10,6 +10,7 @@ use mako\cli\exceptions\CliException; use mako\cli\output\formatter\FormatterInterface; use mako\cli\output\helpers\Table; +use mako\cli\output\helpers\table\AsciiBorder; use mako\cli\output\Output; use mako\tests\TestCase; use Mockery; @@ -40,6 +41,28 @@ public function testBasicTable(): void $this->assertSame($expected, $table->render(['Col1'], [['Cell1']])); } + /** + * + */ + public function testBasicTableWithAsciiBorder(): void + { + /** @var \mako\cli\output\Output|\Mockery\MockInterface $output */ + $output = Mockery::mock(Output::class); + + $output->shouldReceive('getFormatter')->once()->andReturn(null); + + $table = new Table($output, new AsciiBorder); + + $expected = ''; + $expected .= '---------' . PHP_EOL; + $expected .= '| Col1 |' . PHP_EOL; + $expected .= '---------' . PHP_EOL; + $expected .= '| Cell1 |' . PHP_EOL; + $expected .= '---------' . PHP_EOL; + + $this->assertSame($expected, $table->render(['Col1'], [['Cell1']])); + } + /** * */