public function run() { if ($this->readline) { readline_callback_handler_install("CS> ", [$this, "readline_callback"]); $this->logger->setConsoleCallback("readline_redisplay"); } while (!$this->shutdown) { $r = [$this->stdin]; $w = null; $e = null; if (stream_select($r, $w, $e, 0, 200000) > 0) { // PHP on Windows sucks if (feof($this->stdin)) { if (Utils::getOS() == "win") { $this->stdin = fopen("php://stdin", "r"); if (!is_resource($this->stdin)) { break; } } else { break; } } $this->readLine(); } } if ($this->readline) { $this->logger->setConsoleCallback(null); readline_callback_handler_remove(); } }
public function showPrompt() { // @codeCoverageIgnoreStart if ($this->hasReadline) { readline_callback_handler_install($this->getPrompt(), array($this, 'runCommand')); } else { $this->output->write($this->getPrompt()); } }
/** * If readline is installed, then prevent the user having to * press <return> in order to paginate. */ public function __construct() { // we could use extension_loaded but HHVM returns true and // still doesn't have this function.. if (function_exists('readline_callback_handler_install')) { readline_callback_handler_install('', function () { }); } }
function _screenBase() { readline_callback_handler_install('', function () { }); _screenBaseUpdateDimensions(); // In seconds e.g. 1.5 _configBaseSet("_screenBaseUpdateRate", 0.1); _configBaseSet("_screenBaseUpdateLast", 0); _configBaseSet("_screenBaseViewDirty", true); }
function install_stream_handler() { if (isPOSIX()) { begin_yellow(); readline_callback_handler_install("Quack> ", 'readline_callback'); end_yellow(); } else { echo "Quack> "; } }
public function resume() { if ($this->listening) { return; } if (!function_exists('readline_callback_handler_install')) { throw new BadMethodCallException('Method "readline_callback_handler_install" not available, update PHP and/or enable ext-readline'); } $this->listening = true; readline_callback_handler_install($this->prompt, array($this, 'handleLine')); $this->loop->addReadStream(STDIN, function () { readline_callback_read_char(); }); }
function getInputChar() { // Prepare Required Functions readline_callback_handler_install('', function () { }); while (true) { $r = array(STDIN); $w = NULL; $e = NULL; $n = stream_select($r, $w, $e, 0); if ($n && in_array(STDIN, $r)) { $c = stream_get_contents(STDIN, 1); return $c; } } }
/** * Starts shell */ public function run() { if ($this->running) { return; } $shell = $this; $this->loop->addReadStream(STDIN, function ($n) use($shell) { $c = stream_get_contents($n, 1); $shell->read($c); }); // allows control keys to work if (function_exists('readline_callback_handler_install')) { readline_callback_handler_install('', function () { }); } $this->running = true; $this->loop->run(); }
function install_handler() { // build prompt $path = implode($this->path, "[37m / [0m"); $c = $this->status; if ($c == 1) { $color1 = "[32m"; $sep = "#"; } else { $color1 = "[33m"; $sep = ">"; } if ($c == 0) { if (count($this->path) > 0) { $color1 = "[31m"; } else { $color1 = "[37m"; } } readline_callback_handler_install("{$color1}" . "[{$c}][0m {$path} [037m{$sep}[0m ", array($this, 'rl_callback')); }
/** * Overrides the default readline handler * * By default STDIN is read when the Enter key is pressed * we can override it by registering an empty closure as * the readline handler * * @return $this */ protected function overrideReadlineHandler() { readline_callback_handler_install('', function () { }); return $this; }
public function runREPL() { // install signal handlers if possible declare (ticks=1); if (function_exists('pcntl_signal')) { pcntl_signal(SIGINT, array($this, 'stop')); } print $this->options[self::OPT_PROMPT_HEADER]; // readline history if (function_exists('readline_read_history')) { readline_read_history($this->historyFile()); // doesn't seem to work, even though readline_list_history() shows the read items! } // install tab-complete if (function_exists('readline_completion_function')) { readline_completion_function(array($this, 'readlineCompleter')); } // run repl loop. if (function_exists('readline')) { // readline automatically re-prints the prompt after the callback runs, so the only way to prevent double-prompts is to do it this way until we figure out something better readline_callback_handler_install($this->inputPrompt, array($this, 'doCommand')); while (true) { $this->realReadline(); } } else { while (true) { $this->fakeReadline(); } } }
<?php function foo() { readline_callback_handler_remove(); } var_dump(readline_callback_handler_install('testing: ', 'foo')); var_dump(readline_callback_handler_install('testing: ', 'foobar!')); var_dump(readline_callback_handler_install('testing: '));
public static function readInput(&$fields, $singleChar = false) { // prevent default output if able if (self::$hasReadline) { readline_callback_handler_install('', function () { }); } foreach ($fields as $name => $data) { $vars = ['desc', 'isHidden', 'validPattern']; foreach ($vars as $idx => $v) { ${$v} = isset($data[$idx]) ? $data[$idx] : false; } $charBuff = ''; if ($desc) { echo "\n" . $desc . ": "; } while (true) { $r = [STDIN]; $w = $e = null; $n = stream_select($r, $w, $e, 200000); if ($n && in_array(STDIN, $r)) { $char = stream_get_contents(STDIN, 1); $keyId = ord($char); // ignore this one if ($keyId == self::CHR_TAB) { continue; } // WIN sends \r\n as sequence, ignore one if ($keyId == self::CHR_CR && self::$win) { continue; } // will not be send on WIN .. other ways of returning from setup? (besides ctrl + c) if ($keyId == self::CHR_ESC) { echo chr(self::CHR_BELL); return false; } else { if ($keyId == self::CHR_BACKSPACE) { if (!$charBuff) { continue; } $charBuff = mb_substr($charBuff, 0, -1); echo chr(self::CHR_BACK) . " " . chr(self::CHR_BACK); } else { if ($keyId == self::CHR_LF) { $fields[$name] = $charBuff; break; } else { if (!$validPattern || preg_match($validPattern, $char)) { $charBuff .= $char; if (!$isHidden && self::$hasReadline) { echo $char; } if ($singleChar && self::$hasReadline) { $fields[$name] = $charBuff; break; } } } } } } } } echo chr(self::CHR_BELL); foreach ($fields as $f) { if (strlen($f)) { return true; } } $fields = null; return true; }
/** * */ public function __construct() { stream_set_blocking(STDIN, 0); readline_callback_handler_install('', function () { }); }
/** * 可以处理IO的readline */ public function readio_readline($prompt) { readline_callback_handler_install($prompt, array($this, 'readio_handler')); $this->_inputLine = ''; $this->_inputFinished = false; while (!$this->_inputFinished) { $w = NULL; $e = NULL; $r = array(STDIN); $n = @stream_select($r, $w, $e, null); // var_dump($n, $r, $w, $e); if ($n === false) { // echo "io incrupt\n"; // exit; } else { if ($n && in_array(STDIN, $r)) { // read a character, will call the callback when a newline is entered readline_callback_read_char(); $rli = readline_info(); // print_r($rli); } } } return $this->_inputLine; }
private function myReadline() { $this->lastCommand = NULL; readline_callback_handler_install($this->prompt, array($this, 'readlineCallback')); while ($this->lastCommand === NULL) { readline_callback_read_char(); } return $this->lastCommand; }
function readline_predefined($prompt, $predefined_text = "") { global $readline, $prompt_finished; readline_callback_handler_install($prompt, 'readline_callback'); for ($i = 0; $i < strlen($predefined_text); $i++) { readline_info('pending_input', substr($predefined_text, $i, 1)); readline_callback_read_char(); } $readline = FALSE; $prompt_finished = FALSE; while (!$prompt_finished) { readline_callback_read_char(); } return $readline; }
public static function readInput(&$fields, $singleChar = false) { // prevent default output on *nix (readline doen't exist for WIN) if (!self::$win) { readline_callback_handler_install('', function () { }); } foreach ($fields as $name => $data) { $vars = ['desc', 'isHidden', 'validPattern']; foreach ($vars as $idx => $v) { ${$v} = isset($data[$idx]) ? $data[$idx] : false; } $charBuff = ''; if ($desc) { echo "\n" . $desc . ": "; } while (true) { $r = [STDIN]; $w = $e = null; $n = stream_select($r, $w, $e, 200000); if ($n && in_array(STDIN, $r)) { $char = stream_get_contents(STDIN, 1); $keyId = ord($char); if ($keyId == self::CHR_TAB) { // ignore this one continue; } if ($keyId == self::CHR_CR) { // also ignore this bastard! continue; } if ($keyId == self::CHR_ESC) { echo chr(self::CHR_BELL); return false; } else { if ($keyId == self::CHR_BACKSPACE) { if (!$charBuff) { continue; } $charBuff = substr($charBuff, 0, -1); echo chr(self::CHR_BACK) . " " . chr(self::CHR_BACK); } else { if ($keyId == self::CHR_LF) { $fields[$name] = $charBuff; break; } else { if (!$validPattern || preg_match($validPattern, $char)) { $charBuff .= $char; if (!$isHidden && !self::$win) { // see note above echo $char; } if ($singleChar && !self::$win) { $fields[$name] = $charBuff; break; } } } } } } } } echo chr(self::CHR_BELL); foreach ($fields as $f) { if (strlen($f)) { return true; } } $fields = null; return true; }
<?php readline_callback_handler_install('', function () { }); class TerminalHandler { private $textColors = array('BLACK' => "0;30", 'BLUE' => "0;34", 'BOLD' => "1", 'BROWN' => "0;33", 'CYAN' => "0;36", 'GREEN' => "0;32", 'LIGHT_BLUE' => "1;34", 'LIGHT_CYAN' => "1;36", 'LIGHT_GREEN' => "1;32", 'LIGHT_RED' => "1;31", 'MAGENTA' => "1;35", 'NORMAL' => "0", 'RED' => "0;31", 'REVERSE' => "7", 'UNDERSCORE' => "4", 'WHITE' => "1;37", 'YELLOW' => "1;33"); private $backgroundColors = array('BLACK' => '40', 'BLUE' => '44', 'CYAN' => '46', 'GREEN' => '42', 'LIGHT_GRAY' => '47', 'MAGENTA' => '45', 'NORMAL' => '0', 'RED' => '41', 'YELLOW' => '43'); private $useFullScreen = true; private $activeTextColor = 'NORMAL'; private $activeBackgroundColor = 'NORMAL'; private $activeSelectionColor = 'NORMAL'; private $activeSelectionBackgroundColor = 'LIGHT_GRAY'; private $borderCharacters = array('╔', '╗', '╝', '╚', '═', '║'); private $currentTerminalSize = array(0, 0); private $textAlign = 'left'; private function resetColorCodes() { echo "[0m"; } private function activateColorCodes() { if ($this->activeTextColor !== 'NORMAL') { echo "[" . $this->textColors[$this->activeTextColor] . 'm'; } if ($this->activeBackgroundColor !== 'NORMAL') { echo "[" . $this->backgroundColors[$this->activeBackgroundColor] . 'm'; } } private function getTerminalSize() {
/** * call-back for readline */ function gotInput($in) { $in = trim($in); if ($in == '') { return; } $this->gettingInput = TRUE; $this->cmd->callCommand($in); if ($GLOBALS['DIE']) { readline_callback_handler_remove(); return; } readline_add_history($in); readline_callback_handler_install($this->prompt, array($this, "gotInput")); $this->gettingInput = FALSE; }