annotate vendor/psy/psysh/src/Readline/Libedit.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Readline;
Chris@0 13
Chris@0 14 use Psy\Util\Str;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * A Libedit-based Readline implementation.
Chris@0 18 *
Chris@0 19 * This is largely the same as the Readline implementation, but it emulates
Chris@0 20 * support for `readline_list_history` since PHP decided it was a good idea to
Chris@0 21 * ship a fake Readline implementation that is missing history support.
Chris@0 22 */
Chris@0 23 class Libedit extends GNUReadline
Chris@0 24 {
Chris@0 25 /**
Chris@0 26 * Let's emulate GNU Readline by manually reading and parsing the history file!
Chris@0 27 *
Chris@0 28 * @return bool
Chris@0 29 */
Chris@0 30 public static function isSupported()
Chris@0 31 {
Chris@4 32 return \function_exists('readline') && !\function_exists('readline_list_history');
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function listHistory()
Chris@0 39 {
Chris@4 40 $history = \file_get_contents($this->historyFile);
Chris@0 41 if (!$history) {
Chris@0 42 return [];
Chris@0 43 }
Chris@0 44
Chris@0 45 // libedit doesn't seem to support non-unix line separators.
Chris@4 46 $history = \explode("\n", $history);
Chris@0 47
Chris@0 48 // shift the history signature, ensure it's valid
Chris@4 49 if (\array_shift($history) !== '_HiStOrY_V2_') {
Chris@0 50 return [];
Chris@0 51 }
Chris@0 52
Chris@0 53 // decode the line
Chris@4 54 $history = \array_map([$this, 'parseHistoryLine'], $history);
Chris@0 55 // filter empty lines & comments
Chris@4 56 return \array_values(\array_filter($history));
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * From GNUReadline (readline/histfile.c & readline/histexpand.c):
Chris@0 61 * lines starting with "\0" are comments or timestamps;
Chris@0 62 * if "\0" is found in an entry,
Chris@0 63 * everything from it until the next line is a comment.
Chris@0 64 *
Chris@0 65 * @param string $line The history line to parse
Chris@0 66 *
Chris@0 67 * @return string | null
Chris@0 68 */
Chris@0 69 protected function parseHistoryLine($line)
Chris@0 70 {
Chris@0 71 // empty line, comment or timestamp
Chris@0 72 if (!$line || $line[0] === "\0") {
Chris@0 73 return;
Chris@0 74 }
Chris@0 75 // if "\0" is found in an entry, then
Chris@0 76 // everything from it until the end of line is a comment.
Chris@4 77 if (($pos = \strpos($line, "\0")) !== false) {
Chris@4 78 $line = \substr($line, 0, $pos);
Chris@0 79 }
Chris@0 80
Chris@0 81 return ($line !== '') ? Str::unvis($line) : null;
Chris@0 82 }
Chris@0 83 }