Skip to content

Commit

Permalink
Made it easier to customize the spinner and table rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Oct 27, 2024
1 parent d31b3ec commit 7f6e8b4
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 78 deletions.
23 changes: 9 additions & 14 deletions src/mako/cli/output/helpers/Spinner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mako\cli\output\helpers;

use mako\cli\output\helpers\spinner\Frames;
use mako\cli\output\Output;

use function count;
Expand All @@ -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?
*/
Expand All @@ -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();
}
Expand All @@ -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;
Expand All @@ -76,7 +71,7 @@ protected function spinner(string $message, string $template): void
posix_kill(posix_getpid(), SIGKILL);
}

usleep(static::TIME_BETWEEN_REDRAW);
usleep($timeBetweenRedraw);
}
}

Expand Down
71 changes: 10 additions & 61 deletions src/mako/cli/output/helpers/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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

Expand All @@ -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

Expand Down
24 changes: 24 additions & 0 deletions src/mako/cli/output/helpers/spinner/AsciiFrames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @copyright Frederic G. Østby
* @license http://www.makoframework.com/license
*/

namespace mako\cli\output\helpers\spinner;

/**
* Ascii frames.
*/
class AsciiFrames extends Frames
{
/**
* {@inheritDoc}
*/
protected const int TIME_BETWEEN_REDRAW = 200000;

/**
* {@inheritDoc}
*/
protected const array FRAMES = [')', '|', '('];
}
40 changes: 40 additions & 0 deletions src/mako/cli/output/helpers/spinner/Frames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Frederic G. Østby
* @license http://www.makoframework.com/license
*/

namespace mako\cli\output\helpers\spinner;

/**
* Frames.
*/
class Frames
{
/**
* Time between redraw in microseconds.
*/
protected const int TIME_BETWEEN_REDRAW = 100000;

/**
* Spinner frames.
*/
protected const array FRAMES = ['', '', '', '', '', '', '', '', '', ''];

/**
* Returns the spinner frames.
*/
public function getFrames(): array
{
return static::FRAMES;
}

/**
* Returns the time between redraw in microseconds.
*/
public function getTimeBetweenRedraw(): int
{
return static::TIME_BETWEEN_REDRAW;
}
}
69 changes: 69 additions & 0 deletions src/mako/cli/output/helpers/table/AsciiBorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Frederic G. Østby
* @license http://www.makoframework.com/license
*/

namespace mako\cli\output\helpers\table;

/**
* Ascii border.
*/
class AsciiBorder extends Border
{
/**
* {@inheritDoc}
*/
protected const string HORIZONTAL_LINE = '-';

/**
* {@inheritDoc}
*/
protected const string VERTICAL_LINE = '|';

/**
* {@inheritDoc}
*/
protected const string CORNER_TOP_LEFT = '-';

/**
* {@inheritDoc}
*/
protected const string CORNER_TOP_RIGHT = '-';

/**
* {@inheritDoc}
*/
protected const string T_JUNCTION_DOWN = '-';

/**
* {@inheritDoc}
*/
protected const string T_JUNCTION_UP = '-';

/**
* {@inheritDoc}
*/
protected const string T_JUNCTION_LEFT = '-';

/**
* {@inheritDoc}
*/
protected const string T_JUNCTION_RIGHT = '-';

/**
* {@inheritDoc}
*/
protected const string JUNCTION = '|';

/**
* {@inheritDoc}
*/
protected const string CORNER_BOTTOM_LEFT = '-';

/**
* {@inheritDoc}
*/
protected const string CORNER_BOTTOM_RIGHT = '-';
}
Loading

0 comments on commit 7f6e8b4

Please sign in to comment.