terminal-table 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,8 @@
1
1
 
2
+ === 1.3.0 / 2009-10-16
3
+
4
+ * Major refactoring (functionality remains the same)
5
+
2
6
  === 1.2.0 / 2009-08-06
3
7
 
4
8
  * Added colspan support to table
data/Manifest CHANGED
@@ -1,5 +1,10 @@
1
- examples/examples.rb
2
1
  History.rdoc
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ Todo.rdoc
6
+ examples/examples.rb
7
+ lib/terminal-table.rb
3
8
  lib/terminal-table/cell.rb
4
9
  lib/terminal-table/core_ext.rb
5
10
  lib/terminal-table/heading.rb
@@ -7,19 +12,13 @@ lib/terminal-table/import.rb
7
12
  lib/terminal-table/table.rb
8
13
  lib/terminal-table/table_helper.rb
9
14
  lib/terminal-table/version.rb
10
- lib/terminal-table.rb
11
- Manifest
12
- Rakefile
13
- README.rdoc
14
15
  spec/cell_spec.rb
15
16
  spec/core_ext_spec.rb
16
17
  spec/import_spec.rb
17
18
  spec/spec.opts
18
19
  spec/spec_helper.rb
19
- spec/table_helper_spec.rb
20
20
  spec/table_spec.rb
21
21
  tasks/docs.rake
22
22
  tasks/gemspec.rake
23
23
  tasks/spec.rake
24
24
  terminal-table.gemspec
25
- Todo.rdoc
@@ -54,6 +54,7 @@ rows << ['JavaScript', 30]
54
54
  puts table(nil, *rows)
55
55
 
56
56
 
57
+ $VERBOSE = nil
57
58
  Terminal::Table::X = '='
58
59
  Terminal::Table::Y = '!'
59
60
  Terminal::Table::I = '*'
@@ -3,29 +3,50 @@ module Terminal
3
3
  class Table
4
4
  class Cell
5
5
 
6
- attr_accessor :value, :alignment, :colspan
6
+ ##
7
+ # Cell width.
7
8
 
8
- def initialize width, params
9
+ attr_reader :width
10
+
11
+ ##
12
+ # Cell value.
13
+
14
+ attr_reader :value
15
+
16
+ ##
17
+ # Cell alignment.
18
+
19
+ attr_reader :alignment
20
+
21
+ ##
22
+ # Column span.
23
+
24
+ attr_reader :colspan
25
+
26
+ ##
27
+ # Initialize with _width_ and _options_.
28
+
29
+ def initialize width, options = nil
9
30
  @width = width
10
- @alignment = :left
11
- @colspan = 1
12
-
13
- if params.is_a? Hash
14
- @value = params[:value]
15
- @alignment = params[:alignment] unless params[:alignment].nil?
16
- @colspan = params[:colspan] unless params[:colspan].nil?
17
- else
18
- @value = params
19
- end
31
+ @value, options = options, {} unless Hash === options
32
+ @value = options.fetch :value, value
33
+ @alignment = options.fetch :alignment, :left
34
+ @colspan = options.fetch :colspan, 1
20
35
  end
21
36
 
37
+ ##
38
+ # Render the cell.
39
+
22
40
  def render
23
- " #{value.to_s} ".align alignment, @width + 2
41
+ " #{value} ".align alignment, width + 2
24
42
  end
25
43
  alias :to_s :render
26
44
 
45
+ ##
46
+ # Cell length.
47
+
27
48
  def length
28
- @value.to_s.length + 2
49
+ value.to_s.length + 2
29
50
  end
30
51
  end
31
52
  end
@@ -1,32 +1,22 @@
1
1
 
2
2
  class String
3
-
4
- ##
5
- # Align to +position+, which may be :left, :right, or :center.
6
-
7
3
  def align position, length
8
4
  send position, length
9
5
  end
10
6
  alias_method :left, :ljust
11
7
  alias_method :right, :rjust
12
-
13
8
  end
14
9
 
15
10
  module Enumerable
16
11
  def map_with_index &block
17
- result = []
18
- each_with_index { |v, i| result << yield(v, i) }
19
- result
12
+ vals = []
13
+ each_with_index { |v, i| vals << yield(v, i) }
14
+ vals
20
15
  end
21
16
  alias :collect_with_index :map_with_index
22
17
  end
23
18
 
24
19
  class Object
25
-
26
- ##
27
- # Yields or instance_eval's a +block+ depending on the arity of a block
28
- # in order to support both types of block syntax for DSL's.
29
-
30
20
  def yield_or_eval &block
31
21
  return unless block
32
22
  if block.arity > 0
@@ -35,5 +25,4 @@ class Object
35
25
  self.instance_eval &block
36
26
  end
37
27
  end
38
-
39
28
  end
@@ -1,5 +1,4 @@
1
+
1
2
  require 'terminal-table'
2
3
 
3
- class Object
4
- include Terminal::Table::TableHelper
5
- end
4
+ include Terminal::Table::TableHelper
@@ -1,32 +1,5 @@
1
1
 
2
2
  module Terminal
3
-
4
- ##
5
- # Generates an ASCII table for terminal usage.
6
- #
7
- # This class can be used in many ways, however ultimately you should
8
- # require 'terminal-table/import' (which requires terminal-table itself) to use
9
- # the Kernel helper method which is easier, and cleaner to use. View Object#table
10
- # for examples.
11
- #
12
- # === Examples:
13
- #
14
- # table = Terminal::Table.new
15
- # table.headings = ['Characters', { :value => 'Nums', :align => :right }]
16
- # table << [{ :value => 'a', :align => :center }, 1]
17
- # table << ['b', 222222222222222]
18
- # table << ['c', 3]
19
- # puts table.render # or simply puts table
20
- #
21
- # +------------+-----------------+
22
- # | Characters | Nums |
23
- # +------------+-----------------+
24
- # | a | 1 |
25
- # | b | 222222222222222 |
26
- # | c | 3 |
27
- # +------------+-----------------+
28
- #
29
-
30
3
  class Table
31
4
 
32
5
  #--
@@ -40,48 +13,55 @@ module Terminal
40
13
 
41
14
  X, Y, I = '-', '|', '+'
42
15
 
43
- attr_accessor :headings, :rows
16
+ ##
17
+ # Headings array.
18
+
19
+ attr_accessor :headings
44
20
 
45
21
  ##
46
- # Generates a ASCII table.
22
+ # Rows array.
23
+
24
+ attr_accessor :rows
25
+
26
+ ##
27
+ # Generates a ASCII table with the given _options_.
47
28
 
48
29
  def initialize options = {}, &block
49
30
  @headings = options.fetch :headings, []
50
31
  @rows = options.fetch :rows, []
51
- yield_or_eval &block if block_given?
32
+ yield_or_eval &block if block
52
33
  end
53
34
 
54
35
  ##
55
- # Render the table. Often you will simply call _puts_ with an
56
- # instance in order to output it to the terminal.
36
+ # Render the table.
57
37
 
58
38
  def render
59
39
  buffer = seperator << "\n"
60
40
  if has_headings?
61
41
  buffer << Y + headings.map_with_index do |heading, i|
62
42
  width = 0
63
- if heading.is_a?(Hash) and !heading[:colspan].nil?
43
+ if Hash === heading and not heading[:colspan].nil?
64
44
  i.upto(i + heading[:colspan] - 1) do |col|
65
45
  width += length_of_column(col)
66
46
  end
67
47
  width += (heading[:colspan] - 1) * (Y.length + 2)
68
48
  else
69
- width = length_of_column(i)
49
+ width = length_of_column i
70
50
  end
71
- Heading.new( width, heading).render
51
+ Heading.new(width, heading).render
72
52
  end.join(Y) + Y
73
53
  buffer << "\n#{seperator}\n"
74
54
  end
75
55
  buffer << rows.map do |row|
76
56
  Y + row.map_with_index do |cell, i|
77
57
  width = 0
78
- if cell.is_a?(Hash) and !cell[:colspan].nil?
58
+ if Hash === cell and not cell[:colspan].nil?
79
59
  i.upto(i + cell[:colspan] - 1) do |col|
80
60
  width += length_of_column(col)
81
61
  end
82
62
  width += (cell[:colspan] - 1) * (Y.length + 2)
83
63
  else
84
- width = length_of_column(i)
64
+ width = length_of_column i
85
65
  end
86
66
  Cell.new(width, cell).render
87
67
  end.join(Y) + Y
@@ -108,21 +88,21 @@ module Terminal
108
88
  alias :<< :add_row
109
89
 
110
90
  ##
111
- # Weither or not any headings are present, since they are optional.
91
+ # Check if headings are present.
112
92
 
113
93
  def has_headings?
114
- !headings.empty?
94
+ not headings.empty?
115
95
  end
116
96
 
117
97
  ##
118
- # Return +n+ column.
98
+ # Return column _n_.
119
99
 
120
100
  def column n
121
101
  rows.map { |row| row[n] }.compact
122
102
  end
123
103
 
124
104
  ##
125
- # Return +n+ column including headings.
105
+ # Return _n_ column including headings.
126
106
 
127
107
  def column_with_headings n
128
108
  headings_with_rows.map { |row| row[n] }.compact
@@ -132,26 +112,26 @@ module Terminal
132
112
  # Return columns.
133
113
 
134
114
  def columns
135
- (0..number_of_columns-1).map { |n| column n }
115
+ (0...number_of_columns).map { |n| column n }
136
116
  end
137
117
 
138
118
  ##
139
- # Return the largest cell found within column +n+.
119
+ # Return the largest cell found within column _n_.
140
120
 
141
121
  def largest_cell_in_column n
142
- column_with_headings(n).sort_by { |cell| Cell.new(0, cell).length }.last
122
+ column_with_headings(n).sort_by do |cell|
123
+ Cell.new(0, cell).length
124
+ end.last
143
125
  end
144
126
 
145
127
  ##
146
- # Return length of column +n+.
128
+ # Return length of column _n_.
147
129
 
148
130
  def length_of_column n
149
- largest_cell = largest_cell_in_column(n)
150
- if largest_cell.is_a? Hash
151
- largest_cell[:value].length# - 2
152
- else
153
- largest_cell.to_s.length
154
- end
131
+ largest_cell = largest_cell_in_column n
132
+ Hash === largest_cell ?
133
+ largest_cell[:value].to_s.length :
134
+ largest_cell.to_s.length
155
135
  end
156
136
 
157
137
  ##
@@ -159,15 +139,15 @@ module Terminal
159
139
 
160
140
  def number_of_columns
161
141
  return rows.first.length unless rows.empty?
162
- raise Error, 'your table needs some rows.'
142
+ raise Error, 'your table needs some rows'
163
143
  end
164
144
 
165
145
  ##
166
- # Align column +n+ to +alignment+ of :center, :left, or :right.
146
+ # Align column _n_ to the given _alignment_ of :center, :left, or :right.
167
147
 
168
148
  def align_column n, alignment
169
149
  column(n).each_with_index do |col, i|
170
- @rows[i][n] = {:value => col, :alignment => alignment} unless col.is_a? Hash
150
+ @rows[i][n] = { :value => col, :alignment => alignment } unless Hash === col
171
151
  end
172
152
  end
173
153
 
@@ -179,12 +159,12 @@ module Terminal
179
159
  end
180
160
 
181
161
  ##
182
- # Check if +other+ is equal to self. +other+ is considered equal
162
+ # Check if _other_ is equal to self. _other_ is considered equal
183
163
  # if it contains the same headings and rows.
184
164
 
185
165
  def == other
186
- if other.respond_to?(:headings) && other.respond_to?(:rows)
187
- headings == other.headings && rows == other.rows
166
+ if other.respond_to? :headings and other.respond_to? :rows
167
+ headings == other.headings and rows == other.rows
188
168
  end
189
169
  end
190
170
  end
@@ -1,50 +1,6 @@
1
1
  module Terminal
2
2
  class Table
3
3
  module TableHelper
4
- ##
5
- # Generates a Terminal::Table object.
6
- #
7
- # === Examples:
8
- #
9
- # puts table(['a', 'b'], [1, 2], [3, 4])
10
- #
11
- # # OR
12
- #
13
- # t = table ['a', 'b']
14
- # t << [1, 2]
15
- # t << [3, 4]
16
- # puts t
17
- #
18
- # # OR
19
- #
20
- # user_table = table do |t|
21
- # t.headings = 'First Name', 'Last Name', 'Email'
22
- # t << ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
23
- # t << ['Bob', 'Someone', 'bob@vision-media.ca']
24
- # t << ['Joe', 'Whatever', 'joe@vision-media.ca']
25
- # end
26
- # puts user_table
27
- #
28
- # # OR
29
- #
30
- # user_table = table do
31
- # self.headings = 'First Name', 'Last Name', 'Email'
32
- # add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
33
- # add_row ['Bob', 'Someone', 'bob@vision-media.ca']
34
- # add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
35
- # end
36
- # puts user_table
37
- #
38
- # # OR
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
4
  def table headings = [], *rows, &block
49
5
  Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
50
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Terminal
3
3
  class Table
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end
6
6
  end
@@ -1,7 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
 
3
4
  describe Terminal::Table do
4
-
5
5
  Cell = Terminal::Table::Cell
6
6
 
7
7
  it "should default alignment to the left" do
@@ -10,10 +10,9 @@ describe Terminal::Table do
10
10
  cell.alignment.should == :left
11
11
  end
12
12
 
13
- it "should override alignment" do
14
- cell = Cell.new 0, {:value => 'foo', :alignment => :center}
13
+ it "should allow overriding of alignment" do
14
+ cell = Cell.new 0, :value => 'foo', :alignment => :center
15
15
  cell.value.should == 'foo'
16
16
  cell.alignment.should == :center
17
17
  end
18
-
19
18
  end
@@ -1,7 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
 
3
4
  describe String do
4
-
5
5
  describe "#align" do
6
6
  it "should center" do
7
7
  'foo'.align(:center, 10).should == ' foo '
@@ -15,5 +15,4 @@ describe String do
15
15
  'foo'.align(:right, 10).should == ' foo'
16
16
  end
17
17
  end
18
-
19
18
  end
@@ -1,11 +1,11 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
  require "terminal-table/import"
3
4
 
4
5
  describe Object do
5
6
  describe "#table" do
6
7
  it "should allow creation of a terminal table" do
7
- table(['foo', 'bar'], ['a', 'b'], [1, 2]).
8
- should be_instance_of(Terminal::Table)
8
+ table(['foo', 'bar'], ['a', 'b'], [1, 2]).should be_instance_of(Terminal::Table)
9
9
  end
10
10
  end
11
11
  end
@@ -1,5 +1,5 @@
1
1
 
2
- require File.expand_path(File.dirname(__FILE__) + "/../lib/terminal-table")
2
+ require File.dirname(__FILE__) + '/../lib/terminal-table'
3
3
 
4
4
  class String
5
5
  def deindent
@@ -1,4 +1,5 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
2
3
 
3
4
  module Terminal
4
5
  describe Table do
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{terminal-table}
5
- s.version = "1.2.0"
5
+ s.version = "1.3.0"
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-10-10}
9
+ s.date = %q{2009-10-16}
10
10
  s.description = %q{Simple, feature rich ascii table generation library}
11
11
  s.email = %q{tj@vision-media.ca}
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/table_helper.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/table_helper.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.opts", "spec/spec_helper.rb", "spec/table_helper_spec.rb", "spec/table_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "terminal-table.gemspec", "Todo.rdoc"]
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "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/table_helper.rb", "lib/terminal-table/version.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["History.rdoc", "Manifest", "README.rdoc", "Rakefile", "Todo.rdoc", "examples/examples.rb", "lib/terminal-table.rb", "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/table_helper.rb", "lib/terminal-table/version.rb", "spec/cell_spec.rb", "spec/core_ext_spec.rb", "spec/import_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/table_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "terminal-table.gemspec"]
14
14
  s.homepage = %q{http://github.com/visionmedia/terminal-table}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terminal-table", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal-table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-10 00:00:00 -07:00
12
+ date: 2009-10-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,6 +20,8 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/terminal-table.rb
23
25
  - lib/terminal-table/cell.rb
24
26
  - lib/terminal-table/core_ext.rb
25
27
  - lib/terminal-table/heading.rb
@@ -27,14 +29,17 @@ extra_rdoc_files:
27
29
  - lib/terminal-table/table.rb
28
30
  - lib/terminal-table/table_helper.rb
29
31
  - lib/terminal-table/version.rb
30
- - lib/terminal-table.rb
31
- - README.rdoc
32
32
  - tasks/docs.rake
33
33
  - tasks/gemspec.rake
34
34
  - tasks/spec.rake
35
35
  files:
36
- - examples/examples.rb
37
36
  - History.rdoc
37
+ - Manifest
38
+ - README.rdoc
39
+ - Rakefile
40
+ - Todo.rdoc
41
+ - examples/examples.rb
42
+ - lib/terminal-table.rb
38
43
  - lib/terminal-table/cell.rb
39
44
  - lib/terminal-table/core_ext.rb
40
45
  - lib/terminal-table/heading.rb
@@ -42,22 +47,16 @@ files:
42
47
  - lib/terminal-table/table.rb
43
48
  - lib/terminal-table/table_helper.rb
44
49
  - lib/terminal-table/version.rb
45
- - lib/terminal-table.rb
46
- - Manifest
47
- - Rakefile
48
- - README.rdoc
49
50
  - spec/cell_spec.rb
50
51
  - spec/core_ext_spec.rb
51
52
  - spec/import_spec.rb
52
53
  - spec/spec.opts
53
54
  - spec/spec_helper.rb
54
- - spec/table_helper_spec.rb
55
55
  - spec/table_spec.rb
56
56
  - tasks/docs.rake
57
57
  - tasks/gemspec.rake
58
58
  - tasks/spec.rake
59
59
  - terminal-table.gemspec
60
- - Todo.rdoc
61
60
  has_rdoc: true
62
61
  homepage: http://github.com/visionmedia/terminal-table
63
62
  licenses: []
@@ -1,18 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
-
3
- module Terminal
4
- describe Table::TableHelper do
5
- before do
6
- @obj = Class.new do
7
- include Table::TableHelper
8
- end.new
9
- end
10
-
11
- describe "#table" do
12
- it "should allow creation of a terminal table" do
13
- table = @obj.table(['foo', 'bar'], ['a', 'b'], [1, 2])
14
- table.should be_instance_of(Terminal::Table)
15
- end
16
- end
17
- end
18
- end