Skip to content

Commit

Permalink
Merge pull request #36 from sj-i/restore_stty_settings
Browse files Browse the repository at this point in the history
restore the state of stty
  • Loading branch information
sj-i committed Jun 7, 2021
2 parents ae21847 + b5fee90 commit f6bfbf3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Command/Inspector/DaemonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PhpProfiler\Inspector\Settings\GetTraceSettings\GetTraceSettingsFromConsoleInput;
use PhpProfiler\Inspector\Settings\TargetPhpSettings\TargetPhpSettingsFromConsoleInput;
use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettingsFromConsoleInput;
use PhpProfiler\Lib\Console\EchoBackCanceller;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$get_trace_settings
);

exec('stty -icanon -echo');
$echo_back_canceler = new EchoBackCanceller();

Loop::onReadable(
STDIN,
Expand Down
3 changes: 2 additions & 1 deletion src/Inspector/TraceLoopProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace PhpProfiler\Inspector;

use PhpProfiler\Inspector\Settings\TraceLoopSettings\TraceLoopSettings;
use PhpProfiler\Lib\Console\EchoBackCanceller;
use PhpProfiler\Lib\Loop\Loop;
use PhpProfiler\Lib\Loop\LoopBuilder;
use PhpProfiler\Lib\Loop\LoopMiddleware\CallableMiddleware;
Expand All @@ -35,7 +36,7 @@ public function getMainLoop(callable $main, TraceLoopSettings $settings): Loop
{
return $this->loop_builder
->addProcess(RetryOnExceptionMiddleware::class, [$settings->max_retries, [MemoryReaderException::class]])
->addProcess(KeyboardCancelMiddleware::class, [$settings->cancel_key])
->addProcess(KeyboardCancelMiddleware::class, [$settings->cancel_key, new EchoBackCanceller()])
->addProcess(NanoSleepMiddleware::class, [$settings->sleep_nano_seconds])
->addProcess(CallableMiddleware::class, [$main])
->build();
Expand Down
35 changes: 35 additions & 0 deletions src/Lib/Console/EchoBackCanceller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Lib\Console;

final class EchoBackCanceller
{
private ?string $stty_settings;

public function __construct()
{
/** @psalm-suppress ForbiddenCode */
$this->stty_settings = shell_exec('stty -g');
exec('stty -icanon -echo');
}

public function __destruct()
{
if (isset($this->stty_settings)) {
exec('stty ' . $this->stty_settings);
} else {
exec('stty icanon echo');
}
}
}
11 changes: 8 additions & 3 deletions src/Lib/Loop/LoopMiddleware/KeyboardCancelMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace PhpProfiler\Lib\Loop\LoopMiddleware;

use PhpProfiler\Lib\Console\EchoBackCanceller;
use PhpProfiler\Lib\Loop\LoopMiddlewareInterface;

final class KeyboardCancelMiddleware implements LoopMiddlewareInterface
Expand All @@ -21,14 +22,18 @@ final class KeyboardCancelMiddleware implements LoopMiddlewareInterface
private string $cancel_key;
/** @var resource */
private $keyboard_input;
private EchoBackCanceller $echo_back_canceller;

public function __construct(string $cancel_key, LoopMiddlewareInterface $chain)
{
public function __construct(
string $cancel_key,
EchoBackCanceller $echo_back_canceller,
LoopMiddlewareInterface $chain
) {
$this->chain = $chain;
exec('stty -icanon -echo');
$this->keyboard_input = fopen('php://stdin', 'r');
stream_set_blocking($this->keyboard_input, false);
$this->cancel_key = $cancel_key;
$this->echo_back_canceller = $echo_back_canceller;
}

public function invoke(): bool
Expand Down

0 comments on commit f6bfbf3

Please sign in to comment.