/** * Backspaces characters. * * @param int $n The number of characters to backspace. * * @return true on success, false on failure */ private function _backspace($n = 1) { if ($this->_buffer_position < $n) { // We can't backspace this far return false; } ob_start(); for ($i = 0; $i < $n; $i++) { if ($this->_buffer_position < mb_strlen($this->_buffer)) { $head = mb_substr($this->_buffer, 0, $this->_buffer_position); $tail = mb_substr($this->_buffer, $this->_buffer_position, mb_strlen($this->_buffer)); Terminal::backspace(); echo $tail . ' '; Terminal::left(mb_strlen($tail) + 1); // Update buffer $this->_buffer = mb_substr($head, 0, mb_strlen($head) - 1) . $tail; } else { // Just backspace one char $this->_buffer = mb_substr($this->_buffer, 0, mb_strlen($this->_buffer) - 1); Terminal::backspace(); } $this->_buffer_position--; } ob_end_flush(); return true; }