From 3e97c51418e6d27e9a81906f347fcb7c78e27d4f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:23:16 +0200 Subject: Adding upstream version 0.20.0. Signed-off-by: Daniel Baumann --- vendor/gipfl/cli/src/Tty.php | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 vendor/gipfl/cli/src/Tty.php (limited to 'vendor/gipfl/cli/src/Tty.php') diff --git a/vendor/gipfl/cli/src/Tty.php b/vendor/gipfl/cli/src/Tty.php new file mode 100644 index 0000000..efe5924 --- /dev/null +++ b/vendor/gipfl/cli/src/Tty.php @@ -0,0 +1,132 @@ +loop = $loop; + register_shutdown_function([$this, 'restore']); + $loop->futureTick(function () { + $this->initialize(); + }); + } + + public function setEcho($echo) + { + if (! is_bool($echo) && ! is_string($echo) && strlen($echo) !== 1) { + throw new InvalidArgumentException( + "\$echo must be boolean or a single character, got " . var_export($echo, 1) + ); + } + $this->echo = $echo; + if ($this->ttyMode) { + if ($echo) { + $this->ttyMode->enableFeature('echo'); + } else { + $this->ttyMode->disableFeature('echo'); + } + } + + return $this; + } + + public function stdin() + { + if ($this->stdin === null) { + $this->assertValidStdin(); + $this->stdin = new ReadableResourceStream(STDIN, $this->loop); + } + + return $this->stdin; + } + + protected function hasStdin() + { + return defined('STDIN') && is_resource(STDIN) && fstat(STDIN) !== false; + } + + protected function assertValidStdin() + { + if (! $this->hasStdin()) { + throw new RuntimeException('I have no STDIN'); + } + } + + public function stdout() + { + if ($this->stdout === null) { + $this->assertValidStdout(); + $this->stdout = new WritableResourceStream(STDOUT, $this->loop); + } + + return $this->stdout; + } + + protected function hasStdout() + { + return defined('STDOUT') && is_resource(STDOUT) && fstat(STDOUT) !== false; + } + + protected function assertValidStdout() + { + if (! $this->hasStdout()) { + throw new RuntimeException('I have no STDOUT'); + } + } + + protected function initialize() + { + $this->ttyMode = new TtyMode(); + $this->ttyMode->setPreferredMode($this->echo); + } + + public static function isSupported() + { + if (PHP_VERSION_ID >= 70200) { + return stream_isatty(STDIN); + } elseif (function_exists('posix_isatty')) { + return posix_isatty(STDIN); + } else { + return false; + } + } + + public function restore() + { + if ($this->hasStdin()) { + // ReadableResourceStream sets blocking to false, let's restore this + stream_set_blocking(STDIN, true); + } + } +} -- cgit v1.2.3