Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'active_support/core_ext/object/blank'
- # require "benchmark"
- =begin benchmark setup
- a = []
- 10000.times do
- a << "BlackRose@sakura.theta.cyberconnect.co.jp"
- a << "Azure!~azt@unaffiliated/azure"
- end
- =end
- def 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 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
- 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
- }
- 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
- =begin benchmark
- Benchmark.bm(20) do |x|
- x.report("table_format (20000):") { table_format a, splitchars: "!@", regexp: /(.+!)?(.+)@(.+)/, headers: ["nick","username","host"], gutter: 6, justify: :right, left_gutter: true; }
- end
- results: #######################################################################
- user system total real
- table_format (20000): 0.593000 0.000000 0.593000 ( 0.617036)
- =end
- =begin test cases:
- Default (only array) ###########################################################
- table_format a
- > [NLR]Azuresocks!~azt@unaffiliated/azure
- > Azure!~azt@unaffiliated/azure
- > Azure|netbook!~blue@affiliated/azure
- splitchars only ################################################################
- table_format a, splitchars: "!@"
- > [NLR]Azuresocks ~azt unaffiliated/azure
- > Azure ~azt unaffiliated/azure
- > Azure|netbook ~blue affiliated/azure
- headers only ###################################################################
- table_format a, headers: ["nick","username","host"]
- > nick username host
- > --------------------------------------- -------- ----
- > [NLR]Azuresocks!~azt@unaffiliated/azure
- > Azure!~azt@unaffiliated/azure
- > Azure|netbook!~blue@affiliated/azure
- splitchars + headers ###########################################################
- table_format a, splitchars: "!@", headers: ["nick","username","host"]
- > nick username host
- > --------------- -------- ------------------
- > [NLR]Azuresocks ~azt unaffiliated/azure
- > Azure ~azt unaffiliated/azure
- > Azure|netbook ~blue affiliated/azure
- All options ####################################################################
- table_format a, splitchars: "!@", headers: ["nick","username","host"], gutter: 20, justify: :right, left_gutter: true;
- > nick username host
- > --------------- -------- ------------------
- > [NLR]Azuresocks ~azt unaffiliated/azure
- > Azure ~azt unaffiliated/azure
- > Azure|netbook ~blue affiliated/azure
- =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement