Beispiel #1
0
 /**
  * Returns a colorized string
  *
  * @param string $string Message to colorize for output
  * @return string
  */
 private function colorize($string)
 {
     $colorization_setting = $this->runner->getConfig('colorize');
     $colorize = $colorization_setting == 'auto' && !\cli\Shell::isPiped() || is_bool($colorization_setting) && $colorization_setting;
     $colorized_string = \cli\Colors::colorize($string, $colorize);
     return $colorized_string;
 }
Beispiel #2
0
 /**
  * Initializes the `Table` class.
  *
  * There are 3 ways to instantiate this class:
  *
  *  1. Pass an array of strings as the first paramater for the column headers
  *     and a 2-dimensional array as the second parameter for the data rows.
  *  2. Pass an array of hash tables (string indexes instead of numerical)
  *     where each hash table is a row and the indexes of the *first* hash
  *     table are used as the header values.
  *  3. Pass nothing and use `setHeaders()` and `addRow()` or `setRows()`.
  *
  * @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.
  */
 public function __construct(array $headers = null, array $rows = null, array $footers = null)
 {
     if (!empty($headers)) {
         // If all the rows is given in $headers we use the keys from the
         // first row for the header values
         if (empty($rows)) {
             $rows = $headers;
             $keys = array_keys(array_shift($headers));
             $headers = array();
             foreach ($keys as $header) {
                 $headers[$header] = $header;
             }
         }
         $this->setHeaders($headers);
         $this->setRows($rows);
     }
     if (!empty($footers)) {
         $this->setFooters($footers);
     }
     if (Shell::isPiped()) {
         $this->setRenderer(new Tabular());
     } else {
         $this->setRenderer(new Ascii());
     }
 }
Beispiel #3
0
 private function init_colorization()
 {
     if ($this->config['colorize'] == 'auto') {
         $this->colorize = !\cli\Shell::isPiped();
     } else {
         $this->colorize = $this->config['colorize'];
     }
 }
Beispiel #4
0
/**
 * Returns a colorized string
 *
 * @param string $string Message to colorize for output
 * @return string
 */
function colorize($string)
{
    $colorize = true;
    if (Terminus::getConfig('colorize') == 'auto') {
        $colorize = !\cli\Shell::isPiped();
    }
    $colorized_string = \cli\Colors::colorize($string, $colorize);
    return $colorized_string;
}
Beispiel #5
0
 /**
  * Initializes the `Table` class.
  *
  * There are 3 ways to instantiate this class:
  *
  *  1. Pass an array of strings as the first paramater for the column headers
  *     and a 2-dimensional array as the second parameter for the data rows.
  *  2. Pass an array of hash tables (string indexes instead of numerical)
  *     where each hash table is a row and the indexes of the *first* hash
  *     table are used as the header values.
  *  3. Pass nothing and use `setHeaders()` and `addRow()` or `setRows()`.
  *
  * @param array  $headers  Headers used in this table. Optional.
  * @param array  $rows     The rows of data for this table. Optional.
  */
 public function __construct(array $headers = null, array $rows = null)
 {
     if (!empty($headers)) {
         // If all the rows is given in $headers we use the keys from the
         // first row for the header values
         if (empty($rows)) {
             $rows = $headers;
             $keys = array_keys(array_shift($headers));
             $headers = array();
             foreach ($keys as $header) {
                 $headers[$header] = $header;
             }
         }
         $this->setHeaders($headers);
         $this->setRows($rows);
     }
     $className = '\\cli\\table' . (\cli\Shell::isPiped() ? '\\Tabular' : '\\Ascii');
     $this->setRenderer(new $className());
 }
Beispiel #6
0
/**
 * Create a progress bar to display percent completion of a given operation.
 *
 * Progress bar is written to STDOUT, and disabled when command is piped. Progress
 * advances with `$progress->tick()`, and completes with `$progress->finish()`.
 * Process bar also indicates elapsed time and expected total time.
 *
 * ```
 * # `wp user generate` ticks progress bar each time a new user is created.
 * #
 * # $ wp user generate --count=500
 * # Generating users  22 % [=======>                             ] 0:05 / 0:23
 *
 * $progress = \WP_CLI\Utils\make_progress_bar( 'Generating users', $count );
 * for ( $i = 0; $i < $count; $i++ ) {
 *     // uses wp_insert_user() to insert the user
 *     $progress->tick();
 * }
 * $progress->finish();
 * ```
 *
 * @access public
 * @category Output
 *
 * @param string  $message  Text to display before the progress bar.
 * @param integer $count    Total number of ticks to be performed.
 * @return cli\progress\Bar|WP_CLI\NoOp
 */
function make_progress_bar($message, $count)
{
    if (\cli\Shell::isPiped()) {
        return new \WP_CLI\NoOp();
    }
    return new \cli\progress\Bar($message, $count);
}
Beispiel #7
0
 private function init_colorization()
 {
     if ('auto' === $this->config['color']) {
         $this->colorize = !\cli\Shell::isPiped() && !\WP_CLI\Utils\is_windows();
     } else {
         $this->colorize = $this->config['color'];
     }
 }
Beispiel #8
0
/**
 * Creates progress bar for operation in progress
 *
 * @param [string]  $message Message to be displayed with the progress bar
 * @param [integer] $count   Number of progress dots to be displayed
 * @return [\cli\progress\Bar] $progress_bar Object which handles display of bar
 */
function make_progress_bar($message, $count)
{
    if (\cli\Shell::isPiped()) {
        return new \Terminus\NoOp();
    }
    $progress_bar = new \cli\progress\Bar($message, $count);
    return $progress_bar;
}
Beispiel #9
0
 public static function progress_bar($message, $count)
 {
     if (\cli\Shell::isPiped()) {
         return new \ETSIS_CLI\NoOp();
     }
     return new \cli\progress\Bar($message, $count);
 }
Beispiel #10
0
 public function testTables()
 {
     $this->resetStreams();
     $suffix = \cli\Shell::isPiped() ? "_piped" : "";
     $this->assertTrue(is_numeric($columns = \cli\Shell::columns()));
     $headers = array('First Name', 'Last Name', 'City', 'State');
     $data = array(array('Maryam', 'Elliott', 'Elizabeth City', 'SD'), array('Jerry', 'Washington', 'Bessemer', 'ME'), array('Allegra', 'Hopkins', 'Altoona', 'ME'), array('Audrey', 'Oneil', 'Dalton', 'SK'));
     $table = new \cli\Table();
     $table->setRenderer(new \cli\table\Ascii());
     $table->setHeaders($headers);
     $table->setRows($data);
     $table->display();
     $output = $this->getStreams();
     $this->assertEquals("", $output['errors']);
     $this->assertEquals(file_get_contents("test/output/table_1"), $output['contents']);
     $this->resetStreams();
     $table->sort(1);
     $table->display();
     $output = $this->getStreams();
     $this->assertEquals("", $output['errors']);
     $this->assertEquals(file_get_contents("test/output/table_2"), $output['contents']);
     $this->resetStreams();
     foreach ($data as $k => $v) {
         $data[$k] = array_combine(array("name", "surname", "city", "state"), $v);
     }
     $renderer = new \cli\table\Ascii();
     $renderer->setCharacters(array("corner" => "x", "line" => "=", "border" => "!"));
     $table = new \cli\Table($data);
     $table->setRenderer($renderer);
     $table->sort("surname");
     $table->display();
     $output = $this->getStreams();
     $this->assertEquals("", $output['errors']);
     $this->assertEquals(file_get_contents("test/output/table_3"), $output['contents']);
     $this->assertEquals("", \cli\Colors::color("reset"));
     $this->assertEquals("foo\tbar", \cli\table\Tabular::row(array("foo", "bar")));
     $this->assertNull(\cli\table\Tabular::border());
     // test output
     $this->resetStreams();
     \cli\out("  \\cli\\out sends output to STDOUT\n");
     \cli\out("  It does not automatically append a new line\n");
     \cli\out("  It does accept any number of %s which are then %s to %s for formatting\n", 'arguments', 'passed', 'sprintf');
     \cli\out("  Alternatively, {:a} can use an {:b} as the second argument.\n\n", array('a' => 'you', 'b' => 'array'));
     \cli\err('  \\cli\\err sends output to STDERR');
     \cli\err('  It does automatically append a new line');
     \cli\err('  It does accept any number of %s which are then %s to %s for formatting', 'arguments', 'passed', 'sprintf');
     \cli\err("  Alternatively, {:a} can use an {:b} as the second argument.\n", array('a' => 'you', 'b' => 'array'));
     \cli\line('  \\cli\\line forwards to \\cli\\out for output');
     \cli\line('  It does automatically append a new line');
     \cli\line('  It does accept any number of %s which are then %s to %s for formatting', 'arguments', 'passed', 'sprintf');
     \cli\line("  Alternatively, {:a} can use an {:b} as the second argument.\n", array('a' => 'you', 'b' => 'array'));
     $output = $this->getStreams();
     $this->assertEquals(file_get_contents("test/output/out_errors"), $output['errors']);
     $this->assertEquals(file_get_contents("test/output/out_contents"), $output['contents']);
     $string = "";
     $string .= \cli\render('  \\cli\\err sends output to STDERR' . "\n");
     $string .= \cli\render('  It does automatically append a new line' . "\n");
     $string .= \cli\render('  It does accept any number of %s which are then %s to %s for formatting' . "\n", 'arguments', 'passed', 'sprintf');
     $string .= \cli\render("  Alternatively, {:a} can use an {:b} as the second argument.\n\n", array('a' => 'you', 'b' => 'array'));
     $this->assertEquals(file_get_contents("test/output/out_errors"), $string);
     $this->resetStreams();
     $in = tmpfile();
     fputs($in, "quit\n");
     fseek($in, 0);
     \cli\Streams::setStream("in", $in);
     $line = \cli\prompt("prompt", false, "# ");
     $output = $this->getStreams();
     $this->assertEquals("quit", $line);
     $this->assertEquals("", $output['errors']);
     $this->assertEquals("prompt# ", $output['contents']);
     fseek($in, 0);
     $this->assertEquals("quit", \cli\input());
     fclose($in);
 }