/** * Tries to return autocompletion for the current entered text. * * @param string $text The last segment of the entered text * @param integer $position The current position */ protected function autocompleter($text, $position) { $info = readline_info(); $text = substr($info['line_buffer'], 0, $info['end']); if ($info['point'] !== $info['end']) { return true; } // task name? if (false === strpos($text, ' ') || !$text) { return array_keys($this->application->getCommands()); } // options and arguments? try { $command = $this->application->findCommand(substr($text, 0, strpos($text, ' '))); } catch (\Exception $e) { return true; } $list = array('--help'); foreach ($command->getDefinition()->getOptions() as $option) { $list[] = '--' . $option->getName(); } if (method_exists($command, 'getAutocompleteValues')) { foreach ($command->getAutocompleteValues() as $value) { $list[] = $value; } } return $list; }
protected static function get_real_keyword($keyword) { $info = readline_info(); if(!is_array($info) or !isset($info['pending_input'])) return $keyword; return substr($info['line_buffer'], 0, $info['end']); }
protected static function _initCompletion() { self::$_readlineFile = sys_get_temp_dir() . '/phputils-data-history'; // init read line readline_info('readline_name', 'data'); readline_completion_function([__CLASS__, 'readlineCompletion']); is_file(self::$_readlineFile) and readline_read_history(self::$_readlineFile); }
/** * @param string $input * @param int $index * @return array|bool */ public function complete($input, $index) { $configuration = $this->configuration; if ($index == 0) { $completion = array_keys($configuration); } else { $buffer = preg_replace('/\\s+/', ' ', trim(readline_info('line_buffer'))); $tokens = explode(' ', $buffer); $completion = $this->fetchCompletion($configuration, $tokens); } return $completion; }
function info() { $config = new Configuration(); $core = array('PsySH version' => Shell::VERSION, 'PHP version' => PHP_VERSION, 'default includes' => $config->getDefaultIncludes(), 'require semicolons' => $config->requireSemicolons(), 'error logging level' => $config->errorLoggingLevel(), 'config file' => array('default config file' => $config->getConfigFile(), 'local config file' => $config->getLocalConfigFile(), 'PSYSH_CONFIG env' => getenv('PSYSH_CONFIG'))); if ($config->hasReadline()) { $info = readline_info(); $readline = array('readline available' => true, 'readline enabled' => $config->useReadline(), 'readline service' => get_class($config->getReadline()), 'readline library' => $info['library_version']); if ($info['readline_name'] !== '') { $readline['readline name'] = $info['readline_name']; } } else { $readline = array('readline available' => false); } $pcntl = array('pcntl available' => function_exists('pcntl_signal'), 'posix available' => function_exists('posix_getpid')); $history = array('history file' => $config->getHistoryFile(), 'history size' => $config->getHistorySize(), 'erase duplicates' => $config->getEraseDuplicates()); $docs = array('manual db file' => $config->getManualDbFile(), 'sqlite available' => true); try { if ($db = $config->getManualDb()) { if ($q = $db->query('SELECT * FROM meta;')) { $q->setFetchMode(\PDO::FETCH_KEY_PAIR); $meta = $q->fetchAll(); foreach ($meta as $key => $val) { switch ($key) { case 'built_at': $d = new \DateTime('@' . $val); $val = $d->format(\DateTime::RFC2822); break; } $key = 'db ' . str_replace('_', ' ', $key); $docs[$key] = $val; } } else { $docs['db schema'] = '0.1.0'; } } } catch (Exception\RuntimeException $e) { if ($e->getMessage() === 'SQLite PDO driver not found') { $docs['sqlite available'] = false; } else { throw $e; } } $autocomplete = array('tab completion enabled' => $config->getTabCompletion(), 'custom matchers' => array_map('get_class', $config->getTabCompletionMatchers())); return array_merge($core, compact('pcntl', 'readline', 'history', 'docs', 'autocomplete')); }
/** * @brief Proxy for autocomplete calls * @internal */ static function _rlAutoCompleteProxy($input, $index) { // Collect some information and perform the call if we have a completer // assigned. $info = readline_info(); if (self::$completer) { $matches = self::$completer->complete($input, $index, $info); } else { // Maybe return true here if no data would help from the crash? $matches = array(); } // Check the number of matches, and push a null character if empty, // in order to avoid the php segfault bug. if (count($matches) == 0) { $matches[] = chr(0); } // Return the data return $matches; }
function readline_completion_impl($string, $index) { $readline_info = readline_info(); $line = substr($readline_info['line_buffer'], 0, $readline_info['end']); $parts = Helper_Common::parseCommand($line); $candidates = array(); if (empty($parts)) { // no input yet, just return list of helper functions $candidates += array_keys($GLOBALS['HELPERS']); } else { if (isset($GLOBALS['HELPERS'][$parts[0]])) { // we actually got the helper function correctly $PARAMS = array_slice($parts, 1); $IS_COMPLETION = true; require $GLOBALS['HELPERS'][$parts[0]]; } else { // incomplete helper function... $candidates += array_keys($GLOBALS['HELPERS']); } } return $candidates; }
<?php var_dump(@readline_info()); var_dump(@readline_info(1)); var_dump(@readline_info(1, 1)); var_dump(@readline_info('line_buffer')); var_dump(@readline_info('readline_name')); var_dump(@readline_info('readline_name', 1)); var_dump(@readline_info('readline_name')); var_dump(@readline_info('attempted_completion_over', 1)); var_dump(@readline_info('attempted_completion_over'));
/** * prints a line to stdout */ function writeln($s) { if ($this->hide) { return; } if ($this->promptShown && !$this->gettingInput) { /* something will be displayed, so remove the prompt */ // see how long the text is $l = readline_info('end') + strlen($this->prompt); // remove it $this->write("\r" . str_repeat(' ', $l) . "\r"); $this->promptShown = FALSE; } $this->write("{$s}\n"); }
/** * Autocomplete variable names. * * This is used by `readline` for tab completion. * * @param string $text * * @return mixed Array possible completions for the given input, if any. */ protected function autocomplete($text) { $info = readline_info(); // $line = substr($info['line_buffer'], 0, $info['end']); // Check whether there's a command for this // $words = explode(' ', $line); // $firstWord = reset($words); // check whether this is a variable... $firstChar = substr($info['line_buffer'], max(0, $info['end'] - strlen($text) - 1), 1); if ($firstChar === '$') { return $this->getScopeVariableNames(); } }
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; }
/** * Get an internal readline variable * * @param const name e.g. RL_LIBRARY_VERSION * @return string */ public function getVar($name) { return readline_info($name); }
/** * Tries to return autocompletion for the current entered text. * * * @return bool|array A list of guessed strings or true * @codeCoverageIgnore */ private function autocompleter() { $info = readline_info(); $text = substr($info['line_buffer'], 0, $info['end']); if ($info['point'] !== $info['end']) { return true; } // task name? if (false === strpos($text, ' ') || !$text) { $commands = array_keys($this->application->all()); $commands[] = 'quit'; $commands[] = 'all'; return $commands; } // options and arguments? try { $command = $this->application->find(substr($text, 0, strpos($text, ' '))); } catch (\Exception $e) { return true; } $list = array('--help'); foreach ($command->getDefinition()->getOptions() as $option) { $opt = '--' . $option->getName(); if (!in_array($opt, $list)) { $list[] = $opt; } } return $list; }
<?php //get 3 commands from user for ($i = 0; $i < 3; $i++) { $line = readline("Command: "); readline_add_history($line); } //dump history print_r(readline_list_history()); //dump variables print_r(readline_info());
/** * 可以处理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; }
/** * a readline completion callback * * @param string $str linebuffer * @param integer $pos position in linebuffer * @return array list of possible matches */ function __shell_readline_complete($str, $pos) { $in = readline_info('line_buffer'); /** * parse the line-buffer backwards to see if we have a * - constant * - function * - variable */ $m = array(); if (preg_match('#\\$([A-Za-z0-9_]+)->#', $in, $a)) { /* check for $o->... */ $name = $a[1]; if (isset($GLOBALS[$name]) && is_object($GLOBALS[$name])) { $c = get_class_methods($GLOBALS[$name]); foreach ($c as $v) { $m[] = $v . '('; } $c = get_class_vars(get_class($GLOBALS[$name])); foreach ($c as $k => $v) { $m[] = $k; } return $m; } } else { if (preg_match('#\\$([A-Za-z0-9_]+)\\[([^\\]]+)\\]->#', $in, $a)) { /* check for $o[...]->... */ $name = $a[1]; if (isset($GLOBALS[$name]) && is_array($GLOBALS[$name]) && isset($GLOBALS[$name][$a[2]])) { $c = get_class_methods($GLOBALS[$name][$a[2]]); foreach ($c as $v) { $m[] = $v . '('; } $c = get_class_vars(get_class($GLOBALS[$name][$a[2]])); foreach ($c as $k => $v) { $m[] = $k; } return $m; } } else { if (preg_match('#([A-Za-z0-9_]+)::#', $in, $a)) { /* check for Class:: */ $name = $a[1]; if (class_exists($name, false)) { $c = get_class_methods($name); foreach ($c as $v) { $m[] = sprintf('%s::%s(', $name, $v); } $cl = new ReflectionClass($name); $c = $cl->getConstants(); foreach ($c as $k => $v) { $m[] = sprintf('%s::%s', $name, $k); } return $m; } } else { if (preg_match('#\\$([a-zA-Z]?[a-zA-Z0-9_]*)$#', $in)) { $m = array_keys($GLOBALS); return $m; } else { if (preg_match('#new #', $in)) { $c = get_declared_classes(); foreach ($c as $v) { $m[] = $v . '('; } return $m; } else { if (preg_match('#^:set #', $in)) { $m[] = 'autoload'; $m[] = 'background='; return $m; } } } } } } $f = get_defined_functions(); foreach ($f['internal'] as $v) { $m[] = $v . '('; } foreach ($f['user'] as $v) { $m[] = $v . '('; } $c = get_declared_classes(); foreach ($c as $v) { $m[] = $v . '::'; } $c = get_defined_constants(); foreach ($c as $k => $v) { $m[] = $k; } $m[] = 'foreach ('; $m[] = 'require'; # printf("%s ... %s\n", $str, $pos); return $m; }
<?php error_reporting(E_ALL & ~E_WARNING); // Changeable readline_info('readline_name', 123); // This gets converted to string var_dump(readline_info('line_buffer', 'Buffer Content') != readline_info('line_buffer')); var_dump(readline_info('readline_name')); // Not Changeable var_dump(readline_info('library_version', 'Fake') == readline_info('library_version'));
public function handleAutocomplete($part, $offset) { if (!$this->autocompleteCallback) { return; } $info = readline_info(); $line = substr($info['line_buffer'], 0, $info['end']); return call_user_func($this->autocompleteCallback, $part, $offset, $line, $info['point']); }
/** * a readline completion callback * * @param string $str linebuffer * @param integer $pos position in linebuffer * * @return array list of possible matches */ function PHP_Shell_readlineComplete($str, $pos) { $in = readline_info('line_buffer'); /** * parse the line-buffer backwards to see if we have a * - constant * - function * - variable */ $m = array(); if (preg_match('#\\$([A-Za-z0-9_]+)->#', $in, $a)) { /* check for $o->... */ $name = $a[1]; if (isset($GLOBALS[$name]) && is_object($GLOBALS[$name])) { $c = get_class_methods($GLOBALS[$name]); foreach ($c as $v) { $m[] = $v . '('; } $c = get_class_vars(get_class($GLOBALS[$name])); foreach ($c as $k => $v) { $m[] = $k; } return $m; } } else { if (preg_match('#\\$([A-Za-z0-9_]+)\\[([^\\]]+)\\]->#', $in, $a)) { /* check for $o[...]->... */ $name = $a[1]; if (isset($GLOBALS[$name]) && is_array($GLOBALS[$name]) && isset($GLOBALS[$name][$a[2]])) { $c = get_class_methods($GLOBALS[$name][$a[2]]); foreach ($c as $v) { $m[] = $v . '('; } $c = get_class_vars(get_class($GLOBALS[$name][$a[2]])); foreach ($c as $k => $v) { $m[] = $k; } return $m; } } else { if (preg_match('#([A-Za-z0-9_]+)::#', $in, $a)) { /* check for Class:: */ $name = $a[1]; if (class_exists($name, false)) { $c = get_class_methods($name); foreach ($c as $v) { $m[] = sprintf('%s::%s(', $name, $v); } $cl = new ReflectionClass($name); $c = $cl->getConstants(); foreach ($c as $k => $v) { $m[] = sprintf('%s::%s', $name, $k); } return $m; } } else { if (preg_match('#\\$([a-zA-Z]?[a-zA-Z0-9_]*)$#', $in)) { $m = array_keys($GLOBALS); return $m; } else { if (preg_match('#new #', $in)) { $c = get_declared_classes(); foreach ($c as $v) { $m[] = $v . '('; } return $m; } else { if (preg_match('#^:set #', $in)) { foreach (PHP_Shell_Options::getInstance()->getOptions() as $v) { $m[] = $v; } return $m; } } } } } } $f = get_defined_functions(); foreach ($f['internal'] as $v) { $m[] = $v . '('; } foreach ($f['user'] as $v) { $m[] = $v . '('; } $c = get_declared_classes(); foreach ($c as $v) { $m[] = $v . '::'; } $c = get_defined_constants(); foreach ($c as $k => $v) { $m[] = $k; } /* taken from http://de3.php.net/manual/en/reserved.php */ $m[] = 'abstract'; $m[] = 'and'; $m[] = 'array('; $m[] = 'as'; $m[] = 'break'; $m[] = 'case'; $m[] = 'catch'; $m[] = 'class'; $m[] = 'const'; $m[] = 'continue'; $m[] = 'default'; $m[] = 'die('; $m[] = 'do'; $m[] = 'echo('; $m[] = 'else'; $m[] = 'elseif'; $m[] = 'empty('; $m[] = 'eval('; $m[] = 'exception'; $m[] = 'extends'; $m[] = 'exit('; $m[] = 'extends'; $m[] = 'final'; $m[] = 'for ('; $m[] = 'foreach ('; $m[] = 'function'; $m[] = 'global'; $m[] = 'if'; $m[] = 'implements'; $m[] = 'include "'; $m[] = 'include_once "'; $m[] = 'interface'; $m[] = 'isset('; $m[] = 'list('; $m[] = 'new'; $m[] = 'or'; $m[] = 'print('; $m[] = 'private'; $m[] = 'protected'; $m[] = 'public'; $m[] = 'require "'; $m[] = 'require_once "'; $m[] = 'return'; $m[] = 'static'; $m[] = 'switch ('; $m[] = 'throw'; $m[] = 'try'; $m[] = 'unset('; $m[] = 'var'; $m[] = 'while'; $m[] = 'xor'; $m[] = '__FILE__'; $m[] = '__FUNCTION__'; $m[] = '__CLASS__'; $m[] = '__LINE__'; $m[] = '__METHOD__'; return $m; }
function callback_complete($input, $index) { $buffer = ""; $buffer = substr(readline_info()["line_buffer"], 0, readline_info()['end']); $cmds = explode(" ", $buffer); $c = $cmds[0]; $cmp1 = []; if (count($cmds) <= 1) { $cmp1 = $this->commands; } if (!in_array($c, $this->commands)) { $c = "cd"; $cmds = array_merge([$c], $cmds); } if (array_key_exists($c, $this->cmdobj)) { $cmp = $this->cmdobj[$c]->complete($this->path, $cmds); } else { $cmp = $this->{"complete_" . $c}($cmds); } if (!is_array($cmp)) { $cmp = []; } if (count($cmp) == 0) { $cmp1 = $this->commands; } $cmp = array_merge($cmp1, $cmp); sort($cmp); return $cmp; }
/** * @return string */ protected function getLineBuffer() : string { $info = $info = readline_info(); return substr($info['line_buffer'], 0, $info['end']); }
function complete($text, $state) { $info = readline_info(); $origline = substr($info['line_buffer'], 0, $info['end']); $line = ltrim($origline); $stripped = strlen($origline) - strlen($line); $begidx = $info['point'] - $stripped; $endidx = $info['end'] - $stripped; if ($begidx > 0) { list($cmd, ) = $this->parseline($line); if ($cmd == '') { $compfunc = 'completedefault'; } elseif (method_exists($this, 'complete_' . $cmd)) { $compfunc = 'complete_' . $cmd; } else { $compfunc = 'completedefault'; } } else { $compfunc = 'completedefault'; } return call_user_func(array($this, $compfunc), $text, $line, $begidx, $endidx) ?: null; }
public function autocomplete($name, $args2, $args3) { $res = array(); if (preg_match('#^(.*)::(.*)$#', $name, $matches)) { $methods = get_class_methods($matches[1]); foreach ($methods as $m) { if ($matches[2] === '' or strpos($m, $matches[2]) === 0) { $res[] = $matches[1] . '::' . $m . '()'; } } } elseif (preg_match('#\\$([^$]*)->([^>]*)$#', readline_info('line_buffer'), $matches)) { $obj = self::$_vars[$matches[1]]; if (!is_object($obj) or ($class = get_class($obj)) === false) { return null; } if (is_a($obj, 'Pix_Table_Row')) { // columns foreach (array_keys($obj->getTable()->_columns) as $m) { if ($matches[2] === '' or strpos($m, $matches[2]) === 0) { $res[] = $m; } } // relations foreach (array_keys($obj->getTable()->_relations) as $m) { if ($matches[2] === '' or strpos($m, $matches[2]) === 0) { $res[] = $m; } } // aliases if ($obj->getTable()->_aliases) { foreach (array_keys($obj->getTable()->_aliases) as $m) { if ($matches[2] === '' or strpos($m, $matches[2]) === 0) { $res[] = $m; } } } // hook if ($obj->getTable()->_hooks) { foreach (array_keys($obj->getTable()->_hooks) as $name) { if ($matches[2] === '' or stripos($name, $matches[2]) === 0) { $res[] = $name; } } } // Table Row Helper foreach (array_keys($obj->getTable()->getHelperManager('row')->getMethods()) as $name) { if ($matches[2] === '' or stripos($name, $matches[2]) === 0) { $res[] = $name . '()'; } } // Table Static Helper foreach (array_keys(Pix_Table::getStaticHelperManager('row')->getMethods()) as $name) { if ($matches[2] === '' or stripos($name, $matches[2]) === 0) { $res[] = $name . '()'; } } } $methods = get_class_methods($class); foreach ($methods as $m) { if ($matches[2] === '' or strpos($m, $matches[2]) === 0) { $res[] = $m . '()'; } } } else { foreach (self::findMatch($name) as $o) { $res[] = $o; } } if (count($res) == 0) { return null; } return $res; }
/** * @param string $input the last command part * @param integer $index the $index is the place of the cursor in the line * * @return array */ public function autoComplete($input = '', $index = 0) { $completions = array(); // Get info about the current buffer $info = readline_info(); // Figure out what the entire input is $fullInput = substr($info['line_buffer'], 0, $info['end']); // Figure out how many parts do we have? $inputParts = explode(' ', $fullInput); $partCount = count($inputParts); if ($partCount === 3) { if (in_array($inputParts[1], $this->completions['actions']['check']) or in_array($inputParts[1], $this->completions['actions']['migration'])) { $completions = $this->completions['extensions']; } } elseif ($partCount === 2) { switch ($inputParts[0]) { case '0': case 'check': $completions = $this->completions['actions']['check']; break; case '1': case 'migration': $completions = $this->completions['actions']['migration']; break; default: $completions = $this->completions['extensions']; } } elseif ($partCount === 1) { if (isset($this->completions['commands'])) { $completions = $this->completions['commands']; } else { $completions = $this->completions['actions']; } } return $completions; }
/** * The readline_completion_function callback handler. * * @see processCallback * * @param $input * @param $index * * @return array */ public function callback($input, $index) { return $this->processCallback($input, $index, readline_info()); }
private function autocompleter($text) { $info = readline_info(); $text = substr($info['line_buffer'], 0, $info['end']); if ($info['point'] !== $info['end']) { return true; } if (false === strpos($text, ' ') || !$text) { return array_keys($this->application->all()); } try { $command = $this->application->find(substr($text, 0, strpos($text, ' '))); } catch (\Exception $e) { return true; } $list = array('--help'); foreach ($command->getDefinition()->getOptions() as $option) { $list[] = '--' . $option->getName(); } return $list; }