class RequestLogAnalyzer::Output::FixedWidth

Fixed Width output class. Outputs a fixed width ASCII or UF8 report.

Constants

CHARACTERS

Attributes

characters[R]

Public Class Methods

new(io, options = {}) click to toggle source

Initialize a report io iO Object (file, STDOUT, etc.) options

* <tt>:characters</tt> :utf for UTF8 or :ascii for ANSI compatible output. Defaults to :utf.
* <tt>:color</tt> If true, ASCII colorization is used, else Monochrome. Defaults to Monochrome.
* <tt>:width</tt> Output width in characters. Defaults to 80.
Calls superclass method RequestLogAnalyzer::Output::Base::new
   # File lib/request_log_analyzer/output/fixed_width.rb
64 def initialize(io, options = {})
65   super(io, options)
66   @options[:width]      ||= 80
67   @options[:characters] ||= :utf
68   @characters = CHARACTERS[@options[:characters]]
69 
70   color_module = @options[:color] ? Color : Monochrome
71   (class << self; self; end).send(:include, color_module)
72 end

Public Instance Methods

<<(str)
Alias for: print
header() click to toggle source

Generate a header for a report

    # File lib/request_log_analyzer/output/fixed_width.rb
113 def header
114   if io.is_a?(File)
115     puts colorize('Request-log-analyzer summary report', :white, :bold)
116     line(:green)
117     puts "Version #{RequestLogAnalyzer::VERSION} - written by Willem van Bergen and Bart ten Brinke"
118     puts "Website: #{link('http://github.com/wvanbergen/request-log-analyzer')}"
119   end
120 end
line(*font) click to toggle source

Write a line

   # File lib/request_log_analyzer/output/fixed_width.rb
97 def line(*font)
98   puts colorize(characters[:horizontal_line] * @options[:width], *font)
99 end
print(str) click to toggle source

Write a string to the output object. str The string to write.

Also aliased as: <<
puts(str = '') click to toggle source

Write a string to the output object with a newline at the end. str The string to write.

   # File lib/request_log_analyzer/output/fixed_width.rb
84 def puts(str = '')
85   @io << str << "\n"
86 end
table(*columns) { |rows| ... } click to toggle source

Generate a report table and push it into the output object. *colums<tt> Columns hash <tt>&block: A block yeilding the rows.

    # File lib/request_log_analyzer/output/fixed_width.rb
134 def table(*columns, &_block)
135   rows = []
136   yield(rows)
137 
138   # determine maximum cell widths
139   max_cell_widths = rows.reduce(Array.new(columns.length, 0)) do |result, row|
140     lengths = row.map { |column| column.to_s.length }
141     result.each_with_index { |length, index| result[index] = ([length, lengths[index]].max rescue length) }
142   end
143   columns.each_with_index { |col, index| col[:actual_width] ||= max_cell_widths[index] }
144 
145   # determine actual column width
146   column_widths = columns.map do |column|
147     if column[:width] == :rest
148       nil
149     elsif column[:width]
150       column[:width]
151     elsif column[:min_width]
152       [column[:min_width], column[:actual_width]].max
153     elsif column[:max_width]
154       [column[:max_width], column[:actual_width]].min
155     else
156       column[:actual_width]
157     end
158   end
159 
160   if column_widths.include?(nil)
161     width_left = options[:width] - ((columns.length - 1) * (style[:cell_separator] ? 3 : 1)) - column_widths.compact.reduce(0) { |sum, col| sum + col }
162     column_widths[column_widths.index(nil)] = width_left
163   end
164 
165   line(:green) if @style[:top_line]
166 
167   # Print table header
168   if table_has_header?(columns)
169     column_titles = []
170     columns.each_with_index do |column, index|
171       width = column_widths[index]
172       alignment = (column[:align] == :right ? '' : '-')
173       column_titles.push(colorize("%#{alignment}#{width}s" % column[:title].to_s[0...width], :bold))
174     end
175 
176     puts column_titles.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ')
177     line(:green)
178   end
179 
180   # Print the rows
181   rows.each do |row|
182     row_values = []
183     columns.each_with_index do |column, index|
184       width = column_widths[index]
185       case column[:type]
186       when :ratio
187         if width > 4
188           if column[:treshold] && column[:treshold] < row[index].to_f
189             bar = ''
190             bar << characters[:block] * (width.to_f * column[:treshold]).round
191             bar << colorize(characters[:block] * (width.to_f * (row[index].to_f - column[:treshold])).round, :red)
192             row_values.push(bar)
193           else
194             # Create a bar by combining block characters
195             row_values.push(characters[:block] * (width.to_f * row[index].to_f).round)
196           end
197         else
198           # Too few characters for a ratio bar. Display nothing
199           row_values.push('')
200         end
201       else
202         alignment = (columns[index][:align] == :right ? '' : '-')
203         cell_value = "%#{alignment}#{width}s" % row[index].to_s[0...width]
204         cell_value = colorize(cell_value, :bold, :brown) if columns[index][:highlight]
205         row_values.push(cell_value)
206       end
207     end
208     puts row_values.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ')
209   end
210 end
title(title) click to toggle source

Write the title of a report title The title to write

   # File lib/request_log_analyzer/output/fixed_width.rb
90 def title(title)
91   puts
92   puts colorize(title, :bold, :white)
93   line(:green)
94 end