Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Nov 3, 2024
1 parent 063804d commit 087f5ab
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 64 deletions.
55 changes: 44 additions & 11 deletions src/mako/application/cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
use mako\cli\input\arguments\exceptions\UnexpectedValueException;
use mako\cli\input\Input;
use mako\cli\input\reader\Reader;
use mako\cli\input\reader\ReaderInterface;
use mako\cli\output\Cursor;
use mako\cli\output\formatter\Formatter;
use mako\cli\output\Output;
use mako\cli\output\writer\Error;
use mako\cli\output\writer\Standard;
use mako\cli\output\writer\WriterInterface;
use mako\cli\signals\SignalHandler;
use mako\database\ConnectionManager as DatabaseConnectionManager;
use mako\file\Finder;
Expand All @@ -42,7 +45,6 @@
use mako\reactor\Reactor;
use ReflectionClass;

use function array_shift;
use function file_get_contents;
use function ob_start;
use function putenv;
Expand All @@ -59,24 +61,51 @@ class Application extends BaseApplication
protected Reactor $reactor;

/**
* Creates a input instance.
* Creates a reader instance.
*/
protected function inputFactory(): Input
protected function readerFactory(): Reader
{
/** @var array $argv */
$argv = $_SERVER['argv'];
return new Reader;
}

array_shift($argv); // Remove the script name
/**
* Creates a standard writer instance.
*/
protected function standardWriterFactory(): Standard
{
return new Standard;
}

return new Input(new Reader, new ArgvParser($argv));
/**
* Creates an error writer instance.
*/
protected function errorWriterFactory(): Error
{
return new Error;
}

/**
* Creates a input instance.
*/
protected function inputFactory(Reader $reader): Input
{
return new Input($reader, ArgvParser::fromArgv());
}

/**
* Creates a cursor instance.
*/
public function cursorFactory(WriterInterface $writer, ReaderInterface $reader): Cursor
{
return new Cursor($writer, $reader);
}

/**
* Creates an output instance.
*/
protected function outputFactory(): Output
protected function outputFactory(Standard $standard, Error $error, Cursor $cursor): Output
{
return new Output(new Standard, new Error, formatter: new Formatter);
return new Output($standard, $error, formatter: new Formatter, cursor: $cursor);
}

/**
Expand Down Expand Up @@ -143,11 +172,15 @@ protected function registerAndhandleGlobalArguments(): void
*/
protected function startReactor(): void
{
$reader = $this->readerFactory();
$standard = $this->standardWriterFactory();
$error = $this->errorWriterFactory();

// Register input, output and signal handler instances

$this->container->registerSingleton([Input::class, 'input'], fn () => $this->inputFactory());
$this->container->registerInstance([Input::class, 'input'], $this->inputFactory($reader));

$output = $this->outputFactory();
$output = $this->outputFactory($standard, $error, $this->cursorFactory($standard, $reader));

$this->container->registerInstance([Output::class, 'output'], $output);

Expand Down
15 changes: 0 additions & 15 deletions src/mako/cli/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,6 @@ public function restoreStty(): void
}
}

/**
* Executes a callable in a stty sandbox.
*/
public function sttySandbox(callable $callable): mixed
{
$settings = shell_exec('stty -g');

try {
return $callable();
}
finally {
exec("stty {$settings}");
}
}

/**
* Attempts to get dimensions for Windows.
*/
Expand Down
15 changes: 14 additions & 1 deletion src/mako/cli/input/arguments/ArgvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,26 @@ class ArgvParser
/**
* Constructor.
*/
public function __construct(
final public function __construct(
protected array $argv,
array $arguments = []
) {
$this->addArguments($arguments);
}

/**
* Creates a new instance from the $_SERVER['argv'] array.
*/
public static function fromArgv(): static
{
/** @var array $argv */
$argv = $_SERVER['argv'];

array_shift($argv); // Remove the script name

return new static($argv);
}

/**
* Returns the registered arguments.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/mako/cli/input/helpers/Secret.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace mako\cli\input\helpers;

use mako\cli\exceptions\CliException;
use mako\cli\traits\SttySandboxTrait;

use function escapeshellcmd;
use function exec;
Expand All @@ -19,6 +20,7 @@
*/
class Secret extends Question
{
use SttySandboxTrait;
/**
* Writes question to output and returns hidden user input.
*/
Expand All @@ -30,7 +32,7 @@ public function ask(string $question, mixed $default = null, bool $fallback = fa
$this->displayPrompt($question);

if ($hasStty) {
$answer = $this->output->getEnvironment()->sttySandbox(function (): string {
$answer = $this->sttySandbox(function (): string {
exec('stty -echo');
return $this->input->read();
});
Expand Down
50 changes: 28 additions & 22 deletions src/mako/cli/output/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@

namespace mako\cli\output;

use mako\cli\input\reader\ReaderInterface;
use mako\cli\output\writer\WriterInterface;
use mako\cli\traits\SttySandboxTrait;

use function exec;
use function fgetc;
use function sscanf;

/**
* Cursor.
*/
class Cursor
{
use SttySandboxTrait;

/**
* Is the cursor hidden?
*/
Expand All @@ -25,7 +30,8 @@ class Cursor
* Constructor.
*/
public function __construct(
protected Output $output
protected WriterInterface $writer,
protected ReaderInterface $reader,
) {
}

Expand All @@ -50,7 +56,7 @@ public function isHidden(): bool
*/
public function hide(): void
{
$this->output->write("\033[?25l");
$this->writer->write("\033[?25l");

$this->hidden = true;
}
Expand All @@ -60,7 +66,7 @@ public function hide(): void
*/
public function show(): void
{
$this->output->write("\033[?25h");
$this->writer->write("\033[?25h");

$this->hidden = false;
}
Expand All @@ -80,55 +86,55 @@ public function restore(): void
*/
public function beginningOfLine(): void
{
$this->output->write("\r");
$this->writer->write("\r");
}

/**
* Moves the cursor up.
*/
public function up(int $lines = 1): void
{
$this->output->write("\033[{$lines}A");
$this->writer->write("\033[{$lines}A");
}

/**
* Moves the cursor down.
*/
public function down(int $lines = 1): void
{
$this->output->write("\033[{$lines}B");
$this->writer->write("\033[{$lines}B");
}

/**
* Moves the cursor right.
* Moves the cursor left.
*/
public function right(int $columns = 1): void
public function left(int $columns = 1): void
{
$this->output->write("\033[{$columns}C");
$this->writer->write("\033[{$columns}D");
}

/**
* Moves the cursor left.
* Moves the cursor right.
*/
public function left(int $columns = 1): void
public function right(int $columns = 1): void
{
$this->output->write("\033[{$columns}D");
$this->writer->write("\033[{$columns}C");
}

/**
* Moves the cursor to a specific position.
*/
public function moveTo(int $row, int $column): void
{
$this->output->write("\033[{$row};{$column}H");
$this->writer->write("\033[{$row};{$column}H");
}

/**
* Moves the cursor to the beginning of the line.
*/
public function moveToBeginningOfLine(): void
{
$this->output->write("\r");
$this->writer->write("\r");
}

/**
Expand All @@ -146,15 +152,15 @@ public function moveToEndOfLine(): void
*/
public function getPosition(): array
{
$response = $this->output->getEnvironment()->sttySandbox(function (): string {
$response = $this->sttySandbox(function (): string {
exec('stty -echo -icanon');

$this->output->write("\033[6n");
$this->writer->write("\033[6n");

$response = '';

while (true) {
$char = fgetc(STDIN);
$char = $this->reader->readCharacter();

if ($char === 'R') {
return "{$response}R";
Expand All @@ -174,15 +180,15 @@ public function getPosition(): array
*/
public function clearLine(): void
{
$this->output->write("\r\33[2K");
$this->writer->write("\r\33[2K");
}

/**
* Clears the line from the cursor.
*/
public function clearLineFromCursor(): void
{
$this->output->write("\33[K");
$this->writer->write("\33[K");
}

/**
Expand All @@ -204,14 +210,14 @@ public function clearLines(int $lines): void
*/
public function clearScreen(): void
{
$this->output->write("\033[H\033[2J");
$this->writer->write("\033[H\033[2J");
}

/**
* Clears the screen from the cursor.
*/
public function clearScreenFromCursor(): void
{
$this->output->write("\033[J");
$this->writer->write("\033[J");
}
}
7 changes: 1 addition & 6 deletions src/mako/cli/output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class Output
*/
protected bool $muted = false;

/**
* Cursor.
*/
protected Cursor $cursor;

/**
* Constructor.
*/
Expand All @@ -48,8 +43,8 @@ public function __construct(
protected WriterInterface $error = new Error,
protected Environment $environment = new Environment,
protected ?FormatterInterface $formatter = null,
protected ?Cursor $cursor = null
) {
$this->cursor = new Cursor($this);
}

/**
Expand Down
Loading

0 comments on commit 087f5ab

Please sign in to comment.