/** * Creates instance and redirects STDOUT to temporary file */ public function setUp() { $this->_mockFile = tempnam(sys_get_temp_dir(), 'temp'); $resource = fopen($this->_mockFile, 'wb'); Streams::setStream('out', $resource); $this->_instance = new Table(); $this->_instance->setRenderer(new Ascii()); }
/** * Prints the current spinner position to `STDOUT` with the time elapsed * and tick speed. * * @param boolean $finish `true` if this was called from * `cli\Notify::finish()`, `false` otherwise. * @see cli\out_padded() * @see cli\Notify::formatTime() * @see cli\Notify::speed() */ public function display($finish = false) { $msg = $this->_message; $idx = $this->_iteration++ % strlen($this->_chars); $char = $this->_chars[$idx]; $speed = number_format(round($this->speed())); $elapsed = $this->formatTime($this->elapsed()); Streams::out_padded($this->_format, compact('msg', 'char', 'elapsed', 'speed')); }
/** * Produces a menu with the given attributes * * @param [array] $choices Menu options for the user * @param [mixed] $default Given as null option in the menu * @param [string] $text Prompt printed to STDOUT * @param [boolean] $return_value If true, returns selection. False, the index * @return [string] Either the selection, its index, or the default */ public static function menu($choices, $default = null, $text = "Select one", $return_value = false) { echo PHP_EOL; $index = \cli\Streams::menu($choices, $default, $text); if ($return_value) { return $choices[$index]; } return $index; }
/** * Same as \cli\prompt except that it allows empty input * @param string $question * @param bool $default * @param string $marker * @return string */ public static function promptAllowingEmpty($question, $default = false, $marker = ': ') { if ($default && strpos($question, '[') === false) { $question .= ' [' . $default . ']'; } while (true) { \cli\Streams::out($question . $marker); $line = \cli\Streams::input(); return $line; } }
/** * Prints the correct number of dots to `STDOUT` with the time elapsed and * tick speed. * * @param boolean $finish `true` if this was called from * `cli\Notify::finish()`, `false` otherwise. * @see cli\out_padded() * @see cli\Notify::formatTime() * @see cli\Notify::speed() */ public function display($finish = false) { $repeat = $this->_dots; if (!$finish) { $repeat = $this->_iteration++ % $repeat; } $msg = $this->_message; $dots = str_pad(str_repeat('.', $repeat), $this->_dots); $speed = number_format(round($this->speed())); $elapsed = $this->formatTime($this->elapsed()); Streams::out_padded($this->_format, compact('msg', 'dots', 'speed', 'elapsed')); }
/** * Produces a menu with the given attributes * * @param [array] $choices Menu options for the user * @param [mixed] $default Given as null option in the menu * @param [string] $text Prompt printed to STDOUT * @param [boolean] $return_value If true, returns selection. False, the index * @return [string] Either the selection, its index, or the default */ public static function menu($choices, $default = null, $text = "Select one", $return_value = false) { if (count($choices) == 1) { $only_choice = array_shift($choices); return $only_choice; } $index = \cli\Streams::menu($choices, $default, $text); if ($return_value) { return $choices[$index]; } return $index; }
/** * Prints the progress bar to the screen with percent complete, elapsed time * and estimated total time. * * @param boolean $finish `true` if this was called from * `cli\Notify::finish()`, `false` otherwise. * @see cli\out() * @see cli\Notify::formatTime() * @see cli\Notify::elapsed() * @see cli\Progress::estimated(); * @see cli\Progress::percent() * @see cli\Shell::columns() */ public function display($finish = false) { $_percent = $this->percent(); $percent = str_pad(floor($_percent * 100), 3); $msg = $this->_message; $msg = Streams::render($this->_formatMessage, compact('msg', 'percent')); $estimated = $this->formatTime($this->estimated()); $elapsed = str_pad($this->formatTime($this->elapsed()), strlen($estimated)); $timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated')); $size = Shell::columns(); $size -= strlen($msg . $timing); if ($size < 0) { $size = 0; } $bar = str_repeat($this->_bars[0], floor($_percent * $size)) . $this->_bars[1]; // substr is needed to trim off the bar cap at 100% $bar = substr(str_pad($bar, $size, ' '), 0, $size); Streams::out($this->_format, compact('msg', 'bar', 'timing')); }
/** * Produces a menu with the given attributes * * @param array $arg_options Elements as follow: * array choices Menu options for the user * mixed default Given as null option in the menu * string message Prompt printed to STDOUT * bool return_value If true, returns selection. False, the index * @return string Either the selection, its index, or the default */ public function menu(array $arg_options = []) { $default_options = ['choices' => [$this->NULL_INPUTS[0]], 'default' => null, 'message' => 'Select one', 'return_value' => false]; $options = array_merge($default_options, $arg_options); if (count($options['choices']) == 1) { $index = 0; } else { $index = \cli\Streams::menu($options['choices'], $options['default'], $options['message']); } if ($options['return_value']) { return $options['choices'][$index]; } return $index; }
/** * Output the table to `STDOUT` using `cli\line()`. * * If STDOUT is a pipe or redirected to a file, should output simple * tab-separated text. Otherwise, renders table with ASCII table borders * * @uses cli\Shell::isPiped() Determine what format to output * * @see cli\Table::renderRow() */ public function display() { foreach ($this->getDisplayLines() as $line) { Streams::line($line); } }
/** * Produces a menu with the given attributes * * @param array $arg_options Elements as follow: * bool autoselect_solo Automatically selects the only given option * array choices Menu options for the user * mixed default Given as null option in the menu * string message Prompt printed to STDOUT * bool return_value If true, returns selection. False, the index * @return string Either the selection, its index, or the default */ public function menu(array $arg_options = []) { $default_options = ['autoselect_solo' => true, 'args' => [], 'choices' => [$this->NULL_INPUTS[0]], 'default' => null, 'key' => null, 'message' => 'Select one', 'return_value' => false]; $options = array_merge($default_options, $arg_options); if (!is_null($options['key']) && isset($options['args'][$options['key']])) { return $options['args'][$options['key']]; } if (count($options['choices']) == 1 && $options['autoselect_solo']) { $indices = array_keys($options['choices']); $index = array_shift($indices); } else { $index = \cli\Streams::menu($options['choices'], $options['default'], $options['message']); } if ($options['return_value']) { return $options['choices'][$index]; } return $index; }
private static function outSolid($color, $text) { cli\Streams::line($color . $text . self::FONT_RESET); }
/** * This method is the meat of all Notifiers. First we increment the ticker * and then update the display if enough time has passed since our last tick. * * @param int $increment The amount to increment by. * @see cli\Notify::increment() * @see cli\Notify::shouldUpdate() * @see cli\Notify::display() */ public function tick($increment = 1) { $this->increment($increment); if ($this->shouldUpdate()) { Streams::out("\r"); $this->display(); } }
/** * Output the table to `STDOUT` using `cli\line()`. * * If STDOUT is a pipe or redirected to a file, should output simple * tab-separated text. Otherwise, renders table with ASCII table borders * * @uses cli\Shell::isPiped() Determine what format to output * * @see cli\Table::renderRow() */ public function display() { $this->_renderer->setWidths($this->_width); $border = $this->_renderer->border(); if (isset($border)) { Streams::line($border); } Streams::line($this->_renderer->row($this->_headers)); if (isset($border)) { Streams::line($border); } foreach ($this->_rows as $row) { Streams::line($this->_renderer->row($row)); } if (isset($border)) { Streams::line($border); } if ($this->_footers) { Streams::line($this->_renderer->row($this->_footers)); if (isset($border)) { Streams::line($border); } } }
/** * Displays an array of strings as a menu where a user can enter a number to * choose an option. The array must be a single dimension with either strings * or objects with a `__toString()` method. * * @param array $items The list of items the user can choose from. * @param string $default The index of the default item. * @param string $title The message displayed to the user when prompted. * @return string The index of the chosen item. * @see cli\line() * @see cli\input() * @see cli\err() */ public static function menu($items, $default = false, $title = 'Choose an item') { $map = array_values($items); if ($default && strpos($title, '[') === false && isset($items[$default])) { $title .= ' [' . $items[$default] . ']'; } foreach ($map as $idx => $item) { \cli\Streams::line(' %d. %s', $idx + 1, (string) $item); } \cli\Streams::line(); while (true) { fwrite(static::$out, sprintf('%s: ', $title)); $line = \cli\Streams::input(); if (is_numeric($line)) { $line--; if (isset($map[$line])) { return array_search($map[$line], $items); } if ($line < 0 || $line >= count($map)) { \cli\Streams::err('Invalid menu selection: out of range'); } } else { if (isset($default)) { return $default; } } } }
/** * Displays an array of strings as a menu where a user can enter a number to * choose an option. The array must be a single dimension with either strings * or objects with a `__toString()` method. * * @param array $items The list of items the user can choose from. * @param string $default The index of the default item. * @param string $title The message displayed to the user when prompted. * @return string The index of the chosen item. * @see cli\line() * @see cli\input() * @see cli\err() */ function menu($items, $default = false, $title = 'Choose an item') { return \cli\Streams::menu($items, $default, $title); }
private function getStreams() { $contents = $this->getStreamContents("output", \cli\Streams::getStream("out")); $errors = $this->getStreamContents("errors", \cli\Streams::getStream("err")); return compact("errors", "contents"); }
/** * Offers a menu to user and returns selection * * @param [array] $data Menu items * @param [mixed] $default Default menu selection * @param [string] $text Prompt text for menu * @param [boolean] $return_value True to return selected value, false for * list ordinal * @return [string] $data[$index] or $index */ static function menu($data, $default = null, $text = 'Select one', $return_value = false) { echo PHP_EOL; $index = \cli\Streams::menu($data, $default, $text); if ($return_value) { return $data[$index]; } return $index; }