diff --git a/console.php b/console.php index d14e4a1..2699a25 100644 --- a/console.php +++ b/console.php @@ -565,21 +565,7 @@ private $usleep = 0; { if ($this->echoMode) { - // Based on https://stackoverflow.com/a/27850902/158716 - $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); - for ($i = $cursorLine ; $i < $lastLines ; $i++) - echo "\033[1B"; - // 3. Remove the lines from lastLines to line 1 - if ($lastLines > 1) - { - for ($i = $lastLines ; $i > 1 ; $i--) - echo "\r\033[K\033[1A\r"; - } - // 4. Clean the line 1 - echo "\r\033[K"; + $this->clearLine (); $this->lineContent = $text; echo $this->lineContent; $this->cursorPos = mb_strlen ($this->lineContent); @@ -623,6 +609,19 @@ private $usleep = 0; public function 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); + for ($i = $cursorLine ; $i < $lastLines ; $i++) + echo "\033[1B"; + // 3. Remove the lines from lastLines to line 1 + if ($lastLines > 1) + { + for ($i = $lastLines ; $i > 1 ; $i--) + echo "\r\033[K\033[1A\r"; + } + // 4. Clean the line 1 echo "\r\033[K"; $this->lineContent = ""; $this->cursorPos = 0;