From c4031b71f88e83d54b94fc0d51ab0b3d6e0f9427 Mon Sep 17 00:00:00 2001 From: Dominique FOURNIER Date: Fri, 18 Mar 2022 10:57:12 +0100 Subject: [PATCH] Console : manage if Console is called from a pipe (without tty available) --- src/Console.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Console.php b/src/Console.php index 306db3d..b6a749f 100644 --- a/src/Console.php +++ b/src/Console.php @@ -89,13 +89,13 @@ class Console { if (! function_exists ("exec")) throw $this->ConsoleException ("No exec support in PHP"); - $this->initSttyState = exec ("stty -g"); + $this->initSttyState = exec ("stty -g 2>/dev/null"); // Set the terminal to return the value each time a key is pressed. // Do not display anything, so we don't see the characters when the user is // deleting. // 'intr ^J' allow to redefine the interruption from Ctrl+C to Ctrl+J. It // allow to manage the Ctrl+C key to clean the entry - exec ("stty -echo -icanon min 1 time 0"); + exec ("stty -echo -icanon min 1 time 0 2>/dev/null"); $this->updateTerminalSize (); } // }}} @@ -107,7 +107,7 @@ class Console { $this->termWidth = 80; $this->termHeight = 25; - $termSize = exec ("stty size", $null, $rc); + $termSize = exec ("stty size 2>/dev/null", $null, $rc); if ($rc === 0) { list ($termHeight, $termWidth) = explode (" ", $termSize); @@ -128,7 +128,8 @@ class Console public function __destruct () // {{{ { - exec ("stty $this->initSttyState"); + if ($this->initSttyState !== "") + exec ("stty $this->initSttyState"); $this->colorReset (); $this->textUnderline (false); $this->textBold (false); @@ -207,7 +208,7 @@ class Console $char = fgetc (STDIN); $sequence .= $char; } - elseif (ord ($char2) === 91) + elseif (ord ($char2) === 91 || ord($char2) === 93) { // Start an ESC CSI sequence. Do not display it, just return it. // The ESC squences are used to communicate the cursor keys, associated @@ -412,6 +413,13 @@ class Console $this->debug ("Autocompletion : end '$prompt.$string'"); } // }}} + elseif (ord ($char) === 0) + // End of file + { + $this->debug("Empty File entry : ".ord($char)); + $string = chr(0); + break; + } elseif (ord ($char) === 3) // Abort (Ctrl+C) // {{{