&1', $output, $result); _drush_shell_exec_output_set($output); if (DRUSH_VERBOSE) { foreach ($output as $line) { drush_print($line, $indent + 2); } } // Exit code 0 means success. return ($result == 0); } /** * Stores output for the most recent shell command. * This should only be run from drush_shell_exec(). * * @param $output * The output of the most recent shell command. * If this is not set the stored value will be returned. */ function _drush_shell_exec_output_set($output = FALSE) { static $stored_output; if (!$output) return $stored_output; $stored_output = $output; } /** * Returns the output of the most recent shell command as an array of lines. */ function drush_shell_exec_output() { return _drush_shell_exec_output_set(); } /** * 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); } * to make your function return FALSE in case of an error. */ function drush_error($msg = '') { // TODO: print to stderr if running in CLI mode. drush_print("E: " . $msg); return FALSE; } /** * Prints a message. * @param $message * The message to print. * @param $indent * The indentation (space chars) */ function drush_print($message = '', $indent = 0) { print str_repeat(' ', $indent) . (string)$message . "\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; } print str_repeat(' ', $indent) . (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) . (string)$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 str_repeat(' ', $indent) . (string)$msg . " (y/n): "; } } /** * Print a formatted table. * @param $rows * The rows to print * @param $indent * Indentation for the whole table * @param $header * If TRUE, the first line will be treated as table * header and therefore be underlined. */ function drush_print_table($rows, $indent = 0, $header = FALSE) { if (count($rows) == 0) { // Nothing to output. return; } $indent = str_repeat(' ', $indent); $format = _drush_get_table_row_format($rows); $header_printed = FALSE; foreach ($rows as $cols) { // Print the current line. print $indent . vsprintf($format, $cols) . "\n"; // Underline the first row if $header is set to true. if (!$header_printed && $header) { $headers = array(); foreach ($cols as $col) { $headers[] = str_repeat('-', strlen($col)); } print $indent . trim(vsprintf($format, $headers)) . "\n"; $header_printed = TRUE; } } } /** * Create the table row format string to be used in vsprintf(). */ function _drush_get_table_row_format($table) { $widths = _drush_get_table_column_widths($table); foreach ($widths as $col_width) { $col_formats[] = "%-{$col_width}s"; } $format = implode("\t", $col_formats); return $format; } /** * Calculate table column widths. */ function _drush_get_table_column_widths($table) { $widths = array(); foreach ($table as $row => $cols) { foreach ($cols as $col => $value) { $old_width = isset($widths[$col]) ? $widths[$col] : 0; $widths[$col] = max($old_width, strlen((string)$value)); } } return $widths; }