lib/hirb/helpers/table.rb in hirb-0.5.0 vs lib/hirb/helpers/table.rb in hirb-0.6.0

- old
+ new

@@ -15,11 +15,11 @@ # | 1 | 2 | # | 2 | 3 | # +---+---+ # # By default, the fields/columns are the numerical indices of the array. -# +# # An array of hashes ie [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], would render: # +-----+--------+ # | age | weight | # +-----+--------+ # | 10 | 100 | @@ -70,11 +70,11 @@ :bottom => {:left => '+', :center => '+', :right => '+', :horizontal => '-', :vertical => {:outside => '|', :inside => '|'} } } class << self - + # Main method which returns a formatted table. # ==== Options: # [*:fields*] An array which overrides the default fields and can be used to indicate field order. # [*:headers*] A hash of fields and their header names. Fields that aren't specified here default to their name. # When set to false, headers are hidden. Can also be an array but only for array rows. @@ -95,10 +95,11 @@ # [*:filter_any*] When set to true, any cell defaults to being filtered by its class in :filter_classes. # Default Hirb::Helpers::Table.filter_any(). # [*:filter_classes*] Hash which maps classes to filters. Default is Hirb::Helpers::Table.filter_classes(). # [*:vertical*] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false. # [*:unicode*] When set to true, renders a unicode table using Hirb::Helpers::UnicodeTable. Default is false. + # [*:tab*] When set to true, renders a tab-delimited table using Hirb::Helpers::TabTable. Default is false. # [*:all_fields*] When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false. # [*:description*] When set to true, renders row count description at bottom. Default is true. # [*:escape_special_chars*] When set to true, escapes special characters \n,\t,\r so they don't disrupt tables. Default is false for # vertical tables and true for anything else. # Examples: @@ -109,10 +110,11 @@ # Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"} # Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]} def render(rows, options={}) options[:vertical] ? Helpers::VerticalTable.render(rows, options) : options[:unicode] ? Helpers::UnicodeTable.render(rows, options) : + options[:tab] ? Helpers::TabTable.render(rows, options) : new(rows, options).render rescue TooManyFieldsForWidthError $stderr.puts "", "** Hirb Warning: Too many fields for the current width. Configure your width " + "and/or fields to avoid this error. Defaulting to a vertical table. **" Helpers::VerticalTable.render(rows, options) @@ -222,44 +224,47 @@ [render_border(:bottom)] end def render_table_header title_row = chars[:top][:vertical][:outside] + ' ' + - @fields.map {|f| format_cell(@headers[f], @field_lengths[f]) }. - join(' ' + chars[:top][:vertical][:inside] +' ') + + format_values(@headers).join(' ' + chars[:top][:vertical][:inside] +' ') + ' ' + chars[:top][:vertical][:outside] [render_border(:top), title_row, render_border(:middle)] end - + def render_border(which) chars[which][:left] + chars[which][:horizontal] + @fields.map {|f| chars[which][:horizontal] * @field_lengths[f] }. join(chars[which][:horizontal] + chars[which][:center] + chars[which][:horizontal]) + chars[which][:horizontal] + chars[which][:right] end - + + def format_values(values) + @fields.map {|field| format_cell(values[field], @field_lengths[field]) } + end + def format_cell(value, cell_width) text = String.size(value) > cell_width ? ( (cell_width < 5) ? String.slice(value, 0, cell_width) : String.slice(value, 0, cell_width - 3) + '...' ) : value String.ljust(text, cell_width) end def render_rows @rows.map do |row| - row = chars[:bottom][:vertical][:outside] + ' ' + @fields.map {|f| - format_cell(row[f], @field_lengths[f]) - }.join(' ' +chars[:bottom][:vertical][:inside] + ' ') + ' ' + chars[:bottom][:vertical][:outside] + chars[:bottom][:vertical][:outside] + ' ' + + format_values(row).join(' ' + chars[:bottom][:vertical][:inside] + ' ') + + ' ' + chars[:bottom][:vertical][:outside] end end - + def render_table_description (@rows.length == 0) ? "0 rows in set" : "#{@rows.length} #{@rows.length == 1 ? 'row' : 'rows'} in set" end - + def setup_field_lengths @field_lengths = default_field_lengths if @options[:resize] raise TooManyFieldsForWidthError if @fields.size > self.actual_width.to_f / MIN_FIELD_LENGTH Resizer.resize!(self) @@ -332,10 +337,10 @@ row[f] = row[f].to_s || '' row[f] = row[f].gsub(/(\t|\r|\n)/) {|e| e.dump.gsub('"','') } if @options[:escape_special_chars] } } end - + # Converts an array to a hash mapping a numerical index to its array value. def array_to_indices_hash(array) array.inject({}) {|hash,e| hash[hash.size] = e; hash } end #:startdoc: