visionmedia-terminal-table 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ === 1.0.5 / 2009-03-14
3
+
4
+ * Allowing nil to be passed to table for headings
5
+ * Revised doc to show that rows can be splatted now
6
+ * Misc refactoring
7
+
2
8
  === 1.0.3 / 2009-01-15
3
9
 
4
10
  * Moved yield or eval to Terminal::Table initialize where it belongs
data/Manifest CHANGED
@@ -1,19 +1,22 @@
1
+ examples/examples.rb
1
2
  History.rdoc
2
- Rakefile
3
- README.rdoc
4
- lib/terminal-table.rb
5
- lib/terminal-table/core_ext.rb
6
3
  lib/terminal-table/cell.rb
4
+ lib/terminal-table/core_ext.rb
7
5
  lib/terminal-table/heading.rb
6
+ lib/terminal-table/import.rb
8
7
  lib/terminal-table/table.rb
9
8
  lib/terminal-table/version.rb
10
- lib/terminal-table/import.rb
11
- spec/spec_helper.rb
9
+ lib/terminal-table.rb
10
+ Manifest
11
+ Rakefile
12
+ README.rdoc
13
+ spec/cell_spec.rb
12
14
  spec/core_ext_spec.rb
15
+ spec/import_spec.rb
16
+ spec/spec_helper.rb
13
17
  spec/table_spec.rb
14
- spec/cell_spec.rb
15
18
  tasks/docs.rake
16
19
  tasks/gemspec.rake
17
20
  tasks/spec.rake
21
+ terminal-table.gemspec
18
22
  Todo.rdoc
19
- Manifest
data/README.rdoc CHANGED
@@ -7,16 +7,16 @@ Simple, feature rich ASCII table generator.
7
7
 
8
8
  * Fast
9
9
  * Simple
10
- * With or without headings
11
- * Alignment of headings, cells, or columns
12
- * Easy modification of table chars (+, -, |)
10
+ * Optional headings
11
+ * Alignment of columns, headings, or cells
12
+ * Easy modification of table strings (+, -, |)
13
13
 
14
14
  == Examples:
15
15
 
16
16
  require 'rubygems'
17
17
  require 'terminal-table/import'
18
18
 
19
- puts table(['a', 'b'], [[1, 2], [3, 4]])
19
+ puts table(['a', 'b'], [1, 2], [3, 4])
20
20
 
21
21
  t = table ['a', 'b']
22
22
  t << [1, 2]
@@ -45,14 +45,14 @@ Simple, feature rich ASCII table generator.
45
45
  rows << ['Comments', 20]
46
46
  rows << ['Ruby', 70]
47
47
  rows << ['JavaScript', 30]
48
- puts table([nil, 'Lines'], rows)
48
+ puts table([nil, 'Lines'], *rows)
49
49
 
50
50
  rows = []
51
51
  rows << ['Lines', 100]
52
52
  rows << ['Comments', 20]
53
53
  rows << ['Ruby', 70]
54
54
  rows << ['JavaScript', 30]
55
- puts table(nil, rows)
55
+ puts table(nil, *rows)
56
56
 
57
57
  == Results:
58
58
 
@@ -106,7 +106,7 @@ Simple, feature rich ASCII table generator.
106
106
 
107
107
  (The MIT License)
108
108
 
109
- Copyright (c) 2008 TJ Holowaychuk <tj@vision-media.ca>
109
+ Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
110
110
 
111
111
  Permission is hereby granted, free of charge, to any person obtaining
112
112
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake'
4
4
  require 'echoe'
5
5
  require './lib/terminal-table.rb'
6
6
 
7
- Echoe.new("terminal-table", Terminal::Table::VERSION::STRING) do |p|
7
+ Echoe.new("terminal-table", Terminal::Table::VERSION) do |p|
8
8
  p.author = "TJ Holowaychuk"
9
9
  p.email = "tj@vision-media.ca"
10
10
  p.summary = "Simple,"
data/Todo.rdoc CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  == Minor:
7
7
 
8
+ * Programmatically add seperator rows
9
+ * Arbitrary dimensions
8
10
  * Add multi-column sorting
9
11
  * Change; pre-create Cell and Heading objects to clean up Table a bit
10
12
 
@@ -0,0 +1,57 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'terminal-table/import'
4
+
5
+ puts
6
+ puts table(['a', 'b'], [1, 2], [3, 4])
7
+
8
+ puts
9
+ t = table ['a', 'b']
10
+ t << [1, 2]
11
+ t << [3, 4]
12
+ puts t
13
+
14
+ puts
15
+ user_table = table do |t|
16
+ t.headings = 'First Name', 'Last Name', 'Email'
17
+ t << %w( TJ Holowaychuk tj@vision-media.ca )
18
+ t << %w( Bob Someone bob@vision-media.ca )
19
+ t << %w( Joe Whatever bob@vision-media.ca )
20
+ end
21
+ puts user_table
22
+
23
+ puts
24
+ user_table = table do
25
+ self.headings = 'First Name', 'Last Name', 'Email'
26
+ add_row ['TJ', 'Holowaychuk', ['tj@vision-media.ca', :right]]
27
+ add_row ['Bob', 'Someone', 'bob@vision-media.ca']
28
+ add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
29
+ align_column 1, :center
30
+ end
31
+ puts user_table
32
+
33
+ rows = []
34
+ rows << ['Lines', 100]
35
+ rows << ['Comments', 20]
36
+ rows << ['Ruby', 70]
37
+ rows << ['JavaScript', 30]
38
+ puts table([nil, 'Lines'], *rows)
39
+
40
+ rows = []
41
+ rows << ['Lines', 100]
42
+ rows << ['Comments', 20]
43
+ rows << ['Ruby', 70]
44
+ rows << ['JavaScript', 30]
45
+ puts table(nil, *rows)
46
+
47
+
48
+ Terminal::Table::X = '='
49
+ Terminal::Table::Y = '!'
50
+ Terminal::Table::I = '*'
51
+
52
+ rows = []
53
+ rows << [nil, nil, nil]
54
+ rows << [nil, ':)', nil]
55
+ rows << [nil, nil, nil]
56
+ puts table(nil, *rows)
57
+
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2008 TJ Holowaychuk
2
+ # Copyright (c) 2008-2009 TJ Holowaychuk <tj@vision-media.ca>
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -3,24 +3,14 @@ module Terminal
3
3
  class Table
4
4
  class Cell
5
5
 
6
- DEFAULT_ALIGNMENT = :left
7
-
8
6
  attr_accessor :value, :alignment
9
7
 
10
- def initialize render_length, initial = nil
11
- @render_length = render_length
12
- case initial
13
- when Hash
14
- @value = initial[:value]
15
- @alignment = initial[:align] unless initial[:align].nil?
16
- when
17
- @value = initial
18
- @alignment = DEFAULT_ALIGNMENT
19
- end
8
+ def initialize width, value = nil, alignment = :left
9
+ @width, @alignment, @value = width, alignment, value
20
10
  end
21
11
 
22
12
  def render
23
- " #{value.to_s} ".align alignment, @render_length + 2
13
+ " #{value.to_s} ".align alignment, @width + 2
24
14
  end
25
15
  alias :to_s :render
26
16
 
@@ -7,17 +7,15 @@ class String
7
7
  def align position, length
8
8
  send position, length
9
9
  end
10
-
11
10
  alias_method :left, :ljust
12
11
  alias_method :right, :rjust
12
+
13
13
  end
14
14
 
15
15
  module Enumerable
16
16
  def map_with_index &block
17
17
  result = []
18
- each_with_index do |v, i|
19
- result << yield(v, i)
20
- end
18
+ each_with_index { |v, i| result << yield(v, i) }
21
19
  result
22
20
  end
23
21
  alias :collect_with_index :map_with_index
@@ -38,4 +36,5 @@ class Object
38
36
  end
39
37
  end
40
38
  end
39
+
41
40
  end
@@ -2,7 +2,9 @@
2
2
  module Terminal
3
3
  class Table
4
4
  class Heading < Cell
5
- DEFAULT_ALIGNMENT = :center
5
+ def initialize width, value = nil, alignment = :center
6
+ super
7
+ end
6
8
  end
7
9
  end
8
10
  end
@@ -8,7 +8,7 @@ module Kernel
8
8
  #
9
9
  # === Examples:
10
10
  #
11
- # puts table(['a', 'b'], [[1, 2], [3, 4]])
11
+ # puts table(['a', 'b'], [1, 2], [3, 4])
12
12
  #
13
13
  # # OR
14
14
  #
@@ -44,10 +44,10 @@ module Kernel
44
44
  # rows << ['Comments', 20]
45
45
  # rows << ['Ruby', 70]
46
46
  # rows << ['JavaScript', 30]
47
- # puts table(nil, rows)
47
+ # puts table(nil, *rows)
48
48
  #
49
49
 
50
- def table headings = [], rows = [], &block
51
- table = Terminal::Table.new :headings => headings, :rows => rows, &block
50
+ def table headings = [], *rows, &block
51
+ Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
52
52
  end
53
53
  end
@@ -46,8 +46,8 @@ module Terminal
46
46
  # Generates a ASCII table.
47
47
 
48
48
  def initialize options = {}, &block
49
- @headings = options.delete(:headings) || []
50
- @rows = options.delete(:rows) || []
49
+ @headings = options.fetch :headings, []
50
+ @rows = options.fetch :rows, []
51
51
  yield_or_eval &block if block_given?
52
52
  end
53
53
 
@@ -56,12 +56,19 @@ module Terminal
56
56
  # instance in order to output it to the terminal.
57
57
 
58
58
  def render
59
- sep = seperator
60
- s = sep + "\n"
61
- s << Y + headings.collect_with_index { |h, i| Heading.new(length_of_column(i), h).render }.join(Y) + Y if has_headings?
62
- s << "\n" + sep + "\n" if has_headings?
63
- s << rows.collect { |row| Y + row.collect_with_index { |c, i| Cell.new(length_of_column(i), c).render }.join(Y) + Y }.join("\n")
64
- s << "\n" + sep + "\n"
59
+ buffer = seperator << "\n"
60
+ if has_headings?
61
+ buffer << Y + headings.map_with_index do |heading, i|
62
+ Heading.new(length_of_column(i), *heading).render
63
+ end.join(Y) + Y
64
+ buffer << "\n#{seperator}\n"
65
+ end
66
+ buffer << rows.map do |row|
67
+ Y + row.map_with_index do |cell, i|
68
+ Cell.new(length_of_column(i), *cell).render
69
+ end.join(Y) + Y
70
+ end.join("\n")
71
+ buffer << "\n#{seperator}\n"
65
72
  end
66
73
  alias :to_s :render
67
74
 
@@ -69,7 +76,9 @@ module Terminal
69
76
  # Create a seperator based on colum lengths.
70
77
 
71
78
  def seperator
72
- I + columns.collect_with_index { |col, i| X * (length_of_column(i) + 2) }.join(I) + I
79
+ I + columns.collect_with_index do |col, i|
80
+ X * (length_of_column(i) + 2)
81
+ end.join(I) + I
73
82
  end
74
83
 
75
84
  ##
@@ -91,28 +100,28 @@ module Terminal
91
100
  # Return +n+ column.
92
101
 
93
102
  def column n
94
- rows.collect { |row| row[n] }.compact
103
+ rows.map { |row| row[n] }.compact
95
104
  end
96
105
 
97
106
  ##
98
107
  # Return +n+ column including headings.
99
108
 
100
109
  def column_with_headings n
101
- headings_with_rows.collect { |row| row[n] }.compact
110
+ headings_with_rows.map { |row| row[n] }.compact
102
111
  end
103
112
 
104
113
  ##
105
114
  # Return columns.
106
115
 
107
116
  def columns
108
- (0..number_of_columns-1).collect { |n| column n }
117
+ (0..number_of_columns-1).map { |n| column n }
109
118
  end
110
119
 
111
120
  ##
112
121
  # Return the largest cell found within column +n+.
113
122
 
114
123
  def largest_cell_in_column n
115
- column_with_headings(n).sort_by { |c| Cell.new(0, c).length }.last
124
+ column_with_headings(n).sort_by { |cell| Cell.new(0, *cell).length }.last
116
125
  end
117
126
 
118
127
  ##
@@ -126,26 +135,19 @@ module Terminal
126
135
  # Return total number of columns available.
127
136
 
128
137
  def number_of_columns
129
- if rows[0]
130
- rows[0].length
131
- else
132
- raise Error, 'Your table needs some rows'
133
- end
138
+ return rows.first.length unless rows.empty?
139
+ raise Error, 'your table needs some rows.'
134
140
  end
135
141
 
136
142
  ##
137
143
  # Align column +n+ to +alignment+ of :center, :left, or :right.
138
144
 
139
145
  def align_column n, alignment
140
- column(n).each_with_index do |c, i|
141
- unless c.is_a? Hash
142
- @rows[i][n] = { :value => c, :align => alignment }
143
- end
146
+ column(n).each_with_index do |col, i|
147
+ @rows[i][n] = [col, alignment] unless col.is_a? Array
144
148
  end
145
149
  end
146
150
 
147
- private
148
-
149
151
  ##
150
152
  # Return headings combined with rows.
151
153
 
@@ -1,9 +1,6 @@
1
1
 
2
2
  module Terminal
3
3
  class Table
4
- module VERSION #:nodoc:
5
- MAJOR, MINOR, TINY = [1, 0, 4]
6
- STRING = [MAJOR, MINOR, TINY].join '.'
7
- end
4
+ VERSION = '1.0.5'
8
5
  end
9
6
  end
data/spec/cell_spec.rb CHANGED
@@ -3,16 +3,16 @@ describe Terminal::Table do
3
3
 
4
4
  Cell = Terminal::Table::Cell
5
5
 
6
- it "should initialize with a string" do
7
- cell = Cell.new 0, 'Foo'
8
- cell.value.should == 'Foo'
6
+ it "should default alignment to the left" do
7
+ cell = Cell.new 0, 'foo'
8
+ cell.value.should == 'foo'
9
9
  cell.alignment.should == :left
10
10
  end
11
-
12
- it "should initialize with a hash" do
13
- cell = Cell.new 0, :value => 'Bar', :align => :right
14
- cell.value.should == 'Bar'
15
- cell.alignment.should == :right
11
+
12
+ it "should override alignment" do
13
+ cell = Cell.new 0, 'foo', :center
14
+ cell.value.should == 'foo'
15
+ cell.alignment.should == :center
16
16
  end
17
17
 
18
18
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  describe String do
3
+
3
4
  describe "#align" do
4
-
5
5
  it "should center" do
6
6
  'foo'.align(:center, 10).should == ' foo '
7
7
  end
@@ -13,6 +13,6 @@ describe String do
13
13
  it "should align right" do
14
14
  'foo'.align(:right, 10).should == ' foo'
15
15
  end
16
-
17
16
  end
18
- end
17
+
18
+ end
@@ -0,0 +1,13 @@
1
+
2
+ require 'terminal-table/import'
3
+
4
+ describe Kernel do
5
+
6
+ describe "#table" do
7
+ it "should allow creation of a terminal table" do
8
+ table(['foo', 'bar'], ['a', 'b'], [1, 2]).
9
+ should be_instance_of(Terminal::Table)
10
+ end
11
+ end
12
+
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,4 +5,4 @@ class String
5
5
  def deindent
6
6
  gsub /^ */, ''
7
7
  end
8
- end
8
+ end
data/spec/table_spec.rb CHANGED
@@ -59,6 +59,10 @@ describe Terminal::Table do
59
59
  @table << ['a', 1]
60
60
  @table.seperator.should == '+------+-----+'
61
61
  end
62
+
63
+ it "should bitch and complain when you have no rows" do
64
+ lambda { @table.render }.should raise_error(Terminal::Table::Error)
65
+ end
62
66
 
63
67
  it "should render properly" do
64
68
  @table.headings = ['Char', 'Num']
@@ -148,8 +152,8 @@ describe Terminal::Table do
148
152
  end
149
153
 
150
154
  it "should allow alignment of headings and cells" do
151
- @table.headings = ['Characters', { :value => 'Nums', :align => :right }]
152
- @table << [{ :value => 'a', :align => :center }, 1]
155
+ @table.headings = ['Characters', ['Nums', :right ]]
156
+ @table << [['a', :center ], 1]
153
157
  @table << ['b', 222222222222222]
154
158
  @table << ['c', 3]
155
159
  @table.render.should == <<-EOF.deindent
@@ -166,7 +170,7 @@ describe Terminal::Table do
166
170
 
167
171
  it "should align columns, but allow specifics to remain" do
168
172
  @table.headings = ['Just some characters', 'Num']
169
- @table.rows = [[{ :value => 'a', :align => :left }, 1], ['b', 2], ['c', 3]]
173
+ @table.rows = [[['a', :left], 1], ['b', 2], ['c', 3]]
170
174
  @table.align_column 0, :center
171
175
  @table.align_column 1, :right
172
176
  @table.render.should == <<-EOF.deindent
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{terminal-table}
5
- s.version = "1.0.4"
5
+ s.version = "1.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
- s.date = %q{2009-01-15}
9
+ s.date = %q{2009-03-14}
10
10
  s.description = %q{Simple,}
11
11
  s.email = %q{tj@vision-media.ca}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table/import.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
- s.files = ["History.rdoc", "Rakefile", "README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table/import.rb", "spec/spec_helper.rb", "spec/core_ext_spec.rb", "spec/table_spec.rb", "spec/cell_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc", "Manifest", "terminal-table.gemspec"]
12
+ s.extra_rdoc_files = ["lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["examples/examples.rb", "History.rdoc", "lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/heading.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table.rb", "Manifest", "Rakefile", "README.rdoc", "spec/cell_spec.rb", "spec/core_ext_spec.rb", "spec/import_spec.rb", "spec/spec_helper.rb", "spec/table_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "terminal-table.gemspec", "Todo.rdoc"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/visionmedia/terminal-table}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terminal-table", "--main", "README.rdoc"]
@@ -24,11 +24,8 @@ Gem::Specification.new do |s|
24
24
  s.specification_version = 2
25
25
 
26
26
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
- s.add_development_dependency(%q<echoe>, [">= 0"])
28
27
  else
29
- s.add_dependency(%q<echoe>, [">= 0"])
30
28
  end
31
29
  else
32
- s.add_dependency(%q<echoe>, [">= 0"])
33
30
  end
34
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-terminal-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,18 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-15 00:00:00 -08:00
12
+ date: 2009-03-14 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: echoe
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: "0"
23
- version:
14
+ dependencies: []
15
+
24
16
  description: Simple,
25
17
  email: tj@vision-media.ca
26
18
  executables: []
@@ -28,38 +20,40 @@ executables: []
28
20
  extensions: []
29
21
 
30
22
  extra_rdoc_files:
31
- - README.rdoc
32
- - lib/terminal-table.rb
33
- - lib/terminal-table/core_ext.rb
34
23
  - lib/terminal-table/cell.rb
24
+ - lib/terminal-table/core_ext.rb
35
25
  - lib/terminal-table/heading.rb
26
+ - lib/terminal-table/import.rb
36
27
  - lib/terminal-table/table.rb
37
28
  - lib/terminal-table/version.rb
38
- - lib/terminal-table/import.rb
29
+ - lib/terminal-table.rb
30
+ - README.rdoc
39
31
  - tasks/docs.rake
40
32
  - tasks/gemspec.rake
41
33
  - tasks/spec.rake
42
34
  files:
35
+ - examples/examples.rb
43
36
  - History.rdoc
44
- - Rakefile
45
- - README.rdoc
46
- - lib/terminal-table.rb
47
- - lib/terminal-table/core_ext.rb
48
37
  - lib/terminal-table/cell.rb
38
+ - lib/terminal-table/core_ext.rb
49
39
  - lib/terminal-table/heading.rb
40
+ - lib/terminal-table/import.rb
50
41
  - lib/terminal-table/table.rb
51
42
  - lib/terminal-table/version.rb
52
- - lib/terminal-table/import.rb
53
- - spec/spec_helper.rb
43
+ - lib/terminal-table.rb
44
+ - Manifest
45
+ - Rakefile
46
+ - README.rdoc
47
+ - spec/cell_spec.rb
54
48
  - spec/core_ext_spec.rb
49
+ - spec/import_spec.rb
50
+ - spec/spec_helper.rb
55
51
  - spec/table_spec.rb
56
- - spec/cell_spec.rb
57
52
  - tasks/docs.rake
58
53
  - tasks/gemspec.rake
59
54
  - tasks/spec.rake
60
- - Todo.rdoc
61
- - Manifest
62
55
  - terminal-table.gemspec
56
+ - Todo.rdoc
63
57
  has_rdoc: true
64
58
  homepage: http://github.com/visionmedia/terminal-table
65
59
  post_install_message: