Advertisement
Azure

table_format - a ascii table formatter

Aug 12th, 2011
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.32 KB | None | 0 0
  1. require 'active_support/core_ext/object/blank'
  2. # require "benchmark"
  3.  
  4. =begin benchmark setup
  5. a = []
  6.  
  7. 10000.times do
  8.     a << "BlackRose@sakura.theta.cyberconnect.co.jp"
  9.     a << "Azure!~azt@unaffiliated/azure"
  10. end
  11. =end
  12.  
  13. def table_format array, params={}
  14.     params = {
  15.         splitchars: "\t",
  16.         regexp: nil,
  17.         justify: :left, # :left, :right
  18.         headers: [], # ary (ex: ["a","b","c"])
  19.         gutter: 4,
  20.         left_gutter: false
  21.     }.merge!(params)
  22.    
  23.     source = array.dup
  24.  
  25.     # We use the str.tr method if our regexp param is nil.
  26.     # Otherwise, we just use regexp.
  27.     source.map! {|e| params[:regexp].nil? ? e.tr(params[:splitchars], "\t").split("\t") : e.match(params[:regexp])[1..-1].map {|e| e.nil? ? "" : e; } }
  28.  
  29.     # calculating the maximum length of each column
  30.     column_lengths = []
  31.     source.dup.unshift(params[:headers]).each {|e|
  32.         e.each_with_index {|item,index|
  33.             column_lengths[index] = [] if column_lengths[index].blank?
  34.             column_lengths[index] << item.size
  35.         }
  36.     }
  37.     column_lengths.map! {|e| e.sort.last }
  38.  
  39.     data = []
  40.  
  41.     # Generating table headers
  42.     if !params[:headers].blank?
  43.         # Generating the headers, separators, etc.
  44.         s_header = []
  45.         s_separator = s_header.dup
  46.         params[:headers].each_with_index {|e,i|
  47.             s_header << "%#{"-" if params[:justify] == :left}#{column_lengths[i]}s" % e
  48.             s_separator << "-"*column_lengths[i]
  49.         }
  50.         data << s_header.join(" "*params[:gutter]) << s_separator.join(" "*params[:gutter])
  51.     end
  52.  
  53.     # Generating formatted table rows
  54.     source.each {|e|
  55.         line = []
  56.         e.each_with_index {|item,index|
  57.             line << "%#{"-" if params[:justify] == :left}#{column_lengths[index]}s" % item
  58.         }
  59.         data << line.join(" "*params[:gutter])
  60.     }
  61.  
  62.     # Adding a gutter to the left side
  63.     if params[:left_gutter] === true then data.map! {|e| " "*params[:gutter] + e } end
  64.        
  65.     data.join("\n")
  66.  
  67. end
  68.  
  69. =begin benchmark
  70. Benchmark.bm(20) do |x|
  71.   x.report("table_format (20000):")   { table_format a, splitchars: "!@", regexp: /(.+!)?(.+)@(.+)/, headers: ["nick","username","host"], gutter: 6, justify: :right, left_gutter: true; }
  72. end
  73.  
  74. results: #######################################################################
  75.                           user     system      total        real
  76. table_format (20000):  0.593000   0.000000   0.593000 (  0.617036)
  77.  
  78. =end
  79. =begin test cases:
  80.  
  81. Default (only array) ###########################################################
  82. table_format a
  83. > [NLR]Azuresocks!~azt@unaffiliated/azure
  84. > Azure!~azt@unaffiliated/azure          
  85. > Azure|netbook!~blue@affiliated/azure  
  86.  
  87. splitchars only ################################################################
  88. table_format a, splitchars: "!@"
  89. > [NLR]Azuresocks    ~azt     unaffiliated/azure
  90. > Azure              ~azt     unaffiliated/azure
  91. > Azure|netbook      ~blue    affiliated/azure  
  92.  
  93. headers only ###################################################################
  94. table_format a, headers: ["nick","username","host"]
  95. > nick                                       username    host
  96. > ---------------------------------------    --------    ----
  97. > [NLR]Azuresocks!~azt@unaffiliated/azure
  98. > Azure!~azt@unaffiliated/azure          
  99. > Azure|netbook!~blue@affiliated/azure  
  100.  
  101. splitchars + headers ###########################################################
  102. table_format a, splitchars: "!@", headers: ["nick","username","host"]
  103. > nick               username    host              
  104. > ---------------    --------    ------------------
  105. > [NLR]Azuresocks    ~azt        unaffiliated/azure
  106. > Azure              ~azt        unaffiliated/azure
  107. > Azure|netbook      ~blue       affiliated/azure
  108.  
  109. All options ####################################################################
  110. table_format a, splitchars: "!@", headers: ["nick","username","host"], gutter: 20, justify: :right, left_gutter: true;
  111. >                                nick                    username                                  host
  112. >                     ---------------                    --------                    ------------------
  113. >                     [NLR]Azuresocks                        ~azt                    unaffiliated/azure
  114. >                               Azure                        ~azt                    unaffiliated/azure
  115. >                       Azure|netbook                       ~blue                      affiliated/azure
  116. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement