Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Oct 26, 2024
1 parent 16c4cf6 commit 445053e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/mako/application/cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,15 @@ protected function startReactor(): void

$this->container->registerInstance(SignalHandler::class, $signalHandler);

// Ensure that the cursor is restored in case of a SIGINT call
// Ensure that the cursor and stty are restored in case of a SIGINT call

if ($signalHandler->canHandleSignals()) {
$signalHandler->addHandler(SIGINT, function ($signal, $isLast) use ($output): void {
$output->getCursor()->restore();
$output->getEnvironment()->restoreStty();

// If we're the last handler then we exit with status code 130 (SIGINT)

if ($isLast) {
exit(130);
}
Expand Down
76 changes: 64 additions & 12 deletions src/mako/cli/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,70 @@ class Environment
*/
protected null|bool $hasAnsiSupport = null;

/**
* Do we have stty support?
*/
protected null|bool $hasStty = null;

/**
* Stty settings.
*/
protected ?string $sttySettings = null;

/**
* Constructor.
*/
public function __construct()
{
if ($this->hasStty()) {
$this->sttySettings = shell_exec('stty -g');
}
}

/**
* Destructor.
*/
public function __destruct()
{
$this->restoreStty();
}

/**
* Do we have ANSI support?
*/
public function hasAnsiSupport(): bool
{
if ($this->hasAnsiSupport === null) {
$this->hasAnsiSupport = PHP_OS_FAMILY !== 'Windows' || (env('ANSICON') !== null || env('ConEmuANSI') === 'ON');
}

return $this->hasAnsiSupport;
}

/**
* Do we have stty support?
*/
public function hasStty(): bool
{
if ($this->hasStty === null) {
exec('stty 2>&1', $output, $status);

$this->hasStty = $status === 0;
}

return $this->hasStty;
}

/**
* Restores the stty settings.
*/
public function restoreStty(): void
{
if ($this->sttySettings !== null) {
exec("stty {$this->sttySettings}");
}
}

/**
* Attempts to get dimensions for Windows.
*/
Expand Down Expand Up @@ -93,16 +157,4 @@ public function getHeight(): int
{
return $this->getDimensions()['height'];
}

/**
* Do we have ANSI support?
*/
public function hasAnsiSupport(): bool
{
if ($this->hasAnsiSupport === null) {
$this->hasAnsiSupport = PHP_OS_FAMILY !== 'Windows' || (env('ANSICON') !== null || env('ConEmuANSI') === 'ON');
}

return $this->hasAnsiSupport;
}
}
21 changes: 1 addition & 20 deletions src/mako/cli/input/helpers/Secret.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,12 @@
*/
class Secret extends Question
{
/**
* Do we have stty support?
*/
protected static null|bool $hasStty = null;

/**
* Do we have stty support?
*/
protected function hasStty(): bool
{
if (static::$hasStty === null) {
exec('stty 2>&1', $output, $status);

static::$hasStty = $status === 0;
}

return static::$hasStty;
}

/**
* Writes question to output and returns hidden user input.
*/
public function ask(string $question, mixed $default = null, bool $fallback = false): mixed
{
if (PHP_OS_FAMILY === 'Windows' || $this->hasStty()) {
if (PHP_OS_FAMILY === 'Windows' || $this->output->getEnvironment()->hasStty()) {
$this->output->write(trim($question) . ' ');

if (PHP_OS_FAMILY === 'Windows') {
Expand Down

0 comments on commit 445053e

Please sign in to comment.