6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/hotdog/formatters/text.rb', line 6
def format(result, options={})
if options[:print0]
sep = "\0"
elsif options[:print1]
sep = newline
elsif options[:print2]
sep = " "
else
sep = " "
end
result = prepare(result)
if options[:print1] and options[:headers] and options[:fields]
field_length = (0...result.last.length).map { |field_index|
result.reduce(0) { |length, row|
[length, row[field_index].to_s.length, options[:fields][field_index].to_s.length].max
}
}
= options[:fields].zip(field_length).map { |field, length|
field.to_s + (" " * (length - field.length))
}
result = [
,
.map { |field|
"-" * field.length
},
] + result.map { |row|
row.zip(field_length).map { |field, length|
padding = length - ((field.nil?) ? 0 : field.to_s.length)
field.to_s + (" " * padding)
}
}
end
s = _format(result, sep, options)
if s.empty? or options[:print0]
s
else
s + newline
end
end
|