Sha256: 416d3c890e1357903c34c0d4406791781fe7e914944469c5c7cbbeb09105e674

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "thor"
Thor::Shell::Basic.class_eval do
  def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)\Z/))
    message = message.to_s

    message = set_color(message, *color) if color && can_display_colors?

    spaces = "  " * padding

    if force_new_line
      stdout.puts(spaces + message)
    else
      stdout.print(spaces + message)
    end

    #puts "message:#{message}   line:#{__LINE__}"
    $ro_out << message
  end

  def print_table(array, options={})
    return if array.empty?

    formats, indent, colwidth = [], options[:indent].to_i, options[:colwidth]
    options[:truncate] = terminal_width if options[:truncate] == true

    formats << "%-#{colwidth + 2}s" if colwidth
    start = colwidth ? 1 : 0

    colcount = array.max{|a,b| a.size <=> b.size }.size

    maximas = []

    start.upto(colcount - 1) do |index|
      maxima = array.map {|row| row[index] ? row[index].to_s.size : 0 }.max
      maximas << maxima
      if index == colcount - 1
        # Don't output 2 trailing spaces when printing the last column
        formats << "%-s"
      else
        formats << "%-#{maxima + 2}s"
      end
    end

    formats[0] = formats[0].insert(0, " " * indent)
    formats << "%s"

    array.each do |row|
      sentence = ""

      row.each_with_index do |column, index|
        maxima = maximas[index]

        if column.is_a?(Numeric)
          if index == row.size - 1
            # Don't output 2 trailing spaces when printing the last column
            f = "%#{maxima}s"
          else
            f = "%#{maxima}s  "
          end
        else
          f = formats[index]
        end
        sentence << f % column.to_s
      end

      sentence = truncate(sentence, options[:truncate]) if options[:truncate]
      $ro_out << sentence
      stdout.puts sentence
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ro_commands-0.0.1 lib/ro_commands/core_ext/thor.rb