Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Explicit use statements and Streams instead of cli\funcs
  • Loading branch information
jlogsdon authored and Ryan committed Dec 17, 2013
1 parent 5d54ae4 commit 69e4c73
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 35 deletions.
25 changes: 15 additions & 10 deletions lib/cli/Arguments.php
Expand Up @@ -12,6 +12,11 @@

namespace cli;

use cli\arguments\Argument;
use cli\arguments\HelpScreen;
use cli\arguments\InvalidArguments;
use cli\arguments\Lexer;

/**
* Parses command line arguments.
*/
Expand Down Expand Up @@ -63,7 +68,7 @@ public function getArguments() {
}

public function getHelpScreen() {
return new arguments\HelpScreen($this);
return new HelpScreen($this);
}

/**
Expand All @@ -82,7 +87,7 @@ public function asJSON() {
* @return bool
*/
public function offsetExists($offset) {
if ($offset instanceOf arguments\Argument) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}

Expand All @@ -96,7 +101,7 @@ public function offsetExists($offset) {
* @return mixed
*/
public function offsetGet($offset) {
if ($offset instanceOf arguments\Argument) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}

Expand All @@ -112,7 +117,7 @@ public function offsetGet($offset) {
* @param mixed $value The value to set
*/
public function offsetSet($offset, $value) {
if ($offset instanceOf arguments\Argument) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}

Expand All @@ -125,7 +130,7 @@ public function offsetSet($offset, $value) {
* @param mixed $offset An Argument object or the name of the argument.
*/
public function offsetUnset($offset) {
if ($offset instanceOf arguments\Argument) {
if ($offset instanceOf Argument) {
$offset = $offset->key;
}

Expand Down Expand Up @@ -274,7 +279,7 @@ public function getInvalidArguments() {
* @return array
*/
public function getFlag($flag) {
if ($flag instanceOf \cli\arguments\Argument) {
if ($flag instanceOf Argument) {
$obj = $flag;
$flag = $flag->value;
}
Expand Down Expand Up @@ -335,7 +340,7 @@ public function isStackable($flag) {
* @return array
*/
public function getOption($option) {
if ($option instanceOf arguments\Argument) {
if ($option instanceOf Argument) {
$obj = $option;
$option = $option->value;
}
Expand Down Expand Up @@ -380,12 +385,12 @@ public function isOption($argument) {
* if a long name is not given.
*
* @return array
* @throws arguments\InvalidArguments
* @throws arguments\InvalidArguments
*/
public function parse() {
$this->_invalid = array();
$this->_parsed = array();
$this->_lexer = new arguments\Lexer($this->_input);
$this->_lexer = new Lexer($this->_input);

foreach ($this->_lexer as $argument) {
if ($this->_parseFlag($argument)) {
Expand All @@ -399,7 +404,7 @@ public function parse() {
}

if ($this->_strict && !empty($this->_invalid)) {
throw new arguments\InvalidArguments($this->_invalid);
throw new InvalidArguments($this->_invalid);
}
}

Expand Down
8 changes: 5 additions & 3 deletions lib/cli/Notify.php
Expand Up @@ -12,6 +12,8 @@

namespace cli;

use cli\Streams;

/**
* The `Notify` class is the basis of all feedback classes, such as Indicators
* and Progress meters. The default behaviour is to refresh output after 100ms
Expand Down Expand Up @@ -127,9 +129,9 @@ public function formatTime($time) {
* @see cli\Notify::display()
*/
public function finish() {
\cli\out("\r");
Streams::out("\r");
$this->display(true);
\cli\line();
Streams::line();
}

/**
Expand Down Expand Up @@ -176,7 +178,7 @@ public function tick($increment = 1) {
$this->increment($increment);

if ($this->shouldUpdate()) {
\cli\out("\r");
Streams::out("\r");
$this->display();
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/cli/Shell.php
Expand Up @@ -24,10 +24,10 @@ class Shell {
* @todo Test on more systems.
*/
static public function columns() {
if ( file_exists('/usr/bin/env') ) {
return exec('/usr/bin/env tput cols');
}
return 80; // default width of cmd window on Windows OS, maybe force using MODE CON COLS=XXX?
if (stripos(PHP_OS, 'indows') === false) {
return exec('/usr/bin/env tput cols');
}
return 80; // default width of cmd window on Windows OS, maybe force using MODE CON COLS=XXX?
}

/**
Expand Down
30 changes: 18 additions & 12 deletions lib/cli/Table.php
Expand Up @@ -12,6 +12,12 @@

namespace cli;

use cli\Shell;
use cli\Streams;
use cli\table\Ascii;
use cli\table\Renderer;
use cli\table\Tabular;

/**
* The `Table` class is used to display data in a tabular format.
*/
Expand All @@ -36,7 +42,7 @@ class Table {
*
* @param array $headers Headers used in this table. Optional.
* @param array $rows The rows of data for this table. Optional.
* @param array $footers Footers used in this table. Optional.
* @param array $footers Footers used in this table. Optional.
*/
public function __construct(array $headers = null, array $rows = null, array $footers = null) {
if (!empty($headers)) {
Expand All @@ -60,10 +66,10 @@ public function __construct(array $headers = null, array $rows = null, array $fo
$this->setFooters($footers);
}

if (\cli\Shell::isPiped()) {
$this->setRenderer(new \cli\table\Tabular());
if (Shell::isPiped()) {
$this->setRenderer(new Tabular());
} else {
$this->setRenderer(new \cli\table\Ascii());
$this->setRenderer(new Ascii());
}
}

Expand All @@ -84,7 +90,7 @@ public function resetTable()
* @see table\Ascii
* @see table\Tabular
*/
public function setRenderer(table\Renderer $renderer) {
public function setRenderer(Renderer $renderer) {
$this->_renderer = $renderer;
}

Expand Down Expand Up @@ -120,25 +126,25 @@ public function display() {
$border = $this->_renderer->border();

if (isset($border)) {
\cli\line($border);
Streams::line($border);
}
\cli\line($this->_renderer->row($this->_headers));
Streams::line($this->_renderer->row($this->_headers));
if (isset($border)) {
\cli\line($border);
Streams::line($border);
}

foreach ($this->_rows as $row) {
\cli\line($this->_renderer->row($row));
Streams::line($this->_renderer->row($row));
}

if (isset($border)) {
\cli\line($border);
Streams::line($border);
}

if ($this->_footers) {
\cli\line($this->_renderer->row($this->_footers));
Streams::line($this->_renderer->row($this->_footers));
if (isset($border)) {
\cli\line($border);
Streams::line($border);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/cli/notify/Dots.php
Expand Up @@ -13,6 +13,7 @@
namespace cli\notify;

use cli\Notify;
use cli\Streams;

/**
* A Notifer that displays a string of periods.
Expand All @@ -28,7 +29,7 @@ class Dots extends Notify {
* @param string $msg The text to display next to the Notifier.
* @param int $dots The number of dots to iterate through.
* @param int $interval The interval in milliseconds between updates.
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function __construct($msg, $dots = 3, $interval = 100) {
parent::__construct($msg, $interval);
Expand Down Expand Up @@ -60,6 +61,6 @@ public function display($finish = false) {
$speed = number_format(round($this->speed()));
$elapsed = $this->formatTime($this->elapsed());

\cli\out_padded($this->_format, compact('msg', 'dots', 'speed', 'elapsed'));
Streams::out_padded($this->_format, compact('msg', 'dots', 'speed', 'elapsed'));
}
}
3 changes: 2 additions & 1 deletion lib/cli/notify/Spinner.php
Expand Up @@ -13,6 +13,7 @@
namespace cli\notify;

use cli\Notify;
use cli\Streams;

/**
* The `Spinner` Notifier displays an ASCII spinner.
Expand All @@ -39,6 +40,6 @@ public function display($finish = false) {
$speed = number_format(round($this->speed()));
$elapsed = $this->formatTime($this->elapsed());

\cli\out_padded($this->_format, compact('msg', 'char', 'elapsed', 'speed'));
Streams::out_padded($this->_format, compact('msg', 'char', 'elapsed', 'speed'));
}
}
7 changes: 4 additions & 3 deletions lib/cli/progress/Bar.php
Expand Up @@ -15,6 +15,7 @@
use cli;
use cli\Progress;
use cli\Shell;
use cli\Streams;

/**
* Displays a progress bar spanning the entire shell.
Expand Down Expand Up @@ -47,11 +48,11 @@ public function display($finish = false) {

$percent = str_pad(floor($_percent * 100), 3);;
$msg = $this->_message;
$msg = cli\render($this->_formatMessage, compact('msg', 'percent'));
$msg = Streams::render($this->_formatMessage, compact('msg', 'percent'));

$estimated = $this->formatTime($this->estimated());
$elapsed = str_pad($this->formatTime($this->elapsed()), strlen($estimated));
$timing = cli\render($this->_formatTiming, compact('elapsed', 'estimated'));
$timing = Streams::render($this->_formatTiming, compact('elapsed', 'estimated'));

$size = Shell::columns();
$size -= strlen($msg . $timing);
Expand All @@ -60,6 +61,6 @@ public function display($finish = false) {
// substr is needed to trim off the bar cap at 100%
$bar = substr(str_pad($bar, $size, ' '), 0, $size);

cli\out($this->_format, compact('msg', 'bar', 'timing'));
Streams::out($this->_format, compact('msg', 'bar', 'timing'));
}
}
1 change: 1 addition & 0 deletions lib/cli/table/Ascii.php
Expand Up @@ -11,6 +11,7 @@
*/

namespace cli\table;

use cli\Colors;

/**
Expand Down

0 comments on commit 69e4c73

Please sign in to comment.