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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.4.0
=====

* (feature) Improve rendering of admonitions.
* (improvement) Improve styling of `comment()` and `done()`.


1.3.0
=====

Expand Down
129 changes: 116 additions & 13 deletions src/Console/Style/TorrStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,132 @@ public function createProgressBar (
}

/**
*
* @param string[]|string $message
*/
#[\Override]
public function info (array|string $message) : void
{
$this->block(
$message,
"INFO",
'fg=white;bg=blue',
' ',
true,
);
$this->renderAdmonition($message, "💡", "black", "blue");
}

/**
* @param string[]|string $message
*/
#[\Override]
public function warning (array|string $message) : void
{
$this->renderAdmonition($message, "🚨", "black", "#FFD700");
}

/**
* @param string[]|string $message
*/
#[\Override]
public function caution (array|string $message) : void
{
$this->renderAdmonition($message, "🚧", "black", "#FF8700");
}

/**
* @param string[]|string $message
*/
#[\Override]
public function error (array|string $message) : void
{
$this->renderAdmonition($message, "🔴", "black", "red");
}

/**
* @param string[]|string $message
*/
#[\Override]
public function note (array|string $message) : void
{
$this->renderAdmonition($message, "📝", "black", "#CCCCCC");
}

/**
* Renders a styled admonition box with rounded corners and solid background fill.
*
* Each line is wrapped in the style tag so the background color covers the full
* content width, producing a solid colored band with rounded corners:
*
* ╭──────────────────────────────────────────────╮
* │ LABEL Message text padded to fill the line │
* ╰──────────────────────────────────────────────╯
*
* Line anatomy (total = lineLength):
* border: ╭ + {innerWidth + 2 dashes} + ╮ = innerWidth + 3
* content: │ + space + {innerWidth chars} + space + │ = innerWidth + 4
* Both are preceded by one leading space → innerWidth = lineLength - 5
*/
private function renderAdmonition (
array|string $message,
string $label,
string $foreground,
string $background,
) : void
{
$messages = array_values(array_filter((array) $message));
$lineLength = $this->getLineLength();

// Inner content area width that makes each line exactly lineLength chars.
// {paddingH} + {innerWidth} + {paddingH} = innerWidth + paddingH * 2
$innerWidth = $lineLength - 6;

// "LABEL " prefix on the first line; subsequent lines indented to match
$labelPrefix = $label . ' ';
$labelPrefixLen = mb_strwidth($labelPrefix);
$textWidth = max(1, $innerWidth - $labelPrefixLen);
$indent = str_repeat(' ', $labelPrefixLen);

$contentLines = [];

foreach ($messages as $i => $msg)
{
foreach (explode("\n", wordwrap($msg, $textWidth, "\n", true)) as $j => $line)
{
$contentLines[] = (0 === $i && 0 === $j ? $labelPrefix : $indent) . $line;
}
}

// Each line is fully wrapped in the style tag so the background color fills
// every character cell — spaces become the solid fill.
// Width: {paddingH} + {innerWidth} + {paddingH} = innerWidth + paddingH * 2 = lineLength
$paddingH = 2;
$tag = "fg={$foreground};bg={$background}";
$blank = \sprintf('<%s>%s</>', $tag, str_repeat(' ', $innerWidth + $paddingH * 2));

$this->newLine();
$this->writeln($blank);

foreach ($contentLines as $line)
{
$padding = str_repeat(' ', max(0, $innerWidth - mb_strwidth($line)));
$horizontalPad = str_repeat(' ', $paddingH);
$this->writeln(\sprintf('<%s>%s%s%s%s</>', $tag, $horizontalPad, $line, $padding, $horizontalPad));
}

$this->writeln($blank);
$this->newLine();
}

/**
*/
#[\Override]
public function comment (array|string $message) : void
{
$this->block($message, null, null, ' <fg=cyan>//</> ', false, false);
}

/**
* A smaller way to mark something as done
*
* @param string[]|string $message
*/
public function done (string $message) : void
public function done (array|string $message = "done") : void
{
$this->write(\sprintf(
"<fg=green>✓</> %s",
$message,
));
$this->block($message, null, null, ' <fg=green>✓</> ', false, false);
}

/**
Expand Down
Loading