4 * Output formatter 'key_value'
7 * The $data parameter contains an array of key / value pairs which
8 * are rendered as "key : value" in a formatted word-wrapped table
9 * with aligned columns. 'value' is expected to always be a simple string;
10 * if it is not, it is rendered with var_export.
12 * 'label' - If present, creates a section header "[label]" prior to the data
13 * 'separator' - If present, used instead of ', ' when impoding data values
14 * 'ini-item' - If present, selects a single item from any data value that is
15 * an array and uses it instead of imploding all values together.
20 * "b" => "Two B or ! Two B, that is the comparison",
21 * "c" => "I see that C has gone to Sea"
24 * Output with --format=key-value:
26 * b : Two B or ! Two B,
29 * c : I see that C has gone
36 * "b" => "Two B or ! Two B, that is the comparison",
37 * "c" => "I see that C has gone to Sea"
40 * "e" => "Elephants and electron microscopes",
41 * "f" => "My margin is too small"
45 * Output with --format=key-value-list:
47 * b : Two B or ! Two B,
50 * c : I see that C has gone
54 * electron microscopes
55 * f : My margin is too
58 class drush_outputformat_key_value extends drush_outputformat {
59 function format($input, $metadata) {
60 if (!is_array($input)) {
61 if (isset($metadata['label'])) {
62 $input = array(dt($metadata['label']) => $input);
65 return $this->format_error(dt('No label provided.'));
68 $kv_metadata = isset($metadata['table-metadata']) ? $metadata['table-metadata'] : array();
69 if ((!isset($kv_metadata['key-value-item'])) && (isset($metadata['field-labels']))) {
70 $input = drush_select_output_fields($input, $metadata['field-labels'], $metadata['field-mappings']);
72 if (isset($metadata['include-field-labels'])) {
73 $kv_metadata['include-field-labels'] = $metadata['include-field-labels'];
75 $formatted_table = drush_key_value_to_array_table($input, $kv_metadata);
76 if ($formatted_table === FALSE) {
79 return drush_format_table($formatted_table, FALSE, array());