From 57739c624bcc1d5f4e31565fd2e303235f1e9f04 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 13 Jun 2018 13:22:49 +0000 Subject: [PATCH] console : manage the Ctrl+C to clean the line if needed git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4241 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/console.php b/console.php index b7b4fac..72d1c33 100644 --- a/console.php +++ b/console.php @@ -8,6 +8,11 @@ * When using this class, you must use the $console::echo method and not * display directely on screen * Like readline, but all in PHP + * Manage the historical of the provided commands + * Allow the user to use arrow keys and the shortcuts Ctrl+arrow, Ctrl+L, + * Ctrl+U, Ctrl+W + * To not allow the stop of the program by Ctrl+C, you can add + * exec ("stty intr ^J"); */ class console { @@ -29,7 +34,7 @@ private $usleep = 0; /** List of non printable chars in decimal. The non printable chars are not * displayed but are correctely captured */ - private $nonWriteableChar = array (1, 2, 4, 6, 8, 9, 16, 18, + private $nonWriteableChar = array (1, 2, 3, 4, 6, 8, 9, 16, 18, 20, 21, 22, 23, 24, 25, 27, 127); /* The history list in an array @@ -69,6 +74,8 @@ private $usleep = 0; // 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"); $this->updateTerminalSize (); } @@ -121,7 +128,8 @@ private $usleep = 0; } // }}} - /** Display a text on screen + /** Display a text on screen. Must be used before "readline" method because + * the provided message will not be deleted by readline process. * @param string $message The message to display */ public function echo ($message) @@ -326,10 +334,16 @@ private $usleep = 0; $this->moveCursor ($cursorPos); } // }}} + elseif (ord ($char) === 3) + // Abort (Ctrl+C) + { + $this->clearLine (); + return ""; + } elseif (ord ($char) === 4) + // Logout (Ctrl+D) // {{{ { - // Logout (Ctrl+D) $string = "exit"; $this->rewriteLine ($prompt.$string); return $string;