Skip to content

Commit

Permalink
Merge pull request #1447 from hydephp/fancy-serve-command
Browse files Browse the repository at this point in the history
Refactor console output internals
  • Loading branch information
caendesilva authored Nov 11, 2023
2 parents 619655d + c4683db commit cd9add2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
16 changes: 13 additions & 3 deletions packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class ServeCommand extends Command
/** @var string */
protected $description = 'Start the realtime compiler server.';

protected ConsoleOutput $console;

public function handle(): int
{
$this->configureOutput();
$this->printStartMessage();

$this->runServerProcess(sprintf('php -S %s:%d %s',
Expand Down Expand Up @@ -67,18 +70,25 @@ protected function getEnvironmentVariables(): array
];
}

protected function configureOutput(): void
{
if (! $this->useBasicOutput()) {
$this->console = new ConsoleOutput($this->output->isVerbose());
}
}

protected function printStartMessage(): void
{
$this->useBasicOutput()
? $this->line('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop')
: ConsoleOutput::printStartMessage($this->getHostSelection(), $this->getPortSelection());
? $this->output->writeln('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop')
: $this->console->printStartMessage($this->getHostSelection(), $this->getPortSelection());
}

protected function getOutputHandler(): Closure
{
return $this->useBasicOutput() ? function (string $type, string $line): void {
$this->output->write($line);
} : ConsoleOutput::getFormatter($this->output->isVerbose());
} : $this->console->getFormatter();
}

protected function useBasicOutput(): bool
Expand Down
69 changes: 34 additions & 35 deletions packages/realtime-compiler/src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
use Hyde\Hyde;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Symfony\Component\Console\Output\ConsoleOutput as SymfonyOutput;

use function max;
use function str;
use function trim;
use function strlen;
use function substr;
use function sprintf;
use function str_repeat;
use function str_replace;
use function str_contains;
use function Termwind\render;

class ConsoleOutput
{
protected bool $verbose;
protected SymfonyOutput $output;

public static function printStartMessage(string $host, int $port): void
public function __construct(bool $verbose = false, ?SymfonyOutput $output = null)
{
$this->verbose = $verbose;
$this->output = $output ?? new SymfonyOutput();
}

public function printStartMessage(string $host, int $port): void
{
$title = 'HydePHP Realtime Compiler';
$version = ' v'.Hyde::version();

$url = sprintf('http://%s:%d', $host, $port);
$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('&nbsp;', $width);
Expand All @@ -52,38 +51,33 @@ public static function printStartMessage(string $host, int $port): void
HTML);
}

public static function getFormatter(bool $verbose): Closure
public function getFormatter(): Closure
{
$console = (new static($verbose));

return function (string $type, string $line) use ($console): void {
$console->handleOutput($line);
return function (string $type, string $line): void {
$this->handleOutput($line);
};
}

/** @experimental */
public static function printMessage(string $message, string $context): void
public function printMessage(string $message, string $context): void
{
$consoleOutput = new \Symfony\Component\Console\Output\ConsoleOutput();
$consoleOutput->writeln(sprintf('%s [%s]', $message, $context));
}

public function __construct(bool $verbose = false)
{
$this->verbose = $verbose;
$this->output->writeln(sprintf('%s [%s]', $message, $context));
}

protected function handleOutput(string $buffer): void
{
str($buffer)->trim()->explode("\n")->each(function (string $line): void {
$line = $this->formatLineForOutput($line);

if ($line !== null) {
render($line);
}
$this->renderLine($this->formatLineForOutput($line));
});
}

protected function renderLine(?string $line): void
{
if ($line !== null) {
render($line);
}
}

protected function formatLineForOutput(string $line): ?string
{
if (str_contains($line, 'Development Server (http:')) {
Expand All @@ -96,11 +90,7 @@ protected function formatLineForOutput(string $line): ?string
return $this->verbose ? $this->formatRequestStatusLine($line) : null;
}
if (str_contains($line, '[dashboard@')) {
$message = trim(Str::before($line, '[dashboard@'));
$context = trim(trim(Str::after($line, $message)), '[]');
$success = str_contains($message, 'Created') || str_contains($message, 'Updated');

return $this->formatLine($message, Carbon::now(), $success ? 'green-500' : 'blue-500', $context);
return $this->formatDashboardContextLine($line);
}

return $this->formatLine($line, Carbon::now());
Expand Down Expand Up @@ -133,7 +123,16 @@ protected function formatRequestStatusLine(string $line): string
return $this->formatLine(sprintf('%s %s', $address, $status), $this->parseDate($line));
}

protected static function formatLine(string $message, Carbon $date, string $iconColor = 'blue-500', string $context = ''): string
protected function formatDashboardContextLine(string $line): string
{
$message = trim(Str::before($line, '[dashboard@'));
$context = trim(trim(Str::after($line, $message)), '[]');
$success = str_contains($message, 'Created') || str_contains($message, 'Updated');

return $this->formatLine($message, Carbon::now(), $success ? 'green-500' : 'blue-500', $context);
}

protected function formatLine(string $message, Carbon $date, string $iconColor = 'blue-500', string $context = ''): string
{
if ($context) {
$context = "$context ";
Expand Down
4 changes: 3 additions & 1 deletion packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class DashboardController
public string $title;

protected Request $request;
protected ConsoleOutput $console;
protected bool $isAsync = false;

protected array $flashes = [];
Expand All @@ -56,6 +57,7 @@ public function __construct()
{
$this->title = config('hyde.name').' - Dashboard';
$this->request = Request::capture();
$this->console = new ConsoleOutput();

$this->loadFlashData();

Expand Down Expand Up @@ -354,7 +356,7 @@ protected function createPage(): void
$this->abort($exception->getCode(), $exception->getMessage());
}

ConsoleOutput::printMessage("Created file '$path'", 'dashboard@createPage');
$this->console->printMessage("Created file '$path'", 'dashboard@createPage');

$this->flash('justCreatedPage', RouteKey::fromPage($pageClass, $pageClass::pathToIdentifier($path))->get());
$this->setJsonResponse(201, "Created file '$path'!");
Expand Down

0 comments on commit cd9add2

Please sign in to comment.