update inbox list

This commit is contained in:
manhlab
2021-04-07 19:25:18 -04:00
parent fda7245f7c
commit 436de2efd6
8576 changed files with 1013325 additions and 3 deletions

View File

@@ -0,0 +1,175 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2020 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Readline;
/**
* A Readline interface implementation for GNU Readline.
*
* This is by far the coolest way to do it, but it doesn't work with new PHP.
*
* Oh well.
*/
class GNUReadline implements Readline
{
/** @var string|false */
protected $historyFile;
/** @var int */
protected $historySize;
/** @var bool */
protected $eraseDups;
/**
* GNU Readline is supported iff `readline_list_history` is defined. PHP
* decided it would be awesome to swap out GNU Readline for Libedit, but
* they ended up shipping an incomplete implementation. So we've got this.
*
* @return bool
*/
public static function isSupported()
{
return \function_exists('readline_list_history');
}
/**
* GNU Readline constructor.
*
* @param string|false $historyFile
* @param int $historySize
* @param bool $eraseDups
*/
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
{
$this->historyFile = ($historyFile !== null) ? $historyFile : false;
$this->historySize = $historySize;
$this->eraseDups = $eraseDups;
// HHVM errors on this, so HHVM doesn't get a readline_name.
if (!\defined('HHVM_VERSION')) {
\readline_info('readline_name', 'psysh');
}
}
/**
* {@inheritdoc}
*/
public function addHistory($line)
{
if ($res = \readline_add_history($line)) {
$this->writeHistory();
}
return $res;
}
/**
* {@inheritdoc}
*/
public function clearHistory()
{
if ($res = \readline_clear_history()) {
$this->writeHistory();
}
return $res;
}
/**
* {@inheritdoc}
*/
public function listHistory()
{
return readline_list_history();
}
/**
* {@inheritdoc}
*/
public function readHistory()
{
// Workaround PHP bug #69054
//
// If open_basedir is set, readline_read_history() segfaults. This was fixed in 5.6.7:
//
// https://github.com/php/php-src/blob/423a057023ef3c00d2ffc16a6b43ba01d0f71796/NEWS#L19-L21
//
if (\version_compare(PHP_VERSION, '5.6.7', '>=') || !\ini_get('open_basedir')) {
\readline_read_history();
}
\readline_clear_history();
return \readline_read_history($this->historyFile);
}
/**
* {@inheritdoc}
*/
public function readline($prompt = null)
{
return \readline($prompt);
}
/**
* {@inheritdoc}
*/
public function redisplay()
{
\readline_redisplay();
}
/**
* {@inheritdoc}
*/
public function writeHistory()
{
// We have to write history first, since it is used
// by Libedit to list history
if ($this->historyFile !== false) {
$res = \readline_write_history($this->historyFile);
} else {
$res = true;
}
if (!$res || !$this->eraseDups && !$this->historySize > 0) {
return $res;
}
$hist = $this->listHistory();
if (!$hist) {
return true;
}
if ($this->eraseDups) {
// flip-flip technique: removes duplicates, latest entries win.
$hist = \array_flip(\array_flip($hist));
// sort on keys to get the order back
\ksort($hist);
}
if ($this->historySize > 0) {
$histsize = \count($hist);
if ($histsize > $this->historySize) {
$hist = \array_slice($hist, $histsize - $this->historySize);
}
}
\readline_clear_history();
foreach ($hist as $line) {
\readline_add_history($line);
}
if ($this->historyFile !== false) {
return \readline_write_history($this->historyFile);
}
return true;
}
}

View File

@@ -0,0 +1,121 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2020 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Readline;
use Hoa\Console\Console;
use Hoa\Console\Cursor;
use Hoa\Console\Readline\Readline as HoaReadline;
use Psy\Exception\BreakException;
/**
* Hoa\Console Readline implementation.
*/
class HoaConsole implements Readline
{
/** @var HoaReadline */
private $hoaReadline;
/** @var string|null */
private $lastPrompt;
/**
* @return bool
*/
public static function isSupported()
{
return \class_exists(Console::class, true);
}
public function __construct()
{
$this->hoaReadline = new HoaReadline();
$this->hoaReadline->addMapping('\C-l', function () {
$this->redisplay();
return HoaReadline::STATE_NO_ECHO;
});
}
/**
* {@inheritdoc}
*/
public function addHistory($line)
{
$this->hoaReadline->addHistory($line);
return true;
}
/**
* {@inheritdoc}
*/
public function clearHistory()
{
$this->hoaReadline->clearHistory();
return true;
}
/**
* {@inheritdoc}
*/
public function listHistory()
{
$i = 0;
$list = [];
while (($item = $this->hoaReadline->getHistory($i++)) !== null) {
$list[] = $item;
}
return $list;
}
/**
* {@inheritdoc}
*/
public function readHistory()
{
return true;
}
/**
* {@inheritdoc}
*
* @throws BreakException if user hits Ctrl+D
*
* @return string
*/
public function readline($prompt = null)
{
$this->lastPrompt = $prompt;
return $this->hoaReadline->readLine($prompt);
}
/**
* {@inheritdoc}
*/
public function redisplay()
{
$currentLine = $this->hoaReadline->getLine();
Cursor::clear('all');
echo $this->lastPrompt, $currentLine;
}
/**
* {@inheritdoc}
*/
public function writeHistory()
{
return true;
}
}

View File

@@ -0,0 +1,107 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2020 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Readline;
use Psy\Util\Str;
/**
* A Libedit-based Readline implementation.
*
* This is largely the same as the Readline implementation, but it emulates
* support for `readline_list_history` since PHP decided it was a good idea to
* ship a fake Readline implementation that is missing history support.
*/
class Libedit extends GNUReadline
{
private $hasWarnedOwnership = false;
/**
* Let's emulate GNU Readline by manually reading and parsing the history file!
*
* @return bool
*/
public static function isSupported()
{
return \function_exists('readline') && !\function_exists('readline_list_history');
}
/**
* {@inheritdoc}
*/
public function listHistory()
{
$history = \file_get_contents($this->historyFile);
if (!$history) {
return [];
}
// libedit doesn't seem to support non-unix line separators.
$history = \explode("\n", $history);
// shift the history signature, ensure it's valid
if (\array_shift($history) !== '_HiStOrY_V2_') {
return [];
}
// decode the line
$history = \array_map([$this, 'parseHistoryLine'], $history);
// filter empty lines & comments
return \array_values(\array_filter($history));
}
/**
* {@inheritdoc}
*/
public function writeHistory()
{
$res = parent::writeHistory();
// Libedit apparently refuses to save history if the history file is not
// owned by the user, even if it is writable. Warn when this happens.
//
// See https://github.com/bobthecow/psysh/issues/552
if ($res === false && !$this->hasWarnedOwnership) {
if (\is_file($this->historyFile) && \is_writable($this->historyFile)) {
$this->hasWarnedOwnership = true;
$msg = \sprintf('Error writing history file, check file ownership: %s', $this->historyFile);
\trigger_error($msg, E_USER_NOTICE);
}
}
return $res;
}
/**
* From GNUReadline (readline/histfile.c & readline/histexpand.c):
* lines starting with "\0" are comments or timestamps;
* if "\0" is found in an entry,
* everything from it until the next line is a comment.
*
* @param string $line The history line to parse
*
* @return string | null
*/
protected function parseHistoryLine($line)
{
// empty line, comment or timestamp
if (!$line || $line[0] === "\0") {
return;
}
// if "\0" is found in an entry, then
// everything from it until the end of line is a comment.
if (($pos = \strpos($line, "\0")) !== false) {
$line = \substr($line, 0, $pos);
}
return ($line !== '') ? Str::unvis($line) : null;
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2020 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Readline;
/**
* An interface abstracting the various readline_* functions.
*/
interface Readline
{
/**
* Check whether this Readline class is supported by the current system.
*
* @return bool
*/
public static function isSupported();
/**
* Add a line to the command history.
*
* @param string $line
*
* @return bool Success
*/
public function addHistory($line);
/**
* Clear the command history.
*
* @return bool Success
*/
public function clearHistory();
/**
* List the command history.
*
* @return array
*/
public function listHistory();
/**
* Read the command history.
*
* @return bool Success
*/
public function readHistory();
/**
* Read a single line of input from the user.
*
* @param string|null $prompt
*
* @return false|string
*/
public function readline($prompt = null);
/**
* Redraw readline to redraw the display.
*/
public function redisplay();
/**
* Write the command history to a file.
*
* @return bool Success
*/
public function writeHistory();
}

View File

@@ -0,0 +1,147 @@
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2020 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Readline;
use Psy\Exception\BreakException;
/**
* An array-based Readline emulation implementation.
*/
class Transient implements Readline
{
private $history;
private $historySize;
private $eraseDups;
private $stdin;
/**
* Transient Readline is always supported.
*
* {@inheritdoc}
*/
public static function isSupported()
{
return true;
}
/**
* Transient Readline constructor.
*/
public function __construct($historyFile = null, $historySize = 0, $eraseDups = false)
{
// don't do anything with the history file...
$this->history = [];
$this->historySize = $historySize;
$this->eraseDups = $eraseDups;
}
/**
* {@inheritdoc}
*/
public function addHistory($line)
{
if ($this->eraseDups) {
if (($key = \array_search($line, $this->history)) !== false) {
unset($this->history[$key]);
}
}
$this->history[] = $line;
if ($this->historySize > 0) {
$histsize = \count($this->history);
if ($histsize > $this->historySize) {
$this->history = \array_slice($this->history, $histsize - $this->historySize);
}
}
$this->history = \array_values($this->history);
return true;
}
/**
* {@inheritdoc}
*/
public function clearHistory()
{
$this->history = [];
return true;
}
/**
* {@inheritdoc}
*/
public function listHistory()
{
return $this->history;
}
/**
* {@inheritdoc}
*/
public function readHistory()
{
return true;
}
/**
* {@inheritdoc}
*
* @throws BreakException if user hits Ctrl+D
*
* @return string
*/
public function readline($prompt = null)
{
echo $prompt;
return \rtrim(\fgets($this->getStdin()), "\n\r");
}
/**
* {@inheritdoc}
*/
public function redisplay()
{
// noop
}
/**
* {@inheritdoc}
*/
public function writeHistory()
{
return true;
}
/**
* Get a STDIN file handle.
*
* @throws BreakException if user hits Ctrl+D
*
* @return resource
*/
private function getStdin()
{
if (!isset($this->stdin)) {
$this->stdin = \fopen('php://stdin', 'r');
}
if (\feof($this->stdin)) {
throw new BreakException('Ctrl+D');
}
return $this->stdin;
}
}