Merge pull request #87 from runcommand/46-skip-total

Don't include 'total' cell when the name column is omitted
This commit is contained in:
Daniel Bachhuber 2016-10-07 14:05:02 -07:00 committed by GitHub
commit 64f1af250a
2 changed files with 22 additions and 4 deletions

View file

@ -36,3 +36,15 @@ Feature: Basic profile usage
And STDOUT should be a table containing rows:
| callback | time |
| total | |
Scenario: Don't include 'total' cell when the name column is omitted
Given a WP install
When I run `wp profile eval 'wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 0 | 1 |
And STDOUT should not contain:
"""
total
"""

View file

@ -8,6 +8,8 @@ class Formatter {
private $args;
private $total_cell_index;
public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
$format_args = array(
'format' => 'table',
@ -25,6 +27,8 @@ class Formatter {
$format_args['fields'] = explode( ',', $format_args['fields'] );
}
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
$this->args = $format_args;
@ -38,7 +42,7 @@ class Formatter {
*/
public function display_items( $items ) {
if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) {
self::show_table( $items, $this->args['fields'] );
$this->show_table( $items, $this->args['fields'] );
} else {
$this->formatter->display_items( $items );
}
@ -50,7 +54,7 @@ class Formatter {
* @param array $items
* @param array $fields
*/
private static function show_table( $items, $fields ) {
private function show_table( $items, $fields ) {
$table = new \cli\Table();
$enabled = \cli\Colors::shouldColorize();
@ -61,11 +65,13 @@ class Formatter {
$table->setHeaders( $fields );
$totals = array_fill( 0, count( $fields ), null );
$totals[0] = 'total';
if ( ! is_null( $this->total_cell_index ) ) {
$totals[ $this->total_cell_index ] = 'total';
}
foreach ( $items as $item ) {
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );
foreach( $values as $i => $value ) {
if ( 0 === $i ) {
if ( ! is_null( $this->total_cell_index ) && $this->total_cell_index === $i ) {
continue;
}
if ( null === $totals[ $i ] ) {