mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-21 06:39:01 +08:00
Introduce our own formatter, so we can implement custom tables
This commit is contained in:
parent
47b6968f5c
commit
919dda5637
2 changed files with 78 additions and 3 deletions
|
@ -135,7 +135,7 @@ class Command {
|
|||
$this->focus_log[ $hook ]['hook'] = '';
|
||||
}
|
||||
}
|
||||
$formatter = new \WP_CLI\Formatter( $assoc_args, $focus_fields );
|
||||
$formatter = new Formatter( $assoc_args, $focus_fields );
|
||||
$formatter->display_items( $this->focus_log );
|
||||
} else if ( $this->focus_hook ) {
|
||||
$hook_fields = array(
|
||||
|
@ -152,7 +152,7 @@ class Command {
|
|||
}
|
||||
}
|
||||
}
|
||||
$formatter = new \WP_CLI\Formatter( $assoc_args, $hook_fields );
|
||||
$formatter = new Formatter( $assoc_args, $hook_fields );
|
||||
$formatter->display_items( $this->hook_log );
|
||||
} else {
|
||||
|
||||
|
@ -164,7 +164,7 @@ class Command {
|
|||
}
|
||||
}
|
||||
}
|
||||
$formatter = new \WP_CLI\Formatter( $assoc_args, $scope_fields );
|
||||
$formatter = new Formatter( $assoc_args, $scope_fields );
|
||||
$formatter->display_items( $this->scope_log );
|
||||
}
|
||||
}
|
||||
|
|
75
inc/class-formatter.php
Normal file
75
inc/class-formatter.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace runcommand\Profile;
|
||||
|
||||
class Formatter {
|
||||
|
||||
private $formatter;
|
||||
|
||||
private $args;
|
||||
|
||||
public function __construct( &$assoc_args, $fields = null, $prefix = false ) {
|
||||
$format_args = array(
|
||||
'format' => 'table',
|
||||
'fields' => $fields,
|
||||
'field' => null
|
||||
);
|
||||
|
||||
foreach ( array( 'format', 'fields', 'field' ) as $key ) {
|
||||
if ( isset( $assoc_args[ $key ] ) ) {
|
||||
$format_args[ $key ] = $assoc_args[ $key ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $format_args['fields'] ) ) {
|
||||
$format_args['fields'] = explode( ',', $format_args['fields'] );
|
||||
}
|
||||
|
||||
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
|
||||
|
||||
$this->args = $format_args;
|
||||
$this->formatter = new \WP_CLI\Formatter( $assoc_args, $fields, $prefix );
|
||||
}
|
||||
|
||||
/**
|
||||
* Display multiple items according to the output arguments.
|
||||
*
|
||||
* @param array $items
|
||||
*/
|
||||
public function display_items( $items ) {
|
||||
if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) {
|
||||
self::show_table( $items, $this->args['fields'] );
|
||||
} else {
|
||||
$this->formatter->display_items( $items );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue