Skip to content

Commit

Permalink
Improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 5, 2024
1 parent a704ccb commit 0f06e64
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 107 deletions.
7 changes: 5 additions & 2 deletions src/Actions/RenderConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use ReflectionParameter;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\ConsoleOutput;
use Tempest\Console\ConsoleStyle;

final readonly class RenderConsoleCommand
{
public function __invoke(ConsoleCommand $consoleCommand): string
public function __construct(private ConsoleOutput $output) {}

public function __invoke(ConsoleCommand $consoleCommand): void
{
$parts = [ConsoleStyle::FG_DARK_BLUE($consoleCommand->getName())];

Expand All @@ -22,7 +25,7 @@ public function __invoke(ConsoleCommand $consoleCommand): string
$parts[] = "- {$consoleCommand->getDescription()}";
}

return implode(' ', $parts);
$this->output->writeln(' ' . implode(' ', $parts));
}

private function renderParameter(ReflectionParameter $parameter): string
Expand Down
32 changes: 14 additions & 18 deletions src/Actions/RenderConsoleCommandOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@

use Tempest\AppConfig;
use Tempest\Console\ConsoleConfig;
use Tempest\Console\ConsoleStyle;
use Tempest\Console\ConsoleOutput;
use Tempest\Console\ConsoleOutputType;

final readonly class RenderConsoleCommandOverview
{
public function __construct(
private ConsoleOutput $output,
private AppConfig $appConfig,
private ConsoleConfig $consoleConfig,
) {
}

public function __invoke(): string
public function __invoke(): void
{
$lines = [
ConsoleStyle::BOLD(ConsoleStyle::BG_DARK_BLUE(" {$this->consoleConfig->name} ")),
];

if ($this->appConfig->discoveryCache) {
$lines[] = ConsoleStyle::BG_RED(' Discovery cache is enabled! ');
}

$lines[] = '';
$this->output
->writeln($this->consoleConfig->name, ConsoleOutputType::H1)
->when(
expression: $this->appConfig->discoveryCache,
callback: fn (ConsoleOutput $output) => $output->error('Discovery cache is enabled!')
);

/** @var \Tempest\Console\ConsoleCommand[][] $commands */
$commands = [];
Expand All @@ -42,16 +41,13 @@ public function __invoke(): string
ksort($commands);

foreach ($commands as $group => $commandsForGroup) {
$lines[] = ConsoleStyle::BOLD(ConsoleStyle::BG_BLUE(' ' . ucfirst($group) . ' '));
$this->output
->writeln()
->writeln(ucfirst($group), ConsoleOutputType::H2);

foreach ($commandsForGroup as $consoleCommand) {
$renderedConsoleCommand = (new RenderConsoleCommand())($consoleCommand);
$lines[] = " {$renderedConsoleCommand}";
(new RenderConsoleCommand($this->output))($consoleCommand);
}

$lines[] = '';
}

return implode(PHP_EOL, $lines);
}
}
4 changes: 1 addition & 3 deletions src/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public function run(): void
try {
$commandName = $this->args[1] ?? null;

$output = $this->container->get(ConsoleOutput::class);

if (! $commandName) {
$output->writeln($this->container->get(RenderConsoleCommandOverview::class)());
$this->container->get(RenderConsoleCommandOverview::class)();

return;
}
Expand Down
14 changes: 9 additions & 5 deletions src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

interface ConsoleOutput
{
public function write(string $line): void;
public function delimiter(string $delimiter): ConsoleOutput;

public function writeln(string $line): void;
public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput;

public function info(string $line): void;
public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput;

public function error(string $line): void;
public function info(string $line): ConsoleOutput;

public function success(string $line): void;
public function error(string $line): ConsoleOutput;

public function success(string $line): ConsoleOutput;

public function when(mixed $expression, callable $callback): ConsoleOutput;
}
52 changes: 52 additions & 0 deletions src/ConsoleOutputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tempest\Console;

enum ConsoleOutputType
{
case DEFAULT;
case INFO;
case ERROR;
case SUCCESS;
case COMMENT;
case H1;
case H2;

public function style(string $input): string
{
return match ($this) {
self::INFO => ConsoleStyle::FG_DARK_BLUE($input),
self::ERROR => ConsoleStyle::BOLD(ConsoleStyle::FG_WHITE(ConsoleStyle::BG_RED(ConsoleStyle::FG_WHITE($this->error($input))))),
self::SUCCESS => ConsoleStyle::BOLD(ConsoleStyle::FG_DARK_GREEN($input)),
self::H1 => ConsoleStyle::BOLD(ConsoleStyle::FG_WHITE(ConsoleStyle::BG_DARK_BLUE($this->header($input)))),
self::H2 => ConsoleStyle::BG_BLUE($this->header($input)),
self::COMMENT => ConsoleStyle::FG_GRAY($this->comment($input)),
default => $input,
};
}

private function header(string $input): string
{
return " {$input} ";
}

private function error(string $input): string
{
return " {$input} ";
}

private function comment(string $input): string
{
return implode(
PHP_EOL,
[
'/*',
...array_map(
fn (string $line) => "* {$line}",
explode(PHP_EOL, $input),
),
'*/'
],
);
}
}
42 changes: 29 additions & 13 deletions src/GenericConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@

namespace Tempest\Console;

readonly class GenericConsole implements Console
final class GenericConsole implements Console
{
private string $delimiter = PHP_EOL;

Check failure on line 9 in src/GenericConsole.php

View workflow job for this annotation

GitHub Actions / Perform Static Analysis

Property Tempest\Console\GenericConsole::$delimiter is never read, only written.

public function __construct(
private ConsoleInput $input,
private ConsoleOutput $output,
private readonly ConsoleInput $input,
private readonly ConsoleOutput $output,
) {
}

public function delimiter(string $delimiter): ConsoleOutput
{
$clone = clone $this;

$this->delimiter = $delimiter;

return $clone;
}

public function readln(): string
{
return $this->input->readln();
Expand All @@ -27,28 +38,33 @@ public function confirm(string $question, bool $default = false): bool
return $this->input->confirm($question, $default);
}

public function write(string $line): void
public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput
{
return $this->output->write($line, $type);
}

public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput
{
$this->output->write($line);
return $this->output->writeln($line, $type);
}

public function writeln(string $line): void
public function info(string $line): ConsoleOutput
{
$this->output->writeln($line);
return $this->output->info($line);
}

public function info(string $line): void
public function error(string $line): ConsoleOutput
{
$this->output->info($line);
return $this->output->error($line);
}

public function error(string $line): void
public function success(string $line): ConsoleOutput
{
$this->output->error($line);
return $this->output->success($line);
}

public function success(string $line): void
public function when(mixed $expression, callable $callback): ConsoleOutput
{
$this->output->success($line);
return $this->output->when($expression, $callback);
}
}
69 changes: 67 additions & 2 deletions src/GenericConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,72 @@

namespace Tempest\Console;

final readonly class GenericConsoleOutput implements ConsoleOutput
final class GenericConsoleOutput implements ConsoleOutput
{
use HandlesConsoleOutput;
public string $delimiter = PHP_EOL;

public function delimiter(string $delimiter): ConsoleOutput
{
$clone = clone $this;

$this->delimiter = $delimiter;

return $clone;
}

public function write(string $line, ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput
{
$this->writeToStdOut($type->style($line));

return $this;
}

public function writeln(string $line = '', ConsoleOutputType $type = ConsoleOutputType::DEFAULT): ConsoleOutput
{
$this->writeToStdOut($type->style($line) . $this->delimiter);

return $this;
}

public function info(string $line): ConsoleOutput
{
$this->writeln($line, ConsoleOutputType::INFO);

return $this;
}

public function error(string $line): ConsoleOutput
{
$this->writeln($line, ConsoleOutputType::ERROR);

return $this;
}

public function success(string $line): ConsoleOutput
{
$this->writeln($line, ConsoleOutputType::SUCCESS);

return $this;
}

public function when(mixed $expression, callable $callback): ConsoleOutput
{
if ($expression) {
$callback($this);
}

return $this;
}

private function writeToStdOut(string $content): void
{
$stdout = fopen('php://stdout', 'w');

fwrite(
$stdout,
ConsoleStyle::RESET($content),
);

fclose($stdout);
}
}
46 changes: 0 additions & 46 deletions src/HandlesConsoleOutput.php

This file was deleted.

Loading

0 comments on commit 0f06e64

Please sign in to comment.