table.inc

  1. 8.0.x commands/core/outputformat/table.inc
  2. 6.x commands/core/outputformat/table.inc
  3. 7.x commands/core/outputformat/table.inc
  4. master commands/core/outputformat/table.inc

Classes

Namesort descending Description
drush_outputformat_table Output formatter 'table'

File

commands/core/outputformat/table.inc
View source
  1. <?php
  2. /**
  3. * Output formatter 'table'
  4. *
  5. * @param $data
  6. * The $data parameter is expected to be an array (keys ignored) of
  7. * rows; each row, in turn, is an array of key / value pairs. Every
  8. * row is expected to have the same set of keys. The data is rendered
  9. * as a formatted word-wrapped table with rows of data cells aligned in
  10. * columns.
  11. * @param $metadata
  12. * 'field-labels' - If present, contains an array of key / value pairs
  13. * that map from the keys in the row columns to the label for the
  14. * column header.
  15. * 'column-widths' - If present, contains an array of key / value pairs,
  16. * where the key is the integer column number, and the value is the
  17. * width that column should be formatted to.
  18. *
  19. * Code:
  20. *
  21. * return array(
  22. * "a" => array("b" => 2, "c" => 3),
  23. * "d" => array("b" => 5, "c" => 6)
  24. * );
  25. *
  26. * Output with --format=table:
  27. *
  28. * b c
  29. * 2 3
  30. * 5 6
  31. */
  32. class drush_outputformat_table extends drush_outputformat {
  33. function format($input, $metadata) {
  34. $field_list = isset($metadata['field-labels']) ? $metadata['field-labels'] : array();
  35. $widths = array();
  36. $col = 0;
  37. foreach($field_list as $key => $label) {
  38. if (isset($metadata['column-widths'][$key])) {
  39. $widths[$col] = $metadata['column-widths'][$key];
  40. }
  41. ++$col;
  42. }
  43. $rows = drush_rows_of_key_value_to_array_table($input, $field_list, $metadata);
  44. $field_labels = array_key_exists('include-field-labels', $metadata) && $metadata['include-field-labels'];
  45. if (!$field_labels) {
  46. array_shift($rows);
  47. }
  48. return drush_format_table($rows, $field_labels, $widths);
  49. }
  50. }