Skip to content

Commit

Permalink
Refactor to handle start message dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 12, 2023
1 parent 3b8f093 commit 401c78b
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions packages/realtime-compiler/src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,32 @@ public function __construct(bool $verbose = false, ?SymfonyOutput $output = null
public function printStartMessage(string $host, int $port): void
{
$title = 'HydePHP Realtime Compiler';
$version = ' v'.Hyde::version();
$version = 'v'.Hyde::version();

$url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port);

$width = max(strlen("$title $version"), strlen("Listening on $url") + 1) + 1;
$spacing = str_repeat(' ', $width);
$lines = str_repeat('', $width);

$line1 = '&nbsp;'.sprintf('<span class="text-blue-500">%s</span>&nbsp;<span class="text-gray">%s</span>', $title, $version).str_repeat('&nbsp;', $width - strlen("$title $version"));
$line2 = '&nbsp;'.sprintf('<span class="text-white">Listening on </span>&nbsp;<a href="%s" class="text-yellow-500">%s</a>', $url, $url).str_repeat('&nbsp;', $width - strlen("Listening on $url") - 1);
render(<<<HTML
<div class="text-green-500">
<br>
&nbsp;╭{$lines}╮<br>
&nbsp;│{$spacing}│<br>
&nbsp;│{$line1}│<br>
&nbsp;│{$spacing}│<br>
&nbsp;│{$line2}│<br>
&nbsp;│{$spacing}│<br>
&nbsp;╰{$lines}╯<br>
</div>
HTML);
$lines = [
sprintf('<span class="text-blue-500">%s</span> <span class="text-gray">%s</span>', $title, $version),
'',
sprintf('<span class="text-white">Listening on</span> <a href="%s" class="text-yellow-500">%s</a>', $url, $url),
];

$lineLength = max(array_map('strlen', array_map('strip_tags', $lines)));

$lines = array_map(function (string $line) use ($lineLength): string {
return sprintf('&nbsp;│&nbsp;<span class="text-white">%s</span>%s│',
$line, str_repeat('&nbsp;', ($lineLength - strlen(strip_tags($line))) + 1)
);
}, array_merge([''], $lines, ['']));

$topLine = sprintf('&nbsp;╭%s╮', str_repeat('', $lineLength + 2));
$bottomLine = sprintf('&nbsp;╰%s╯', str_repeat('', $lineLength + 2));

$lines = array_merge([$topLine], $lines, [$bottomLine]);

$body = implode('<br>', array_merge([''], $lines, ['']));

render("<div class=\"text-green-500\">$body</div>");
}

public function getFormatter(): Closure
Expand Down

0 comments on commit 401c78b

Please sign in to comment.