From 8b821dd2fd47b1c3df328fc72145b5ce5b098c3a Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sat, 7 Jul 2018 19:01:13 +0000 Subject: [PATCH] console : manage correctely the Ctrl+C git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4271 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/console.php b/console.php index b69ebbe..695b0d3 100644 --- a/console.php +++ b/console.php @@ -24,6 +24,12 @@ class console { // PROPERTIES // {{{ + /** Set the debug on if a filename is provided, or do not debug if false is + * provided + */ + //private $debug = "/tmp/debug"; + private $debug = false; + /** Save the initial stty value */ private $initSttyState; @@ -370,16 +376,19 @@ class console // Abort (Ctrl+C) // {{{ { - $this->clearLine (); + $this->debug ("Abort Ctrl+C : ".ord ($char)); $this->lineContent = ""; - echo "\r$prompt\n"; - return ""; + $string = ""; + $this->clearLine (); + echo "$prompt\n"; + break; } // }}} elseif (ord ($char) === 4) // Logout (Ctrl+D) // {{{ { + $this->debug ("Logout Ctrl+D : ".ord ($char)); $string = "exit\n"; $this->rewriteLine ($prompt.$string); return $string; @@ -389,6 +398,7 @@ class console // Refresh page (Ctrl+L) // {{{ { + $this->debug ("Refresh Ctrl+L : ".ord ($char)); echo "\033[2J\033[;H\033c"; $cursorPos = mb_strlen ($prompt.$string) + 1; $this->rewriteLine ($prompt.$string); @@ -399,6 +409,7 @@ class console // Empty line from prompt to cursor (Ctrl+U) // {{{ { + $this->debug ("Empty line from prompt to cursor Ctrl+U : ".ord ($char)); $string = mb_substr ($string, $cursorPos - $minLength); $cursorPos = $minLength; $this->rewriteLine ($prompt.$string); @@ -409,6 +420,7 @@ class console // Remove the last word (Ctrl+W) // {{{ { + $this->debug ("Remove the last word Ctrl+W : ".ord ($char)); $tmp = mb_substr ($string, 0, $cursorPos - $minLength); $end = mb_substr ($string, $cursorPos - $minLength); $tmp = rtrim ($tmp); @@ -425,6 +437,7 @@ class console // Remove the previous char (Backspace) // {{{ { + $this->debug ("Remove the previous char (Backspace) : ".ord ($char)); if ($cursorPos <= $minLength) continue; $strArr = $this->mb_str_split ($string); @@ -446,6 +459,7 @@ class console // Cursor right + Ctrl : cursor jump by word // {{{ { + $this->debug ("Cursor right + Ctrl"); $tmp = mb_substr ($string, $cursorPos - $minLength); $tmp = ltrim ($tmp); $pos = strpos ($tmp, " "); @@ -460,6 +474,7 @@ class console // Cursor left + Ctrl : cursor jump by word // {{{ { + $this->debug ("Cursor left + Ctrl"); $tmp = mb_substr ($string, 0, $cursorPos - $minLength); $tmp = rtrim ($tmp); $pos = strrpos ($tmp, " "); @@ -472,6 +487,7 @@ class console // Cursor up : display the previous history if defined // {{{ { + $this->debug ("Cursor up"); if ($string !== "" && $historyTmp == "") { $historyTmp = $string; @@ -490,6 +506,7 @@ class console // Cursor down : display the next history if defined // {{{ { + $this->debug ("Cursor down"); if ($historyPos < count ($this->history) - 1) { $historyPos++; @@ -508,6 +525,7 @@ class console // Cursor right // {{{ { + $this->debug ("Cursor right"); if ($cursorPos <= mb_strlen ($this->lineContent)) { $cursorPos++; @@ -519,6 +537,7 @@ class console // Cursor left // {{{ { + $this->debug ("Cursor left"); if ($cursorPos > $minLength) { $cursorPos--; @@ -530,6 +549,7 @@ class console // End key // {{{ { + $this->debug ("End key"); $cursorPos = $minLength + mb_strlen ($string); $this->moveCursor ($cursorPos); } @@ -538,6 +558,7 @@ class console // Home key // {{{ { + $this->debug ("Home key"); $cursorPos = $minLength; $this->moveCursor ($cursorPos); } @@ -546,6 +567,7 @@ class console // Remove the char under the cursor (Delete) // {{{ { + $this->debug ("Delete key"); if ($cursorPos > mb_strlen ($prompt.$string)) continue; $strArr = $this->mb_str_split ($string); @@ -560,12 +582,14 @@ class console // Non writeable char : skip it // {{{ { + $this->debug ("Non writeable char : ".ord ($char)); } // }}} else // Normal char : Add it to the string // {{{ { + $this->debug ("Normal char : ".ord ($char)); $strArr = $this->mb_str_split ($string); $firstArr = array_slice ($strArr, 0, $cursorPos - $minLength); $lastArr = array_slice ($strArr, $cursorPos - $minLength); @@ -578,6 +602,7 @@ class console } // }}} } + $this->debug ("End of readline '$string'"); return $string; } // }}} @@ -589,6 +614,7 @@ class console private function rewriteLine ($text) // {{{ { + $this->debug ("Call rewriteLine ($text)"); if ($this->echoMode) { $this->clearLine (); @@ -605,6 +631,7 @@ class console private function moveCursor ($cursorPos) // {{{ { + $this->debug ("Call moveCursor ($cursorPos)"); if ($cursorPos < 1) $this->consoleException ("MoveCursor lesser than one : $cursorPos"); if ($this->echoMode) @@ -635,22 +662,32 @@ class console public function clearLine () // {{{ { + $this->debug ("Call clearLine"); $oldLength = mb_strlen ($this->lineContent); // 1. Calculate on which line the cursor is positionned $cursorLine = 1 + floor ((-1+$this->cursorPos) / $this->termWidth); $lastLines = 1 + floor ((1+$oldLength) / $this->termWidth); + $this->debug ("==> clearLine : oldLength=$oldLength, ". + "cursorLine=$cursorLine, lastLines=$lastLines"); for ($i = $cursorLine ; $i < $lastLines ; $i++) + { + $this->debug ("==> clearLine : go Down (i=$i<$lastLines)"); echo "\033[1B"; + } // 3. Remove the lines from lastLines to line 1 if ($lastLines > 1) { for ($i = $lastLines ; $i > 1 ; $i--) + { + $this->debug ("==> clearLine : Remove line up (i=$i<$lastLines)"); echo "\r\033[K\033[1A\r"; + } } // 4. Clean the line 1 + $this->debug ("==> clearLine : Remove line 1"); echo "\r\033[K"; $this->lineContent = ""; - $this->cursorPos = 0; + $this->cursorPos = 1; } // }}} @@ -831,9 +868,11 @@ class console private function debug ($data) // {{{ { + if ($this->debug === false) + return; if (is_array ($data) || is_bool ($data)) $data = var_export ($data, true); - file_put_contents ("/tmp/debug", date ("H:i:s")." $data\n", FILE_APPEND); + file_put_contents ($this->debug, date ("H:i:s")." $data\n", FILE_APPEND); } // }}}