2008-06-12 02:00:35 -04:00
# Results formatter
module Formatter
2008-06-17 19:58:05 -04:00
# Base abstract class for results formatting.
2008-06-12 02:00:35 -04:00
class Formatter
2009-09-22 23:10:49 -04:00
def is_kernel? ( obj )
obj . kind_of? ( Module ) and obj . name == " Kernel "
end
2008-06-17 19:58:05 -04:00
# Takes an output stream and a print width.
2009-09-22 23:10:49 -04:00
def initialize ( opts = { } )
defaults = { :output_stream = > Kernel , :format_width = > 100 }
options = defaults . merge ( opts )
@out = options [ :output_stream ]
raise TypeError . new ( " Type %s of parameter %s is not IO " % [ @out . class , @out ] ) \
unless @out . instance_of? IO or is_kernel? ( @out )
@maxWidth = options [ :format_width ]
2008-06-12 02:00:35 -04:00
@rowCount = 0
end
2008-06-17 19:58:05 -04:00
attr_reader :rowCount
2009-04-22 15:14:15 -04:00
def header ( args = [ ] , widths = [ ] )
row ( args , false , widths ) if args . length > 0
2008-06-12 02:00:35 -04:00
@rowCount = 0
end
2008-06-17 19:58:05 -04:00
# Output a row.
# Inset is whether or not to offset row by a space.
2009-04-22 15:14:15 -04:00
def row ( args = [ ] , inset = true , widths = [ ] )
2008-06-12 02:00:35 -04:00
if not args or args . length == 0
# Print out nothing
return
end
2008-06-17 19:58:05 -04:00
if args . class == String
output ( @maxWidth , args )
2009-09-22 23:10:49 -04:00
@out . puts
2008-06-17 19:58:05 -04:00
return
end
2008-06-12 02:00:35 -04:00
# TODO: Look at the type. Is it RowResult?
if args . length == 1
splits = split ( @maxWidth , dump ( args [ 0 ] ) )
for l in splits
output ( @maxWidth , l )
2009-09-22 23:10:49 -04:00
@out . puts
2008-06-12 02:00:35 -04:00
end
elsif args . length == 2
2009-04-22 15:14:15 -04:00
col1width = ( not widths or widths . length == 0 ) ? @maxWidth / 4 : @maxWidth * widths [ 0 ] / 100
col2width = ( not widths or widths . length < 2 ) ? @maxWidth - col1width - 2 : @maxWidth * widths [ 1 ] / 100 - 2
2008-06-12 02:00:35 -04:00
splits1 = split ( col1width , dump ( args [ 0 ] ) )
splits2 = split ( col2width , dump ( args [ 1 ] ) )
biggest = ( splits2 . length > splits1 . length ) ? splits2 . length : splits1 . length
index = 0
while index < biggest
2008-06-17 19:58:05 -04:00
if inset
# Inset by one space if inset is set.
@out . print ( " " )
end
2008-06-12 02:00:35 -04:00
output ( col1width , splits1 [ index ] )
2008-06-17 19:58:05 -04:00
if not inset
# Add extra space so second column lines up w/ second column output
@out . print ( " " )
end
2008-06-12 02:00:35 -04:00
@out . print ( " " )
output ( col2width , splits2 [ index ] )
index += 1
2009-09-22 23:10:49 -04:00
@out . puts
2008-06-12 02:00:35 -04:00
end
else
# Print a space to set off multi-column rows
print ' '
first = true
for e in args
@out . print " " unless first
first = false
@out . print e
end
puts
end
@rowCount += 1
end
def split ( width , str )
result = [ ]
index = 0
while index < str . length do
2008-07-09 17:29:46 -04:00
result << str . slice ( index , width )
2008-06-12 02:00:35 -04:00
index += width
end
result
end
def dump ( str )
2008-06-17 19:58:05 -04:00
if str . instance_of? Fixnum
return
end
2008-09-27 17:58:38 -04:00
# Remove double-quotes added by 'dump'.
2010-01-06 21:25:12 -05:00
return str
2008-06-12 02:00:35 -04:00
end
def output ( width , str )
# Make up a spec for printf
2008-06-18 14:18:26 -04:00
spec = " %%-%ds " % width
@out . printf ( spec , str )
2008-06-12 02:00:35 -04:00
end
2008-08-12 14:30:33 -04:00
def footer ( startTime = nil , rowCount = nil )
if not rowCount
rowCount = @rowCount
end
2008-06-12 02:00:35 -04:00
if not startTime
return
end
# Only output elapsed time and row count if startTime passed
2008-08-12 14:30:33 -04:00
@out . puts ( " %d row(s) in %.4f seconds " % [ rowCount , Time . now - startTime ] )
2008-06-12 02:00:35 -04:00
end
end
class Console < Formatter
end
class XHTMLFormatter < Formatter
# http://www.germane-software.com/software/rexml/doc/classes/REXML/Document.html
# http://www.crummy.com/writing/RubyCookbook/test_results/75942.html
end
class JSON < Formatter
end
# Do a bit of testing.
if $0 == __FILE__
formatter = Console . new ( STDOUT )
2008-07-09 17:29:46 -04:00
now = Time . now
2008-06-12 02:00:35 -04:00
formatter . header ( [ 'a' , 'b' ] )
formatter . row ( [ 'a' , 'b' ] )
formatter . row ( [ 'xxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx' ] )
formatter . row ( [ 'yyyyyy yyyyyy yyyyy yyy' , 'xxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx xxx xx x xx xxx xx xx xx x xx x x xxx x x xxx x x xx x x x x x x xx ' ] )
2008-07-09 17:29:46 -04:00
formatter . row ( [ " NAME => 'table1', FAMILIES => [{NAME => 'fam2', VERSIONS => 3, COMPRESSION => 'NONE', IN_MEMORY => false, BLOCKCACHE => false, LENGTH => 2147483647, TTL => FOREVER, BLOOMFILTER => NONE}, {NAME => 'fam1', VERSIONS => 3, COMPRESSION => 'NONE', IN_MEMORY => false, BLOCKCACHE => false, LENGTH => 2147483647, TTL => FOREVER, BLOOMFILTER => NONE}] " ] )
formatter . footer ( now )
2008-06-12 02:00:35 -04:00
end
end