Module: ConsoleTextFormatter
- Extended by:
- ConsoleTextFormatter
- Included in:
- ConsoleTextFormatter
- Defined in:
- lib/console-text-formatter.rb,
lib/console-text-formatter/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Class Method Summary collapse
- .build_row(items, pads) ⇒ Object
- .format(recs, fields) ⇒ Object
-
.pad(str, len) ⇒ Object
print formatter.
- .print_data(recs, width, fields, field_width) ⇒ Object
Class Method Details
.build_row(items, pads) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/console-text-formatter.rb', line 33 def self.build_row(items, pads) output = '' items.each_with_index do |item, i| output << pad(item, pads[i]) << "|" end output.chop! output << "\n" end |
.format(recs, fields) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/console-text-formatter.rb', line 6 def self.format(recs, fields) fields_width = Array.new(fields.length) fields.each_index{|i| fields_width[i] = fields[i].to_s.length } recs.each do |rec| fields.each_index do |i| if (fields_width[i] < rec[fields[i]].to_s.length) fields_width[i] = rec[fields[i]].to_s.length end end end fields_width.each_index{|i| fields_width[i] += 2 } rec_width = fields_width.length - 1 fields_width.each{|x| rec_width+=x } print_data(recs, rec_width, fields, fields_width) end |
.pad(str, len) ⇒ Object
print formatter
27 28 29 30 31 |
# File 'lib/console-text-formatter.rb', line 27 def self.pad(str, len) return "".ljust(len) if str.nil? #str = str.slice(0, len) # truncate long strings " "+str.ljust(len-1) # pad with whitespace end |
.print_data(recs, width, fields, field_width) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/console-text-formatter.rb', line 42 def self.print_data (recs, width, fields, field_width) output = '' output = "".ljust(width,"=") << "\n" output << build_row(fields, field_width) output << "".ljust(width,"-") << "\n" # table data recs.each do |rec| output << build_row(fields.map { |r| rec[r].to_s }, field_width) end output << "".ljust(width,"=") << "\n" print output end |