Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- require 'active_support/core_ext/object/blank'
- module Helpers
- # Formats a supplied array into a list or table.
- # @author Mark Seymour ('Azure') (mark.seymour.ns@gmail.com)
- #
- # @param [Array] The source array.
- # @param [Hash] params Options for generating the table.
- # @option params [String] :splitchars ('\t') Characters to split each individual item on.
- # @option params [Regexp] :regexp (nil) A Regexp to split each individual item on. If one is supplied, then :splitchars is ignored.
- # @option params [Symbol] :justify (:left) Changes the justification of table items., `:left` or `:right`
- # @option params [Array] :headers ([]) Sets the names for the table column headers. If left unset, no headers will be generated.
- # @option params [Integer] :gutter (4) Sets the gutter size between columns and optionally the left gutter.
- # @option params [Boolean] :left_gutter (false) Generates a gutter on the left side of the generated table, equal to the same length as column gutters.
- # @todo Have :justify be set on individual columns.
- # @todo Have the ability to output either an array or string (params[:array]?)
- # @return [String] The formatted string with line endings.
- def Helpers.table_format array, params={}
- params = {
- splitchars: "\t",
- regexp: nil,
- justify: :left, # :left, :right
- headers: [], # ary (ex: ["a","b","c"])
- gutter: 4,
- left_gutter: false
- }.merge!(params)
- source = array.dup # We cannot be altering our original object now, can we?
- # We use the str.tr method if our regexp param is nil.
- # Otherwise, we just use regexp.
- source.map! {|e| params[:regexp].nil? ? e.tr(params[:splitchars], "\t").split("\t") : e.match(params[:regexp])[1..-1].map {|e| e.nil? ? "" : e; } }
- # calculating the maximum length of each column
- column_lengths = []
- source.dup.unshift(params[:headers]).each {|e|
- e.each_with_index {|item,index|
- column_lengths[index] = [] if column_lengths[index].blank?
- column_lengths[index] << item.size
- }
- }
- column_lengths.map! {|e| e.sort.last }
- data = []
- # Generating table headers
- if !params[:headers].blank?
- # Generating the headers, separators, etc.
- s_header = []
- s_separator = s_header.dup
- params[:headers].each_with_index {|e,i|
- s_header << "%#{"-" if params[:justify] == :left}#{column_lengths[i]}s" % e.to_s
- s_separator << "-"*column_lengths[i]
- }
- data << s_header.join(" "*params[:gutter]) << s_separator.join(" "*params[:gutter])
- end
- # Generating formatted table rows
- source.each {|e|
- line = []
- e.each_with_index {|item,index|
- line << "%#{"-" if params[:justify] == :left}#{column_lengths[index]}s" % item.to_s
- }
- data << line.join(" "*params[:gutter])
- }
- # Adding a gutter to the left side
- if params[:left_gutter] === true then data.map! {|e| " "*params[:gutter] + e } end
- data.join("\n")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement