Permalink
Browse files

Use the terminal-table gem to output tables in the shell

  • Loading branch information...
1 parent 58cf498 commit c79b88c95a8cc640300f2a74f2b1af74c113d32c @smtlaissezfaire committed Oct 11, 2009
@@ -2,13 +2,7 @@ module Guillotine
module Shell
class OutputFormatter
dir = File.dirname(__FILE__) + "/output_formatter"
- autoload :ColumnOutputer, "#{dir}/column_outputer"
- autoload :ColumnDelimiterHeader, "#{dir}/column_delimiter_header"
- autoload :ColumnExtractor, "#{dir}/column_extractor"
- autoload :ColumnLengthCalculator, "#{dir}/column_length_calculator"
autoload :TableOutputer, "#{dir}/table_outputer"
- autoload :OutputBuffer, "#{dir}/output_buffer"
- autoload :RowAdder, "#{dir}/row_adder"
EMPTY_SET_STRING = "Empty set"
@@ -1,29 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- class ColumnDelimiterHeader < ColumnOutputer
- PLUS_SEPARATOR = "+"
- DASH = "-"
- CONTENT = DASH
-
- def self.output(column_size, end_column = false)
- new(column_size, end_column).output
- end
-
- def initialize(column_size, end_column = false)
- super(CONTENT * column_size, column_size, end_column)
- end
-
- private
-
- def padding
- DASH
- end
-
- def separator
- PLUS_SEPARATOR
- end
- end
- end
- end
-end
@@ -1,28 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- class ColumnExtractor
- class UnfoundKeyError < StandardError; end
-
- def extract(column_name, tbl)
- @column_name = column_name
- @table = tbl
-
- check_constraints
- {
- :column => column_name,
- :values => tbl.map { |row| row[column_name].to_s }
- }
- end
-
- private
-
- def check_constraints
- unless @table.first.keys.include?(@column_name)
- raise UnfoundKeyError, "Could not find the key '#{@column_name}'"
- end
- end
- end
- end
- end
-end
@@ -1,28 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- module ColumnLengthCalculator
- class << self
- def size_of_column(column, values)
- column_length = column_length(column)
- value_length = max_value_length(values)
-
- column_length > value_length ? column_length : value_length
- end
-
- private
-
- def column_length(column)
- column.to_s.length
- end
-
- def max_value_length(values)
- value_lengths = values.map { |value| value.length }
- value_lengths.sort.last
- end
- end
- end
- end
- end
-end
-
@@ -1,54 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- class ColumnOutputer
- def self.output(string, max_size, end_column = false)
- new(string, max_size, end_column).output
- end
-
- class InvalidMaxSizeError < StandardError; end
-
- BAR_SEPARATOR = "|"
- SPACE = " "
- EMPTY_STRING = ""
-
- def initialize(string, max_size, end_column = false)
- @string = string
- @max_size = max_size
- @end_column = end_column
- end
-
- def output
- "#{separator}#{padding}#{@string}#{spaces}#{padding}#{end_separator}"
- end
-
- private
-
- def spaces
- padding * number_of_spaces
- end
-
- def padding
- SPACE
- end
-
- def number_of_spaces
- space_count = @max_size - @string.length
- if space_count < 0
- raise(InvalidMaxSizeError,
- "The string '#{@string}' cannot fit into a column #{@max_size} chars wide")
- end
- space_count
- end
-
- def end_separator
- @end_column ? separator : EMPTY_STRING
- end
-
- def separator
- BAR_SEPARATOR
- end
- end
- end
- end
-end
@@ -1,44 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- class TableOutputer
- class OutputBuffer
- def initialize
- @separator = ""
- @column_names = ""
- @values = []
- end
-
- def to_s
- out = [
- @separator,
- @column_names,
- @separator,
- @values,
- @separator
- ].join("\n")
- out << "\n"
- end
-
- def add_separator(separator)
- @separator << separator
- end
-
- def add_column_name(column_name)
- @column_names << column_name
- end
-
- def add_values(values)
-# Waiting for ordered hashes. See README
-# @values = if @values.empty?
-# values
-# else
-# @values.zip(values).map { |row| row.join("") }
-# end
- @values = values
- end
- end
- end
- end
- end
-end
@@ -1,40 +0,0 @@
-module Guillotine
- module Shell
- class OutputFormatter
- class RowAdder
- def initialize(column_name, extractor, output_array)
- @column_name = column_name.to_s
- @extractor = extractor
- @out = output_array
- end
-
- def add(last_column)
- @last_column = last_column
- column_name = ColumnOutputer.output(@column_name, column_width, last_column)
-
- @out.add_separator(separator)
- @out.add_column_name(column_name)
- @out.add_values(column_values)
- @out
- end
-
- private
-
- def column_width
- ColumnLengthCalculator.size_of_column(@extractor[:column], @extractor[:values])
- end
-
- def separator
- ColumnDelimiterHeader.output(column_width, @last_column)
- end
-
- def column_values
- @extractor[:values].map do |value|
- ColumnOutputer.output(value, column_width, @last_column)
- end
- end
- end
- end
- end
-end
-
@@ -1,38 +1,29 @@
+require "terminal-table"
+
module Guillotine
module Shell
class OutputFormatter
class TableOutputer
+ include Terminal::Table::TableHelper
+
def initialize(table)
@table = table
- @out_buffer = OutputBuffer.new
end
def column_names
@column_names ||= @table.first.keys
end
def output
- last_column_name = column_names.last
- column_names.each do |column_name|
- add_column(column_name)
- end
- @out_buffer.to_s
- end
-
- def last_column_name
- @last_column_name ||= column_names.last
- end
-
- def last_column?(column_name)
- last_column_name == column_name
- end
-
- def add_column(column_name)
- RowAdder.new(column_name, extractor_for(column_name), @out_buffer).add(last_column?(column_name))
- end
-
- def extractor_for(column_name)
- @extractor = ColumnExtractor.new.extract(column_name, @table)
+ table do |t|
+ t.headings = column_names
+
+ @table.each do |record|
+ t << column_names.map do |col|
+ record[col]
+ end
+ end
+ end.to_s
end
end
end
@@ -1,23 +0,0 @@
-require File.dirname(__FILE__) + "/../../../spec_helper"
-
-module Guillotine
- module Shell
- describe OutputFormatter::ColumnDelimiterHeader do
- before(:each) do
- @outputer = OutputFormatter::ColumnDelimiterHeader
- end
-
- it "should output +--- when given the number 1" do
- @outputer.output(1).should == "+---"
- end
-
- it "should output +---- when given the number 2"do
- @outputer.output(2).should == "+----"
- end
-
- it "should output +---+ when passed '1' (char), and 'true' for being the last column" do
- @outputer.output(1, true).should == "+---+"
- end
- end
- end
-end
@@ -1,45 +0,0 @@
-require File.dirname(__FILE__) + "/../../../spec_helper"
-
-module Guillotine
- module Shell
- describe OutputFormatter::ColumnExtractor do
- before(:each) do
- @extractor = OutputFormatter::ColumnExtractor.new
- end
-
- it "should construct a hash with the column name" do
- @extractor.extract(:foo, [{ :foo => "bar" }]).should include(:column => :foo)
- end
-
- it "should use the proper column name" do
- @extractor.extract(:bar, [{ :bar => "bar"}]).should include(:column => :bar)
- end
-
- it "should raise an error if it can't find the key" do
- lambda {
- @extractor.extract(:bar, [{ :foo => "1"}])
- }.should raise_error(OutputFormatter::ColumnExtractor::UnfoundKeyError, "Could not find the key 'bar'")
- end
-
- it "should raise an error with the correct name" do
- lambda {
- @extractor.extract(:key_looking_for, [{ :foo => "1"}])
- }.should raise_error(OutputFormatter::ColumnExtractor::UnfoundKeyError, "Could not find the key 'key_looking_for'")
- end
-
- it "should return an array with one value" do
- @extractor.extract(:foo, [{ :foo => "foo" }]).should include(:values => ["foo"])
- end
-
- it "should return an array with two values" do
- result = @extractor.extract(:foo, [{ :foo => "foo" }, { :foo => "bar" }])
- result.should include(:values => ["foo", "bar"])
- end
-
- it "should to_s a value" do
- result = @extractor.extract(:foo, [{ :foo => 1 }])
- result.should include(:values => ["1"])
- end
- end
- end
-end
@@ -1,27 +0,0 @@
-require File.dirname(__FILE__) + "/../../../spec_helper"
-
-module Guillotine
- module Shell
- describe OutputFormatter::ColumnLengthCalculator do
- before(:each) do
- @calculator = OutputFormatter::ColumnLengthCalculator
- end
-
- it "should find the size of '3' with a column name one char long (the size + offset)" do
- @calculator.size_of_column(:a, [""]).should == 1
- end
-
- it "should find the size of '4' with a column name two chars long" do
- @calculator.size_of_column(:ab, [""]).should == 2
- end
-
- it "should find the size of '5' with a column name one char long, and a value of 3 chars" do
- @calculator.size_of_column(:a, ["123"]).should == 3
- end
-
- it "should pick the longest column out of a range of column values" do
- @calculator.size_of_column(:a, ["1", "12"]).should == 2
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit c79b88c

Please sign in to comment.