Class: Af::OptionParser::Columnizer
- Inherits:
-
Object
- Object
- Af::OptionParser::Columnizer
- Defined in:
- lib/fiksu-af/option_parser/columnizer.rb
Instance Method Summary collapse
-
#columnized(rows, options = {}) ⇒ Object
Converts an array of arrays into a single array of columnized strings.
-
#columnized_row(fields, sized) ⇒ Object
Convert an array into a single string, where each item consumes a static number of characters.
Instance Method Details
#columnized(rows, options = {}) ⇒ Object
Converts an array of arrays into a single array of columnized strings.
Arguments
* rows - arrays to convert
* - hash of , includes:
:max_width => <integer max width of columns>
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fiksu-af/option_parser/columnizer.rb', line 23 def columnized(rows, = {}) sized = {} rows.each do |row| row.each_index do |i| value = row[i] sized[i] = [sized[i].to_i, value.to_s.length].max sized[i] = [[:max_width], sized[i].to_i].min if [:max_width] end end return rows.map { |row| " " + columnized_row(row, sized).rstrip } end |
#columnized_row(fields, sized) ⇒ Object
Convert an array into a single string, where each item consumes a static number of characters. Long fields are truncated and small ones are padded.
Arguments
* fields - array of objects that respond to "to_s"
* sized - character count for each field in the new string????
9 10 11 12 13 14 15 |
# File 'lib/fiksu-af/option_parser/columnizer.rb', line 9 def columnized_row(fields, sized) r = [] fields.each_with_index do |f, i| r << sprintf("%0-#{sized[i]}s", f.to_s.gsub(/\\n\\r/, '').slice(0, sized[i])) end return r.join(' ') end |