&1', $output, $result); if (DRUSH_VERBOSE) { foreach ($output as $line) { drush_print($line, $indent + 2); } } return ($result == 0); // exit code zero means success } /** * Exits with a message. * TODO: Exit with a correct status code. */ function drush_die($msg = NULL, $status = NULL) { die($msg ? "drush: $msg\n" : ''); } /** * Prints an error message. * Always returns FALSE. This allows to do e.g. * if ($error) return drush_error('A error occured); */ function drush_error($msg = '') { // TODO: print to stderr if running in CLI mode. drush_print("E: " . $msg); return FALSE; } /** * Prints a message. * @param $msg The message to print. * @param $indent The indentation (space chars) */ function drush_print($msg = '', $indent = 0) { if ($indent > 0) print str_repeat(' ', $indent); print (string)$msg . "\n"; } /** * Prints a message, but only if verbose mode is activated. * Returns TRUE if in verbose mode, otherwise FALSE. */ function drush_verbose($msg = FALSE, $indent = 0) { if (!DRUSH_VERBOSE) return FALSE; if (DRUSH_VERBOSE && $msg === FALSE) return TRUE; if ($indent > 0) print str_repeat(' ', $indent); print (string)$msg . "\n"; return TRUE; } /** * Ask the user a basic yes/no question. * * @param $msg The question to ask * @return TRUE if the user entered 'y', FALSE if he entered 'n' */ function drush_confirm($msg, $indent = 0) { print str_repeat(' ', $indent) . $msg . " (y/n): "; if (DRUSH_AFFIRMATIVE) { print "y\n"; return TRUE; } while ($line = trim(fgets(STDIN))) { if ($line == 'y') return TRUE; if ($line == 'n') return FALSE; print $msg . " (y/n): "; } } /** * Print a formatted table. * @param $rows The rows to print * @param $indent Indent for the whole table * @param $header If TRUE, the first line will be treated as table * header and be underlined. */ function drush_print_table($rows, $indent = 0, $header = FALSE) { if (count($rows) == 0) return; // nothing to output $indent = str_repeat(' ', $indent); $format = _drush_get_table_row_format($rows); $counter = 0; foreach ($rows as $cols) { print $indent . vsprintf($format, $cols) . "\n"; if ($counter == 0 && $header) { $headers = array(); foreach ($cols as $col) $headers[] = str_repeat('-', strlen($col)); print $indent . trim(vsprintf($format, $headers)) . "\n"; } $counter++; } } /** * Format a table */ function _drush_get_table_row_format($table) { $widths = _drush_get_table_column_widths($table); $format = implode("\t", array_map(create_function('$width', 'return "%-{$width}s";'), $widths)); return $format; } function _drush_get_table_column_widths($table) { $widths = array(); foreach ($table as $row => $cols) { foreach ($cols as $col => $value) { $widths[$col] = max($widths[$col], strlen((string)$value)); } } return $widths; }