Class: Chimps::Utils::Typewriter
- Defined in:
- lib/chimps/utils/typewriter.rb
Overview
There are two Chimpanzees using a typewriter. One of them presses most of the keys and writes to $stdout. The other only hits the spacebar and writes to $stderr. He’s crazy.
These two Chimps together manage to line everything up just right.
Constant Summary collapse
- ROW_SEPARATOR =
Default row separator
"\n"
- COLUMN_SEPARATOR =
Default columnn separator
"\t"
Instance Attribute Summary collapse
-
#column_separator ⇒ Object
Separates columns.
-
#column_widths ⇒ Object
Widths of columns as determined by the maximum number of characters in any row.
-
#response ⇒ Object
The response that this Typewriter will print.
-
#row_separator ⇒ Object
Separates rows.
Instance Method Summary collapse
-
#accumulate(response) ⇒ Object
Accumulate lines to print from
string
. -
#initialize(response, options = {}) ⇒ Chimps::Typewriter
constructor
Return a Typewriter to print
response
. -
#print ⇒ Object
Print the accumulated lines in this Typewriter.
-
#spacer ⇒ Object
FIXME.
Constructor Details
#initialize(response, options = {}) ⇒ Chimps::Typewriter
Return a Typewriter to print response
.
40 41 42 43 44 45 46 47 |
# File 'lib/chimps/utils/typewriter.rb', line 40 def initialize response, ={} super() @response = response @column_widths = {} self.row_separator = ([:row_separator] || ROW_SEPARATOR) self.column_separator = ([:column_separator] || COLUMN_SEPARATOR) accumulate(response) end |
Instance Attribute Details
#column_separator ⇒ Object
Separates columns.
23 24 25 |
# File 'lib/chimps/utils/typewriter.rb', line 23 def column_separator @column_separator end |
#column_widths ⇒ Object
Widths of columns as determined by the maximum number of characters in any row.
17 18 19 |
# File 'lib/chimps/utils/typewriter.rb', line 17 def column_widths @column_widths end |
#response ⇒ Object
The response that this Typewriter will print.
13 14 15 |
# File 'lib/chimps/utils/typewriter.rb', line 13 def response @response end |
#row_separator ⇒ Object
Separates rows.
20 21 22 |
# File 'lib/chimps/utils/typewriter.rb', line 20 def row_separator @row_separator end |
Instance Method Details
#accumulate(response) ⇒ Object
Accumulate lines to print from string
.
Updates internal width counters as it accumulates
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chimps/utils/typewriter.rb', line 78 def accumulate response response.body.strip.split(row_separator).each do |line| self << [].tap do |row| line.split(column_separator).each_with_index do |entry, field| column_widths[field] = entry.size if entry.size > (column_widths[field] || 0) row << entry end end end end |
#print ⇒ Object
Print the accumulated lines in this Typewriter.
Will first calculate appropriate column widths for each line and then pad with spaces each entry so that the columns line up.
The spaces are written to $stderr and the rest of the characters to $stdout. This lets you pipe output from a Typewriter into other processes and preserve the TSV structure.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chimps/utils/typewriter.rb', line 57 def print $stdout.sync = true ; $stderr.sync = true each do |row| row.each_with_index do |entry, field| $stdout.write entry max_width = column_widths[field] + spacer unless entry.size >= max_width num_spaces = max_width - entry.size pad = " " * num_spaces $stderr.write(pad) end end $stdout.write "\n" end end |