From 46a63f57f94c282fad8bc913473c27f79c22e023 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 13 Jun 2018 14:26:01 +0000 Subject: [PATCH] console : clear line correctely with Ctrl+C git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4243 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- console.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/console.php b/console.php index 3bc7d62..ab057bc 100644 --- a/console.php +++ b/console.php @@ -12,7 +12,13 @@ * 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"); + * exec ("stty intr ^J"); + * To update the window size when the terminal is resized, use after console + * instanciation : + * declare(ticks = 1); + * pcntl_signal (SIGWINCH, function () use ($console) { + * $console->updateTerminalSize (); + * }); */ class console { @@ -135,6 +141,7 @@ private $usleep = 0; public function echo ($message) // {{{ { +$this->debug ("'$message'"); echo $message; $this->lineContent .= $message; } @@ -340,7 +347,7 @@ private $usleep = 0; { $this->clearLine (); $this->lineContent = ""; - $this->echo ($prompt."\n"); + echo "\r$prompt\n"; return ""; } // }}} @@ -617,8 +624,9 @@ private $usleep = 0; public function clearLine () // {{{ { - echo "\r".str_repeat (" ", mb_strlen ($this->lineContent))."\r"; + echo "\r\033[K"; $this->lineContent = ""; + $this->cursorPos = 0; } // }}}