Commit version 24.12.13800
This commit is contained in:
@ -86,6 +86,7 @@ class Arguments implements \ArrayAccess {
|
||||
* @param mixed $offset An Argument object or the name of the argument.
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($offset) {
|
||||
if ($offset instanceOf Argument) {
|
||||
$offset = $offset->key;
|
||||
@ -100,6 +101,7 @@ class Arguments implements \ArrayAccess {
|
||||
* @param mixed $offset An Argument object or the name of the argument.
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset) {
|
||||
if ($offset instanceOf Argument) {
|
||||
$offset = $offset->key;
|
||||
@ -116,6 +118,7 @@ class Arguments implements \ArrayAccess {
|
||||
* @param mixed $offset An Argument object or the name of the argument.
|
||||
* @param mixed $value The value to set
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($offset, $value) {
|
||||
if ($offset instanceOf Argument) {
|
||||
$offset = $offset->key;
|
||||
@ -129,6 +132,7 @@ class Arguments implements \ArrayAccess {
|
||||
*
|
||||
* @param mixed $offset An Argument object or the name of the argument.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($offset) {
|
||||
if ($offset instanceOf Argument) {
|
||||
$offset = $offset->key;
|
||||
|
@ -101,16 +101,16 @@ class Colors {
|
||||
$colors[] = 0;
|
||||
}
|
||||
|
||||
return "\033[" . join(';', $colors) . "m";
|
||||
return "\033[" . implode(';', $colors) . "m";
|
||||
}
|
||||
|
||||
/**
|
||||
* Colorize a string using helpful string formatters. If the `Streams::$out` points to a TTY coloring will be enabled,
|
||||
* otherwise disabled. You can control this check with the `$colored` parameter.
|
||||
*
|
||||
* @param string $string
|
||||
* @param string $string
|
||||
* @param boolean $colored Force enable or disable the colorized output. If left as `null` the TTY will control coloring.
|
||||
* @return string
|
||||
* @return string
|
||||
*/
|
||||
static public function colorize($string, $colored = null) {
|
||||
$passed = $string;
|
||||
@ -146,6 +146,8 @@ class Colors {
|
||||
* @return string A string with color information removed.
|
||||
*/
|
||||
static public function decolorize( $string, $keep = 0 ) {
|
||||
$string = (string) $string;
|
||||
|
||||
if ( ! ( $keep & 1 ) ) {
|
||||
// Get rid of color tokens if they exist
|
||||
$string = str_replace('%%', '%¾', $string);
|
||||
@ -182,7 +184,7 @@ class Colors {
|
||||
* Return the length of the string without color codes.
|
||||
*
|
||||
* @param string $string the string to measure
|
||||
* @return int
|
||||
* @return int
|
||||
*/
|
||||
static public function length($string) {
|
||||
return safe_strlen( self::decolorize( $string ) );
|
||||
@ -194,7 +196,7 @@ class Colors {
|
||||
* @param string $string The string to measure.
|
||||
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
|
||||
* @param string|bool $encoding Optional. The encoding of the string. Default false.
|
||||
* @return int
|
||||
* @return int
|
||||
*/
|
||||
static public function width( $string, $pre_colorized = false, $encoding = false ) {
|
||||
return strwidth( $pre_colorized || self::shouldColorize() ? self::decolorize( $string, $pre_colorized ? 1 /*keep_tokens*/ : 0 ) : $string, $encoding );
|
||||
@ -208,9 +210,11 @@ class Colors {
|
||||
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
|
||||
* @param string|bool $encoding Optional. The encoding of the string. Default false.
|
||||
* @param int $pad_type Optional. Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
|
||||
* @return string
|
||||
* @return string
|
||||
*/
|
||||
static public function pad( $string, $length, $pre_colorized = false, $encoding = false, $pad_type = STR_PAD_RIGHT ) {
|
||||
$string = (string) $string;
|
||||
|
||||
$real_length = self::width( $string, $pre_colorized, $encoding );
|
||||
$diff = strlen( $string ) - $real_length;
|
||||
$length += $diff;
|
||||
|
@ -30,6 +30,9 @@ abstract class Notify {
|
||||
protected $_message;
|
||||
protected $_start;
|
||||
protected $_timer;
|
||||
protected $_tick;
|
||||
protected $_iteration = 0;
|
||||
protected $_speed = 0;
|
||||
|
||||
/**
|
||||
* Instatiates a Notification object.
|
||||
@ -92,23 +95,21 @@ abstract class Notify {
|
||||
* @return int The number of ticks performed in 1 second.
|
||||
*/
|
||||
public function speed() {
|
||||
static $tick, $iteration = 0, $speed = 0;
|
||||
|
||||
if (!$this->_start) {
|
||||
return 0;
|
||||
} else if (!$tick) {
|
||||
$tick = $this->_start;
|
||||
} else if (!$this->_tick) {
|
||||
$this->_tick = $this->_start;
|
||||
}
|
||||
|
||||
$now = microtime(true);
|
||||
$span = $now - $tick;
|
||||
$span = $now - $this->_tick;
|
||||
if ($span > 1) {
|
||||
$iteration++;
|
||||
$tick = $now;
|
||||
$speed = ($this->_current / $iteration) / $span;
|
||||
$this->_iteration++;
|
||||
$this->_tick = $now;
|
||||
$this->_speed = ($this->_current / $this->_iteration) / $span;
|
||||
}
|
||||
|
||||
return $speed;
|
||||
return $this->_speed;
|
||||
}
|
||||
|
||||
/**
|
||||
|
78
libs/cli/README.md
Normal file
78
libs/cli/README.md
Normal file
@ -0,0 +1,78 @@
|
||||
PHP Command Line Tools
|
||||
======================
|
||||
|
||||
A collection of functions and classes to assist with command line development.
|
||||
|
||||
Requirements
|
||||
|
||||
* PHP >= 5.6
|
||||
|
||||
Suggested PHP extensions
|
||||
|
||||
* mbstring - Used for calculating string widths.
|
||||
|
||||
Function List
|
||||
-------------
|
||||
|
||||
* `cli\out($msg, ...)`
|
||||
* `cli\out_padded($msg, ...)`
|
||||
* `cli\err($msg, ...)`
|
||||
* `cli\line($msg = '', ...)`
|
||||
* `cli\input()`
|
||||
* `cli\prompt($question, $default = false, $marker = ':')`
|
||||
* `cli\choose($question, $choices = 'yn', $default = 'n')`
|
||||
* `cli\menu($items, $default = false, $title = 'Choose an Item')`
|
||||
|
||||
Progress Indicators
|
||||
-------------------
|
||||
|
||||
* `cli\notify\Dots($msg, $dots = 3, $interval = 100)`
|
||||
* `cli\notify\Spinner($msg, $interval = 100)`
|
||||
* `cli\progress\Bar($msg, $total, $interval = 100)`
|
||||
|
||||
Tabular Display
|
||||
---------------
|
||||
|
||||
* `cli\Table::__construct(array $headers = null, array $rows = null)`
|
||||
* `cli\Table::setHeaders(array $headers)`
|
||||
* `cli\Table::setRows(array $rows)`
|
||||
* `cli\Table::setRenderer(cli\table\Renderer $renderer)`
|
||||
* `cli\Table::addRow(array $row)`
|
||||
* `cli\Table::sort($column)`
|
||||
* `cli\Table::display()`
|
||||
|
||||
The display function will detect if output is piped and, if it is, render a tab delimited table instead of the ASCII
|
||||
table rendered for visual display.
|
||||
|
||||
You can also explicitly set the renderer used by calling `cli\Table::setRenderer()` and giving it an instance of one
|
||||
of the concrete `cli\table\Renderer` classes.
|
||||
|
||||
Tree Display
|
||||
------------
|
||||
|
||||
* `cli\Tree::__construct()`
|
||||
* `cli\Tree::setData(array $data)`
|
||||
* `cli\Tree::setRenderer(cli\tree\Renderer $renderer)`
|
||||
* `cli\Tree::render()`
|
||||
* `cli\Tree::display()`
|
||||
|
||||
Argument Parser
|
||||
---------------
|
||||
|
||||
Argument parsing uses a simple framework for taking a list of command line arguments,
|
||||
usually straight from `$_SERVER['argv']`, and parses the input against a set of
|
||||
defined rules.
|
||||
|
||||
Check `examples/arguments.php` for an example.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
See `examples/` directory for examples.
|
||||
|
||||
|
||||
Todo
|
||||
----
|
||||
|
||||
* Expand this README
|
||||
* Add doc blocks to rest of code
|
@ -15,9 +15,9 @@ class Streams {
|
||||
|
||||
static public function isTty() {
|
||||
if ( function_exists('stream_isatty') ) {
|
||||
return !stream_isatty(static::$out);
|
||||
return stream_isatty(static::$out);
|
||||
} else {
|
||||
return (function_exists('posix_isatty') && !posix_isatty(static::$out));
|
||||
return (function_exists('posix_isatty') && posix_isatty(static::$out));
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,7 +160,7 @@ class Streams {
|
||||
* @return string The users input.
|
||||
* @see cli\input()
|
||||
*/
|
||||
public static function prompt( $question, $default = null, $marker = ': ', $hide = false ) {
|
||||
public static function prompt( $question, $default = false, $marker = ': ', $hide = false ) {
|
||||
if( $default && strpos( $question, '[' ) === false ) {
|
||||
$question .= ' [' . $default . ']';
|
||||
}
|
||||
|
@ -44,11 +44,11 @@ class Table {
|
||||
* @param array $rows The rows of data for this table. Optional.
|
||||
* @param array $footers Footers used in this table. Optional.
|
||||
*/
|
||||
public function __construct(array $headers = null, array $rows = null, array $footers = null) {
|
||||
public function __construct(array $headers = array(), array $rows = array(), array $footers = array()) {
|
||||
if (!empty($headers)) {
|
||||
// If all the rows is given in $headers we use the keys from the
|
||||
// first row for the header values
|
||||
if ($rows === null) {
|
||||
if ($rows === array()) {
|
||||
$rows = $headers;
|
||||
$keys = array_keys(array_shift($headers));
|
||||
$headers = array();
|
||||
|
@ -77,7 +77,7 @@ class Argument extends Memoize {
|
||||
* @return bool
|
||||
*/
|
||||
public function isLong() {
|
||||
return (0 == strncmp($this->_raw, '--', 2));
|
||||
return (0 == strncmp((string)$this->_raw, '--', 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,7 +86,7 @@ class Argument extends Memoize {
|
||||
* @return bool
|
||||
*/
|
||||
public function isShort() {
|
||||
return !$this->isLong && (0 == strncmp($this->_raw, '-', 1));
|
||||
return !$this->isLong && (0 == strncmp((string)$this->_raw, '-', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,9 +19,9 @@ use cli\Arguments;
|
||||
*/
|
||||
class HelpScreen {
|
||||
protected $_flags = array();
|
||||
protected $_maxFlag = 0;
|
||||
protected $_flagMax = 0;
|
||||
protected $_options = array();
|
||||
protected $_maxOption = 0;
|
||||
protected $_optionMax = 0;
|
||||
|
||||
public function __construct(Arguments $arguments) {
|
||||
$this->setArguments($arguments);
|
||||
|
@ -15,6 +15,7 @@ namespace cli\arguments;
|
||||
use cli\Memoize;
|
||||
|
||||
class Lexer extends Memoize implements \Iterator {
|
||||
private $_item;
|
||||
private $_items = array();
|
||||
private $_index = 0;
|
||||
private $_length = 0;
|
||||
@ -33,6 +34,7 @@ class Lexer extends Memoize implements \Iterator {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function current() {
|
||||
return $this->_item;
|
||||
}
|
||||
@ -49,6 +51,7 @@ class Lexer extends Memoize implements \Iterator {
|
||||
/**
|
||||
* Move the cursor forward 1 element if it is valid.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function next() {
|
||||
if ($this->valid()) {
|
||||
$this->_shift();
|
||||
@ -60,6 +63,7 @@ class Lexer extends Memoize implements \Iterator {
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function key() {
|
||||
return $this->_index;
|
||||
}
|
||||
@ -68,6 +72,7 @@ class Lexer extends Memoize implements \Iterator {
|
||||
* Move forward 1 element and, if the method hasn't been called before, reset
|
||||
* the cursor's position to 0.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function rewind() {
|
||||
$this->_shift();
|
||||
if ($this->_first) {
|
||||
@ -81,6 +86,7 @@ class Lexer extends Memoize implements \Iterator {
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function valid() {
|
||||
return ($this->_index < $this->_length);
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ function input( $format = null ) {
|
||||
* continue displaying until input is received.
|
||||
*
|
||||
* @param string $question The question to ask the user.
|
||||
* @param string $default A default value if the user provides no input.
|
||||
* @param string|false $default A default value if the user provides no input. Default false.
|
||||
* @param string $marker A string to append to the question and default value on display.
|
||||
* @param boolean $hide If the user input should be hidden
|
||||
* @return string The users input.
|
||||
@ -289,7 +289,7 @@ function _safe_substr_eaw( $str, $length ) {
|
||||
if ( $width < 0 && $length > 1 ) {
|
||||
$length--;
|
||||
}
|
||||
return join( '', array_slice( $chars, 0, $length ) );
|
||||
return implode('', array_slice($chars, 0, $length ) );
|
||||
}
|
||||
}
|
||||
return $str;
|
||||
@ -319,6 +319,8 @@ function safe_str_pad( $string, $length, $encoding = false ) {
|
||||
* @return int The string's width.
|
||||
*/
|
||||
function strwidth( $string, $encoding = false ) {
|
||||
$string = (string) $string;
|
||||
|
||||
// Set the East Asian Width and Mark regexs.
|
||||
list( $eaw_regex, $m_regex ) = get_unicode_regexs();
|
||||
|
||||
|
@ -137,7 +137,7 @@ class Ascii extends Renderer {
|
||||
$extra_rows = array_fill( 0, count( $row ), array() );
|
||||
|
||||
foreach( $row as $col => $value ) {
|
||||
|
||||
$value = $value ?: '';
|
||||
$value = str_replace( array( "\r\n", "\n" ), ' ', $value );
|
||||
|
||||
$col_width = $this->_widths[ $col ];
|
||||
@ -180,7 +180,7 @@ class Ascii extends Renderer {
|
||||
$row_values = array();
|
||||
$has_more = false;
|
||||
foreach( $extra_rows as $col => &$col_values ) {
|
||||
$row_values[ $col ] = array_shift( $col_values );
|
||||
$row_values[ $col ] = ! empty( $col_values ) ? array_shift( $col_values ) : '';
|
||||
if ( count( $col_values ) ) {
|
||||
$has_more = true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user