Example #1
0
 /**
  * Show items in a \cli\Table.
  *
  * @param array $items
  * @param array $fields
  */
 private static function show_table($items, $fields)
 {
     $table = new \cli\Table();
     $enabled = \cli\Colors::shouldColorize();
     if ($enabled) {
         \cli\Colors::disable(true);
     }
     $table->setHeaders($fields);
     foreach ($items as $item) {
         $table->addRow(array_values(\WP_CLI\Utils\pick_fields($item, $fields)));
     }
     foreach ($table->getDisplayLines() as $line) {
         \WP_CLI::line($line);
     }
     if ($enabled) {
         \cli\Colors::enable(true);
     }
 }
<?php

// Samples. Lines marked with * should be colored in output
// php examples/colors.php
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
// php examples/colors.php | cat
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
// *  All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
//    All output is run through \cli\Colors::colorize before display
require_once 'common.php';
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');
echo \cli\Colors::colorize('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n', true) . "\n";
echo \cli\Colors::colorize('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n') . "\n";
\cli\Colors::enable();
// Forcefully enable
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');
\cli\Colors::disable();
// Disable forcefully!
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n', true);
\cli\Colors::enable(false);
// Enable, but not forcefully
\cli\line('  %C%5All output is run through %Y%6\\cli\\Colors::colorize%C%5 before display%n');