class Mycmd::Printer
Constants
- BORDER
Attributes
header[RW]
result[RW]
width[RW]
Public Class Methods
new(result, header=false)
click to toggle source
# File lib/mycmd/printer.rb, line 8 def initialize(result, header=false) @header = header if result.is_a? Mysql2::Result @result = result_to_array(result) @header = result.fields if @header else @result = result end end
Private Class Methods
print_title(title, empty_line=false)
click to toggle source
# File lib/mycmd/printer.rb, line 51 def print_title(title, empty_line=false) puts if empty_line puts "#{BORDER}\n#{title}\n#{BORDER}" end
Public Instance Methods
print()
click to toggle source
# File lib/mycmd/printer.rb, line 18 def print if @result.respond_to? :each set_width print_line(@header) if @header @result.each do |row| print_line(row) end end end
Private Instance Methods
print_line(line_array)
click to toggle source
# File lib/mycmd/printer.rb, line 46 def print_line(line_array) puts line_array.map.with_index {|f,i| f.to_s.ljust(@width[i], " ")}.join("\t") end
result_to_array(result)
click to toggle source
# File lib/mycmd/printer.rb, line 29 def result_to_array(result) result_array = [] result.each(as: :array) do |row| result_array << row end result_array end
set_width()
click to toggle source
# File lib/mycmd/printer.rb, line 37 def set_width @width = @header ? @header.map{|f| f.size} : Array.new(@result.first.size){0} @result.each do |row| row.each_with_index do |v,i| @width[i] = v.to_s.size if @width[i] < v.to_s.size end end end