module Capistrano::Doctor::OutputHelpers

Helper methods for pretty-printing doctor output to stdout. All output (other than ‘title`) is indented by four spaces to facilitate copying and pasting this output into e.g. GitHub or Stack Overflow to achieve code formatting.

Public Instance Methods

puts(string=nil) click to toggle source

Override ‘Kernel#puts` to prepend four spaces to each line.

# File lib/capistrano/doctor/output_helpers.rb, line 55
def puts(string=nil)
  $stdout.puts(string.to_s.gsub(/^/, "    "))
end
table(records, &block) click to toggle source

Prints a table for a given array of records. For each record, the block is yielded two arguments: the record and a Row object. To print values for that record, add values using ‘row << “some value”`. A row can optionally be highlighted in yellow using `row.yellow`.

# File lib/capistrano/doctor/output_helpers.rb, line 29
def table(records, &block)
  return if records.empty?
  rows = collect_rows(records, &block)
  col_widths = calculate_column_widths(rows)

  rows.each do |row|
    line = row.values.each_with_index.map do |value, col|
      value.to_s.ljust(col_widths[col])
    end.join(" ").rstrip
    line = color.colorize(line, row.color) if row.color
    puts line
  end
end
title(text) click to toggle source

Prints a title in blue with surrounding newlines.

# File lib/capistrano/doctor/output_helpers.rb, line 44
def title(text)
  # Use $stdout directly to bypass the indentation that our `puts` does.
  $stdout.puts(color.colorize("\n#{text}\n", :blue))
end
warning(text) click to toggle source

Prints text in yellow.

# File lib/capistrano/doctor/output_helpers.rb, line 50
def warning(text)
  puts color.colorize(text, :yellow)
end

Private Instance Methods

calculate_column_widths(rows) click to toggle source
# File lib/capistrano/doctor/output_helpers.rb, line 67
def calculate_column_widths(rows)
  num_columns = rows.map { |row| row.values.length }.max
  Array.new(num_columns) do |col|
    rows.map { |row| row.values[col].to_s.length }.max
  end
end
collect_rows(records) { |rec, row| ... } click to toggle source
# File lib/capistrano/doctor/output_helpers.rb, line 61
def collect_rows(records)
  records.map do |rec|
    Row.new.tap { |row| yield(rec, row) }
  end
end
color() click to toggle source
# File lib/capistrano/doctor/output_helpers.rb, line 74
def color
  @color ||= SSHKit::Color.new($stdout)
end