public function handleKey() { ncurses_getyx(STDSCR, $cury, $curx); // Read one character $c = ncurses_getch(); if ($c == NCURSES_KEY_RETURN) { $in = trim($this->getInput()); // Split input to command and arguments $command = explode(' ', $in, 2); $command[0] = strtolower($command[0]); // Empty command - user just pressed enter if (strlen($command[0]) == 0) { return; } // Command has been given, so: // Add to history, clear the prompt, disable scrollback array_push($this->history, $in); $this->drawPrompt(); $this->historyScrollback = -1; // Add to output so we can see what we are doing $this->addOutput('> ' . $in); // Command not found if (!array_key_exists($command[0], $this->commands)) { $this->addOutput('Unknown command: ' . $command[0]); return; } // Run the command $this->commands[$command[0]]($this, $command[1]); } else { if ($c == NCURSES_KEY_LEFT) { // Move the cursor left ncurses_move($cury, $curx - 1); } else { if ($c == NCURSES_KEY_RIGHT) { // Move the cursor right ncurses_move($cury, $curx + 1); } else { if ($c == NCURSES_KEY_UP) { // If the user is not browsing history, start with the last element + 1 if ($this->historyScrollback == -1) { // There is no history - we can't open it if (count($this->history) == 0) { return; } $this->historyScrollback = count($this->history); } // Move to a previous element $this->historyScrollback--; // The user scrolled beyond the list - move to the first item if ($this->historyScrollback < 0) { $this->historyScrollback = 0; } $this->drawPrompt($this->history[$this->historyScrollback]); } else { if ($c == NCURSES_KEY_DOWN) { // User is not browsing the history - we can't go lower if ($this->historyScrollback == -1) { return; } $this->historyScrollback++; // Disable scrollback when the user went outside the list if ($this->historyScrollback >= count($this->history)) { $this->historyScrollback = -1; $this->drawPrompt(); return; } $this->drawPrompt($this->history[$this->historyScrollback]); } else { if ($c == NCURSES_KEY_BACKSPACE) { // Remove one character to the left of the cursor ncurses_mvdelch($cury, $curx - 1); } else { if ($c == NCURSES_KEY_DC) { // Remove one character at the cursor ncurses_delch($c); } else { if (ctype_print($c)) { // Just print out an ordinary character ncurses_insch($c); ncurses_move($cury, $curx + 1); } } } } } } } } $this->checkCursorBounds(); ncurses_refresh(); }
/** * @param $char * @return $this */ public function insertChar($char) { ncurses_insch($char); return $this; }