diff --git a/CHANGELOG.md b/CHANGELOG.md index 6612345..bf033df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +1.3.0 +===== + +* (feature) Also provide pre-formatted `createTable()`. +* (feature) Add styled `info()`. +* (feature) Add `headline()`. + + 1.2.4 ===== diff --git a/src/Console/Style/TorrStyle.php b/src/Console/Style/TorrStyle.php index 553550d..cc2dbc1 100644 --- a/src/Console/Style/TorrStyle.php +++ b/src/Console/Style/TorrStyle.php @@ -9,6 +9,7 @@ use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Helper\TableStyle; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Console\Terminal; /** * 21TORR-branded CLI style @@ -18,8 +19,8 @@ class TorrStyle extends SymfonyStyle private const HIGHLIGHT = "red"; /** - * @inheritDoc */ + #[\Override] public function title (string $message) : void { $length = Helper::width(Helper::removeDecoration($this->getFormatter(), $message)) + 4; @@ -32,8 +33,26 @@ public function title (string $message) : void } /** - * @inheritDoc + * + */ + public function headline (string $message) : void + { + $length = Helper::width(Helper::removeDecoration($this->getFormatter(), $message)); + + $this->writeln([ + "", + \sprintf( + "──── %s %s", + $message, + "" . str_repeat("─", $this->getLineLength() - $length - 6) . "", + ), + "", + ]); + } + + /** */ + #[\Override] public function section (string $message) : void { $length = Helper::width(Helper::removeDecoration($this->getFormatter(), $message)); @@ -47,33 +66,40 @@ public function section (string $message) : void } /** - * @inheritDoc - * * @param string[] $headers * @param list|TableSeparator> $rows */ + #[\Override] public function table (array $headers, array $rows) : void { + $this->createTable() + ->setHeaders($headers) + ->setRows($rows) + ->render(); + $this->newLine(); + } + + /** + * + */ + #[\Override] + public function createTable () : Table + { + $table = parent::createTable(); + $style = (new TableStyle()) ->setHorizontalBorderChars('─') ->setVerticalBorderChars('│') ->setCrossingChars('┼', '╭', '┬', '╮', '┤', '╯', '┴', '╰', '├'); $style->setCellHeaderFormat('%s'); - $table = new Table($this); - $table->setHeaders($headers); - $table->setRows($rows); - $table->setStyle($style); - - $table->render(); - $this->newLine(); + return $table->setStyle($style); } /** - * @inheritDoc - * * @param string[] $elements */ + #[\Override] public function listing (array $elements) : void { $this->newLine(); @@ -87,8 +113,8 @@ public function listing (array $elements) : void } /** - * @inheritDoc */ + #[\Override] public function createProgressBar ( int $max = 0, string $format = " %current%/%max% [%bar%] %percent:3s%% %elapsed:6s% %message%", @@ -100,6 +126,21 @@ public function createProgressBar ( return $progressBar; } + /** + * + */ + #[\Override] + public function info (array|string $message) : void + { + $this->block( + $message, + "INFO", + 'fg=white;bg=blue', + ' ', + true, + ); + } + /** * A smaller way to mark something as done */ @@ -110,4 +151,16 @@ public function done (string $message) : void $message, )); } + + /** + * Calculates the line length (= width) of the CLI + */ + private function getLineLength ( + int $maxLineLength = 250, + ) : int + { + $width = new Terminal()->getWidth() ?: $maxLineLength; + + return min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), $maxLineLength); + } }