Class: RequestLogAnalyzer::Output::FixedWidth
- Defined in:
- lib/request_log_analyzer/output/fixed_width.rb
Overview
Fixed Width output class. Outputs a fixed width ASCII or UF8 report.
Defined Under Namespace
Modules: Color, Monochrome
Constant Summary collapse
- CHARACTERS =
{ :ascii => { :horizontal_line => '-', :vertical_line => '|', :block => '=' }, :utf => { :horizontal_line => '━', :vertical_line => '┃', :block => '░' } }
Instance Attribute Summary collapse
-
#characters ⇒ Object
readonly
Returns the value of attribute characters.
Attributes inherited from Base
Instance Method Summary collapse
-
#footer ⇒ Object
Generate a footer for a report.
-
#header ⇒ Object
Generate a header for a report.
-
#initialize(io, options = {}) ⇒ FixedWidth
constructor
Initialize a report
io
iO Object (file, STDOUT, etc.)options
*:characters
:utf for UTF8 or :ascii for ANSI compatible output. -
#line(*font) ⇒ Object
Write a line.
-
#link(text, url = nil) ⇒ Object
Write a link
text
The text in the link, or the URL itself if no text is givenurl
The url to link to. -
#print(str) ⇒ Object
(also: #<<)
Write a string to the output object.
-
#puts(str = '') ⇒ Object
Write a string to the output object with a newline at the end.
-
#table(*columns) {|rows| ... } ⇒ Object
Generate a report table and push it into the output object.
-
#title(title) ⇒ Object
Write the title of a report
title
The title to write.
Methods inherited from Base
Constructor Details
#initialize(io, options = {}) ⇒ FixedWidth
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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 69 def initialize(io, = {}) super(io, ) @options[:width] ||= 80 @options[:characters] ||= :utf @characters = CHARACTERS[@options[:characters]] color_module = @options[:color] ? Color : Monochrome (class << self; self; end).send(:include, color_module) end |
Instance Attribute Details
#characters ⇒ Object (readonly)
Returns the value of attribute characters.
56 57 58 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 56 def characters @characters end |
Instance Method Details
#footer ⇒ Object
Generate a footer for a report
128 129 130 131 132 133 134 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 128 def puts puts "Need an expert to analyze your application?" puts "Mail to #{link('[email protected]')} or visit us at #{link('http://railsdoctors.com')}." line(:green) puts "Thanks for using #{colorize('request-log-analyzer', :white, :bold)}!" end |
#header ⇒ Object
Generate a header for a report
118 119 120 121 122 123 124 125 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 118 def header if io.kind_of?(File) puts colorize("Request-log-analyzer summary report", :white, :bold) line(:green) puts "Version #{RequestLogAnalyzer::VERSION} - written by Willem van Bergen and Bart ten Brinke" puts "Website: #{link('http://github.com/wvanbergen/request-log-analyzer')}" end end |
#line(*font) ⇒ Object
Write a line
102 103 104 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 102 def line(*font) puts colorize(characters[:horizontal_line] * @options[:width], *font) end |
#link(text, url = nil) ⇒ Object
Write a link text
The text in the link, or the URL itself if no text is given url
The url to link to.
109 110 111 112 113 114 115 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 109 def link(text, url = nil) if url.nil? colorize(text, :blue, :bold) else "#{text} (#{colorize(url, :blue, :bold)})" end end |
#print(str) ⇒ Object Also known as: <<
Write a string to the output object. str
The string to write.
81 82 83 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 81 def print(str) @io << str end |
#puts(str = '') ⇒ Object
Write a string to the output object with a newline at the end. str
The string to write.
89 90 91 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 89 def puts(str = '') @io << str << "\n" end |
#table(*columns) {|rows| ... } ⇒ Object
Generate a report table and push it into the output object. *colums<tt> Columns hash <tt>&block
: A block yeilding the rows.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 139 def table(*columns, &block) rows = Array.new yield(rows) # determine maximum cell widths max_cell_widths = rows.inject(Array.new(columns.length, 0)) do |result, row| lengths = row.map { |column| column.to_s.length } result.each_with_index { |length, index| result[index] = ([length, lengths[index]].max rescue length) } end columns.each_with_index { |col, index| col[:actual_width] ||= max_cell_widths[index] } # determine actual column width column_widths = columns.map do |column| if column[:width] == :rest nil elsif column[:width] column[:width] elsif column[:min_width] [column[:min_width], column[:actual_width]].max elsif column[:max_width] [column[:max_width], column[:actual_width]].min else column[:actual_width] end end if column_widths.include?(nil) fill_column = columns[column_widths.index(nil)] width_left = [:width] - ((columns.length - 1) * (style[:cell_separator] ? 3 : 1)) - column_widths.compact.inject(0) { |sum, col| sum + col} column_widths[column_widths.index(nil)] = case fill_column[:type] when :ratio; width_left # max out else; [width_left, fill_column[:actual_width]].min end end # Print table header if table_has_header?(columns) column_titles = [] columns.each_with_index do |column, index| width = column_widths[index] alignment = (column[:align] == :right ? '' : '-') column_titles.push(colorize("%#{alignment}#{width}s" % column[:title].to_s[0...width], :bold)) end puts column_titles.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ') line(:green) end # Print the rows rows.each do |row| row_values = [] columns.each_with_index do |column, index| width = column_widths[index] case column[:type] when :ratio if width > 4 if column[:treshold] && column[:treshold] < row[index].to_f = '' << characters[:block] * (width.to_f * column[:treshold]).round << colorize(characters[:block] * (width.to_f * (row[index].to_f - column[:treshold])).round, :red) row_values.push() else # Create a bar by combining block characters row_values.push(characters[:block] * (width.to_f * row[index].to_f).round) end else # Too few characters for a ratio bar. Display nothing row_values.push('') end else alignment = (columns[index][:align] == :right ? '' : '-') cell_value = "%#{alignment}#{width}s" % row[index].to_s[0...width] cell_value = colorize(cell_value, :bold, :brown) if columns[index][:highlight] row_values.push(cell_value) end end puts row_values.join(style[:cell_separator] ? " #{characters[:vertical_line]} " : ' ') end end |
#title(title) ⇒ Object
Write the title of a report title
The title to write
95 96 97 98 99 |
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 95 def title(title) puts puts colorize(title, :bold, :white) line(:green) end |