console : manage correctely the Ctrl+C

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4271 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-07-07 19:01:13 +00:00
parent 71aa47a032
commit 8b821dd2fd

View File

@@ -24,6 +24,12 @@ class console
{ {
// PROPERTIES // 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 /** Save the initial stty value
*/ */
private $initSttyState; private $initSttyState;
@@ -370,16 +376,19 @@ class console
// Abort (Ctrl+C) // Abort (Ctrl+C)
// {{{ // {{{
{ {
$this->clearLine (); $this->debug ("Abort Ctrl+C : ".ord ($char));
$this->lineContent = ""; $this->lineContent = "";
echo "\r$prompt\n"; $string = "";
return ""; $this->clearLine ();
echo "$prompt\n";
break;
} }
// }}} // }}}
elseif (ord ($char) === 4) elseif (ord ($char) === 4)
// Logout (Ctrl+D) // Logout (Ctrl+D)
// {{{ // {{{
{ {
$this->debug ("Logout Ctrl+D : ".ord ($char));
$string = "exit\n"; $string = "exit\n";
$this->rewriteLine ($prompt.$string); $this->rewriteLine ($prompt.$string);
return $string; return $string;
@@ -389,6 +398,7 @@ class console
// Refresh page (Ctrl+L) // Refresh page (Ctrl+L)
// {{{ // {{{
{ {
$this->debug ("Refresh Ctrl+L : ".ord ($char));
echo "\033[2J\033[;H\033c"; echo "\033[2J\033[;H\033c";
$cursorPos = mb_strlen ($prompt.$string) + 1; $cursorPos = mb_strlen ($prompt.$string) + 1;
$this->rewriteLine ($prompt.$string); $this->rewriteLine ($prompt.$string);
@@ -399,6 +409,7 @@ class console
// Empty line from prompt to cursor (Ctrl+U) // 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); $string = mb_substr ($string, $cursorPos - $minLength);
$cursorPos = $minLength; $cursorPos = $minLength;
$this->rewriteLine ($prompt.$string); $this->rewriteLine ($prompt.$string);
@@ -409,6 +420,7 @@ class console
// Remove the last word (Ctrl+W) // Remove the last word (Ctrl+W)
// {{{ // {{{
{ {
$this->debug ("Remove the last word Ctrl+W : ".ord ($char));
$tmp = mb_substr ($string, 0, $cursorPos - $minLength); $tmp = mb_substr ($string, 0, $cursorPos - $minLength);
$end = mb_substr ($string, $cursorPos - $minLength); $end = mb_substr ($string, $cursorPos - $minLength);
$tmp = rtrim ($tmp); $tmp = rtrim ($tmp);
@@ -425,6 +437,7 @@ class console
// Remove the previous char (Backspace) // Remove the previous char (Backspace)
// {{{ // {{{
{ {
$this->debug ("Remove the previous char (Backspace) : ".ord ($char));
if ($cursorPos <= $minLength) if ($cursorPos <= $minLength)
continue; continue;
$strArr = $this->mb_str_split ($string); $strArr = $this->mb_str_split ($string);
@@ -446,6 +459,7 @@ class console
// Cursor right + Ctrl : cursor jump by word // Cursor right + Ctrl : cursor jump by word
// {{{ // {{{
{ {
$this->debug ("Cursor right + Ctrl");
$tmp = mb_substr ($string, $cursorPos - $minLength); $tmp = mb_substr ($string, $cursorPos - $minLength);
$tmp = ltrim ($tmp); $tmp = ltrim ($tmp);
$pos = strpos ($tmp, " "); $pos = strpos ($tmp, " ");
@@ -460,6 +474,7 @@ class console
// Cursor left + Ctrl : cursor jump by word // Cursor left + Ctrl : cursor jump by word
// {{{ // {{{
{ {
$this->debug ("Cursor left + Ctrl");
$tmp = mb_substr ($string, 0, $cursorPos - $minLength); $tmp = mb_substr ($string, 0, $cursorPos - $minLength);
$tmp = rtrim ($tmp); $tmp = rtrim ($tmp);
$pos = strrpos ($tmp, " "); $pos = strrpos ($tmp, " ");
@@ -472,6 +487,7 @@ class console
// Cursor up : display the previous history if defined // Cursor up : display the previous history if defined
// {{{ // {{{
{ {
$this->debug ("Cursor up");
if ($string !== "" && $historyTmp == "") if ($string !== "" && $historyTmp == "")
{ {
$historyTmp = $string; $historyTmp = $string;
@@ -490,6 +506,7 @@ class console
// Cursor down : display the next history if defined // Cursor down : display the next history if defined
// {{{ // {{{
{ {
$this->debug ("Cursor down");
if ($historyPos < count ($this->history) - 1) if ($historyPos < count ($this->history) - 1)
{ {
$historyPos++; $historyPos++;
@@ -508,6 +525,7 @@ class console
// Cursor right // Cursor right
// {{{ // {{{
{ {
$this->debug ("Cursor right");
if ($cursorPos <= mb_strlen ($this->lineContent)) if ($cursorPos <= mb_strlen ($this->lineContent))
{ {
$cursorPos++; $cursorPos++;
@@ -519,6 +537,7 @@ class console
// Cursor left // Cursor left
// {{{ // {{{
{ {
$this->debug ("Cursor left");
if ($cursorPos > $minLength) if ($cursorPos > $minLength)
{ {
$cursorPos--; $cursorPos--;
@@ -530,6 +549,7 @@ class console
// End key // End key
// {{{ // {{{
{ {
$this->debug ("End key");
$cursorPos = $minLength + mb_strlen ($string); $cursorPos = $minLength + mb_strlen ($string);
$this->moveCursor ($cursorPos); $this->moveCursor ($cursorPos);
} }
@@ -538,6 +558,7 @@ class console
// Home key // Home key
// {{{ // {{{
{ {
$this->debug ("Home key");
$cursorPos = $minLength; $cursorPos = $minLength;
$this->moveCursor ($cursorPos); $this->moveCursor ($cursorPos);
} }
@@ -546,6 +567,7 @@ class console
// Remove the char under the cursor (Delete) // Remove the char under the cursor (Delete)
// {{{ // {{{
{ {
$this->debug ("Delete key");
if ($cursorPos > mb_strlen ($prompt.$string)) if ($cursorPos > mb_strlen ($prompt.$string))
continue; continue;
$strArr = $this->mb_str_split ($string); $strArr = $this->mb_str_split ($string);
@@ -560,12 +582,14 @@ class console
// Non writeable char : skip it // Non writeable char : skip it
// {{{ // {{{
{ {
$this->debug ("Non writeable char : ".ord ($char));
} }
// }}} // }}}
else else
// Normal char : Add it to the string // Normal char : Add it to the string
// {{{ // {{{
{ {
$this->debug ("Normal char : ".ord ($char));
$strArr = $this->mb_str_split ($string); $strArr = $this->mb_str_split ($string);
$firstArr = array_slice ($strArr, 0, $cursorPos - $minLength); $firstArr = array_slice ($strArr, 0, $cursorPos - $minLength);
$lastArr = array_slice ($strArr, $cursorPos - $minLength); $lastArr = array_slice ($strArr, $cursorPos - $minLength);
@@ -578,6 +602,7 @@ class console
} }
// }}} // }}}
} }
$this->debug ("End of readline '$string'");
return $string; return $string;
} }
// }}} // }}}
@@ -589,6 +614,7 @@ class console
private function rewriteLine ($text) private function rewriteLine ($text)
// {{{ // {{{
{ {
$this->debug ("Call rewriteLine ($text)");
if ($this->echoMode) if ($this->echoMode)
{ {
$this->clearLine (); $this->clearLine ();
@@ -605,6 +631,7 @@ class console
private function moveCursor ($cursorPos) private function moveCursor ($cursorPos)
// {{{ // {{{
{ {
$this->debug ("Call moveCursor ($cursorPos)");
if ($cursorPos < 1) if ($cursorPos < 1)
$this->consoleException ("MoveCursor lesser than one : $cursorPos"); $this->consoleException ("MoveCursor lesser than one : $cursorPos");
if ($this->echoMode) if ($this->echoMode)
@@ -635,22 +662,32 @@ class console
public function clearLine () public function clearLine ()
// {{{ // {{{
{ {
$this->debug ("Call clearLine");
$oldLength = mb_strlen ($this->lineContent); $oldLength = mb_strlen ($this->lineContent);
// 1. Calculate on which line the cursor is positionned // 1. Calculate on which line the cursor is positionned
$cursorLine = 1 + floor ((-1+$this->cursorPos) / $this->termWidth); $cursorLine = 1 + floor ((-1+$this->cursorPos) / $this->termWidth);
$lastLines = 1 + floor ((1+$oldLength) / $this->termWidth); $lastLines = 1 + floor ((1+$oldLength) / $this->termWidth);
$this->debug ("==> clearLine : oldLength=$oldLength, ".
"cursorLine=$cursorLine, lastLines=$lastLines");
for ($i = $cursorLine ; $i < $lastLines ; $i++) for ($i = $cursorLine ; $i < $lastLines ; $i++)
{
$this->debug ("==> clearLine : go Down (i=$i<$lastLines)");
echo "\033[1B"; echo "\033[1B";
}
// 3. Remove the lines from lastLines to line 1 // 3. Remove the lines from lastLines to line 1
if ($lastLines > 1) if ($lastLines > 1)
{ {
for ($i = $lastLines ; $i > 1 ; $i--) for ($i = $lastLines ; $i > 1 ; $i--)
{
$this->debug ("==> clearLine : Remove line up (i=$i<$lastLines)");
echo "\r\033[K\033[1A\r"; echo "\r\033[K\033[1A\r";
}
} }
// 4. Clean the line 1 // 4. Clean the line 1
$this->debug ("==> clearLine : Remove line 1");
echo "\r\033[K"; echo "\r\033[K";
$this->lineContent = ""; $this->lineContent = "";
$this->cursorPos = 0; $this->cursorPos = 1;
} }
// }}} // }}}
@@ -831,9 +868,11 @@ class console
private function debug ($data) private function debug ($data)
// {{{ // {{{
{ {
if ($this->debug === false)
return;
if (is_array ($data) || is_bool ($data)) if (is_array ($data) || is_bool ($data))
$data = var_export ($data, true); $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);
} }
// }}} // }}}