function drush_print_table

8.0.x output.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)
6.x output.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)
7.x output.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)
3.x drush.inc drush_print_table($rows, $header = FALSE, $widths = array())
4.x drush.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)
5.x output.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)
master output.inc drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL)

Print a formatted table.

Parameters

$rows: The rows to print.

$header: If TRUE, the first line will be treated as table header.

$widths: The widths of each column (in characters) to use - if not specified this will be determined automatically, based on a "best fit" algorithm.

$handle: File handle to write to. NULL will write to standard output, STDERR will write to the standard error. See http://php.net/manual/en/features.commandline.io-streams.php

Return value

$tbl Use $tbl->getTable() to get the output from the return value.

Related topics

19 calls to drush_print_table()
drush_choice in includes/drush.inc
Ask the user to select an item from a list. From a provided associative array, drush_choice will display all of the questions, numbered from 1 to N, and return the item the user selected. "0" is always cancel; entering a blank line is also…
drush_core_global_options in commands/core/core.drush.inc
drush_core_help in commands/core/help.drush.inc
Command callback for help command. This is the default command, when none other has been specified.
drush_core_status in commands/core/core.drush.inc
Command callback. Provides a birds-eye view of the current Drupal installation.
drush_core_watchdog_show_many in commands/core/watchdog.drush.inc
Print a table of watchdog messages.

... See full list

File

includes/drush.inc, line 698
The drush API implementation and helpers.

Code

function drush_print_table($rows, $header = FALSE, $widths = array(), $handle = NULL) {
  $tbl = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, '');

  $auto_widths = drush_table_column_autowidth($rows, $widths);

  // Do wordwrap on all cells.
  $newrows = array();
  foreach ($rows as $rowkey => $row) {
    foreach ($row as $col_num => $cell) {
      $newrows[$rowkey][$col_num] = wordwrap($cell, $auto_widths[$col_num], "\n", TRUE);
      if (isset($widths[$col_num])) {
        $newrows[$rowkey][$col_num] = str_pad($newrows[$rowkey][$col_num], $widths[$col_num]);
      }
    }
  }
  if ($header) {
    $headers = array_shift($newrows);
    $tbl->setHeaders($headers);
  }

  $tbl->addData($newrows);
  $output = $tbl->getTable();
  if (!stristr(PHP_OS, 'WIN')) {
    $output = str_replace("\r\n", PHP_EOL, $output);
  }

  // Check if the handle argument is a string to preserve compatability with
  // previous versions that accepted a filename instead.
  if (is_string($handle)) {
    file_put_contents($handle, $output, FILE_APPEND);
  }
  else {
    drush_print($output, 0, $handle);
  }
  return $tbl;
}