Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.3.0
=====

* (feature) Also provide pre-formatted `createTable()`.
* (feature) Add styled `info()`.
* (feature) Add `headline()`.


1.2.4
=====

Expand Down
81 changes: 67 additions & 14 deletions src/Console/Style/TorrStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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(
"<fg=red>────</> %s %s",
$message,
"<fg=red>" . str_repeat("─", $this->getLineLength() - $length - 6) . "</>",
),
"",
]);
}

/**
*/
#[\Override]
public function section (string $message) : void
{
$length = Helper::width(Helper::removeDecoration($this->getFormatter(), $message));
Expand All @@ -47,33 +66,40 @@ public function section (string $message) : void
}

/**
* @inheritDoc
*
* @param string[] $headers
* @param list<list<scalar|TableCell|null>|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('<info>%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();
Expand All @@ -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%",
Expand All @@ -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
*/
Expand All @@ -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);
}
}