Skip to content

Commit

Permalink
Refactor ConsoleOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 26, 2024
1 parent 18b5eac commit ccb358f
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 173 deletions.
18 changes: 9 additions & 9 deletions app/Console/Hello.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@

namespace App\Console;

use Tempest\Console\Console;
use Tempest\Console\ConsoleArgument;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\ConsoleOutput;

final readonly class Hello
{
public function __construct(
private ConsoleOutput $output,
private Console $console,
) {
}

// hello:world {input} --flag
#[ConsoleCommand]
public function world(string $input)
{
$this->output->info('Hi');
$this->output->error($input);
$this->console->info('Hi');
$this->console->error($input);
}

// hello:werld {input} --flag
#[ConsoleCommand]
public function werdl(string $input)
{
$this->output->info('Hi');
$this->output->error($input);
$this->console->info('Hi');
$this->console->error($input);
}

#[ConsoleCommand(
Expand All @@ -42,12 +42,12 @@ public function test(
) {
$value = $optionalValue ?? 'null';

$this->output->info("{$value}");
$this->console->info("{$value}");

if ($flag) {
$this->output->info('flag');
$this->console->info('flag');
} else {
$this->output->info('no-flag');
$this->console->info('no-flag');
}
}
}
9 changes: 4 additions & 5 deletions src/Actions/RenderConsoleCommandHelp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\ConsoleOutput;

final readonly class RenderConsoleCommandHelp
{
Expand All @@ -17,18 +16,18 @@ public function __construct(private Console $console)
public function __invoke(ConsoleCommand $consoleCommand): void
{
$this->console
->when($consoleCommand->help, fn (ConsoleOutput $output) => $output->writeln("<comment>{$consoleCommand->help}</comment>"))
->when($consoleCommand->help, fn (Console $console) => $console->writeln("<comment>{$consoleCommand->help}</comment>"))
->write('<h2>Usage</h2>');

(new RenderConsoleCommand($this->console))($consoleCommand);

foreach ($consoleCommand->getArgumentDefinitions() as $argumentDefinition) {
$this->console
->writeln()
->when($argumentDefinition->help, fn (ConsoleOutput $output) => $output->writeln('<comment>' . $argumentDefinition->help . '</comment>'))
->when($argumentDefinition->help, fn (Console $console) => $console->writeln('<comment>' . $argumentDefinition->help . '</comment>'))
->write("<em>{$argumentDefinition->name}</em>")
->when($argumentDefinition->aliases !== [], fn (ConsoleOutput $output) => $output->write(' (' . implode(', ', $argumentDefinition->aliases) . ')'))
->when($argumentDefinition->description, fn (ConsoleOutput $output) => $output->write('' . $argumentDefinition->description))
->when($argumentDefinition->aliases !== [], fn (Console $console) => $console->write(' (' . implode(', ', $argumentDefinition->aliases) . ')'))
->when($argumentDefinition->description, fn (Console $console) => $console->write('' . $argumentDefinition->description))
;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Actions/RenderConsoleCommandOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
namespace Tempest\Console\Actions;

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

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

public function __invoke(): void
{
$this->output
$this->console
->writeln("<h1>{$this->consoleConfig->name}</h1>")
->when(
expression: $this->appConfig->discoveryCache,
callback: fn (ConsoleOutput $output) => $output->error('Discovery cache is enabled!')
callback: fn (Console $console) => $console->error('Discovery cache is enabled!')
);

/** @var \Tempest\Console\ConsoleCommand[][] $commands */
Expand All @@ -40,12 +40,12 @@ public function __invoke(): void
ksort($commands);

foreach ($commands as $group => $commandsForGroup) {
$this->output
$this->console
->writeln()
->writeln('<h2>' . ucfirst($group) . '</h2>');

foreach ($commandsForGroup as $consoleCommand) {
(new RenderConsoleCommand($this->output))($consoleCommand);
(new RenderConsoleCommand($this->console))($consoleCommand);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public function confirm(string $question, bool $default = false): bool;
public function password(string $label = 'Password', bool $confirm = false): string;

public function progressBar(iterable $data, Closure $handler): array;

public function info(string $line): self;

public function error(string $line): self;

public function success(string $line): self;

public function when(mixed $expression, callable $callback): self;
}
14 changes: 2 additions & 12 deletions src/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@

interface ConsoleOutput
{
public function delimiter(string $delimiter): ConsoleOutput;
public function write(string $contents): static;

public function write(string $contents): ConsoleOutput;

public function writeln(string $line = ''): ConsoleOutput;

public function info(string $line): ConsoleOutput;

public function error(string $line): ConsoleOutput;

public function success(string $line): ConsoleOutput;

public function when(mixed $expression, callable $callback): ConsoleOutput;
public function writeln(string $line = ''): static;
}
3 changes: 1 addition & 2 deletions src/Exceptions/ConsoleExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Tempest\Application;
use Tempest\Console\Console;
use Tempest\Console\ConsoleApplication;
use Tempest\Console\ConsoleOutput;
use Tempest\ExceptionHandler;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\LightTerminalTheme;
Expand All @@ -27,7 +26,7 @@ public function handle(Throwable $throwable): void
->error($throwable::class)
->when(
expression: $throwable->getMessage(),
callback: fn (ConsoleOutput $output) => $output->error($throwable->getMessage()),
callback: fn (Console $console) => $console->error($throwable->getMessage()),
)
->writeln();

Expand Down
35 changes: 20 additions & 15 deletions src/GenericConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public function __construct(
) {
}

public function delimiter(string $delimiter): ConsoleOutput
{
return $this->output->delimiter($delimiter);
}

public function readln(): string
{
return $this->input->readln();
Expand All @@ -36,38 +31,48 @@ public function read(int $bytes): string
return $this->input->read($bytes);
}

public function write(string $contents): self
public function write(string $contents): static
{
$this->output->write($contents);

return $this;
}

public function writeln(string $line = ''): self
public function writeln(string $line = ''): static
{
$this->output->writeln($line);

return $this;
}

public function info(string $line): ConsoleOutput
public function info(string $line): self
{
return $this->output->info($line);
$this->writeln("<em>{$line}</em>");

return $this;
}

public function error(string $line): ConsoleOutput
public function error(string $line): self
{
return $this->output->error($line);
$this->writeln("<error>{$line}</error>");

return $this;
}

public function success(string $line): ConsoleOutput
public function success(string $line): self
{
return $this->output->success($line);
$this->writeln("<success>{$line}</success>");

return $this;
}

public function when(mixed $expression, callable $callback): ConsoleOutput
public function when(mixed $expression, callable $callback): self
{
return $this->output->when($expression, $callback);
if ($expression) {
$callback($this);
}

return $this;
}

public function component(ConsoleComponent $component, array $validation = []): mixed
Expand Down
51 changes: 5 additions & 46 deletions src/GenericConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,22 @@
use Tempest\Console\Highlight\TempestConsoleLanguage;
use Tempest\Highlight\Highlighter;

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

public function __construct(private readonly Highlighter $highlighter)
public function __construct(private Highlighter $highlighter)
{
}

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

$clone->delimiter = $delimiter;

return $clone;
}

public function write(string $contents): ConsoleOutput
public function write(string $contents): static
{
$this->writeToStdOut($contents);

return $this;
}

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

return $this;
}

public function info(string $line): ConsoleOutput
{
$this->writeln("<em>{$line}</em>");

return $this;
}

public function error(string $line): ConsoleOutput
{
$this->writeln("<error>{$line}</error>");

return $this;
}

public function success(string $line): ConsoleOutput
{
$this->writeln("<success>{$line}</success>");

return $this;
}

public function when(mixed $expression, callable $callback): ConsoleOutput
public function writeln(string $line = ''): static
{
if ($expression) {
$callback($this);
}
$this->writeToStdOut($line . PHP_EOL);

return $this;
}
Expand Down
33 changes: 2 additions & 31 deletions src/NullConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,12 @@ final class NullConsoleOutput implements ConsoleOutput
{
public string $delimiter = PHP_EOL;

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

$this->delimiter = $delimiter;

return $clone;
}

public function write(string $contents): ConsoleOutput
{
return $this;
}

public function writeln(string $line = ''): ConsoleOutput
{
return $this;
}

public function info(string $line): ConsoleOutput
{
return $this;
}

public function error(string $line): ConsoleOutput
{
return $this;
}

public function success(string $line): ConsoleOutput
public function write(string $contents): static
{
return $this;
}

public function when(mixed $expression, callable $callback): ConsoleOutput
public function writeln(string $line = ''): static
{
return $this;
}
Expand Down
Loading

0 comments on commit ccb358f

Please sign in to comment.