Advertisement
Azure

table_format - a ascii table formatter (nc) (tabs→spaces)

Aug 12th, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.04 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. require 'active_support/core_ext/object/blank'
  3.  
  4. module Helpers
  5.   # Formats a supplied array into a list or table.
  6.   # @author Mark Seymour ('Azure') (mark.seymour.ns@gmail.com)
  7.   #
  8.   # @param [Array] The source array.
  9.   # @param [Hash] params Options for generating the table.
  10.   # @option params [String] :splitchars ('\t') Characters to split each individual item on.
  11.   # @option params [Regexp] :regexp (nil) A Regexp to split each individual item on. If one is supplied, then :splitchars is ignored.
  12.   # @option params [Symbol] :justify (:left) Changes the justification of table items., `:left` or `:right`
  13.   # @option params [Array] :headers ([]) Sets the names for the table column headers. If left unset, no headers will be generated.
  14.   # @option params [Integer] :gutter (4) Sets the gutter size between columns and optionally the left gutter.
  15.   # @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.
  16.   # @todo Have :justify be set on individual columns.
  17.   # @todo Have the ability to output either an array or string (params[:array]?)
  18.   # @return [String] The formatted string with line endings.
  19.   def Helpers.table_format array, params={}
  20.     params = {
  21.       splitchars: "\t",
  22.       regexp: nil,
  23.       justify: :left, # :left, :right
  24.       headers: [], # ary (ex: ["a","b","c"])
  25.       gutter: 4,
  26.       left_gutter: false
  27.     }.merge!(params)
  28.    
  29.     source = array.dup # We cannot be altering our original object now, can we?
  30.  
  31.     # We use the str.tr method if our regexp param is nil.
  32.     # Otherwise, we just use regexp.
  33.     source.map! {|e| params[:regexp].nil? ? e.tr(params[:splitchars], "\t").split("\t") : e.match(params[:regexp])[1..-1].map {|e| e.nil? ? "" : e; } }
  34.  
  35.     # calculating the maximum length of each column
  36.     column_lengths = []
  37.     source.dup.unshift(params[:headers]).each {|e|
  38.       e.each_with_index {|item,index|
  39.         column_lengths[index] = [] if column_lengths[index].blank?
  40.         column_lengths[index] << item.size
  41.       }
  42.     }
  43.     column_lengths.map! {|e| e.sort.last }
  44.  
  45.     data = []
  46.  
  47.     # Generating table headers
  48.     if !params[:headers].blank?
  49.       # Generating the headers, separators, etc.
  50.       s_header = []
  51.       s_separator = s_header.dup
  52.       params[:headers].each_with_index {|e,i|
  53.         s_header << "%#{"-" if params[:justify] == :left}#{column_lengths[i]}s" % e.to_s
  54.         s_separator << "-"*column_lengths[i]
  55.       }
  56.       data << s_header.join(" "*params[:gutter]) << s_separator.join(" "*params[:gutter])
  57.     end
  58.  
  59.     # Generating formatted table rows
  60.     source.each {|e|
  61.       line = []
  62.       e.each_with_index {|item,index|
  63.         line << "%#{"-" if params[:justify] == :left}#{column_lengths[index]}s" % item.to_s
  64.       }
  65.       data << line.join(" "*params[:gutter])
  66.     }
  67.  
  68.     # Adding a gutter to the left side
  69.     if params[:left_gutter] === true then data.map! {|e| " "*params[:gutter] + e } end
  70.      
  71.     data.join("\n")
  72.   end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement