terminal-table 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Todo.rdoc CHANGED
@@ -1,14 +1,14 @@
1
-
2
- == Major:
3
-
4
- * Nothing
5
-
6
- == Minor:
7
-
8
- * Programmatically add separator rows
9
- * Add multi-column sorting
10
- * Change; pre-create Cell and Heading objects to clean up Table a bit
11
-
12
- == Brainstorming:
13
-
1
+
2
+ == Major:
3
+
4
+ * Nothing
5
+
6
+ == Minor:
7
+
8
+ * Programmatically add separator rows
9
+ * Add multi-column sorting
10
+ * Change; pre-create Cell and Heading objects to clean up Table a bit
11
+
12
+ == Brainstorming:
13
+
14
14
  * Nothing
@@ -1,83 +1,83 @@
1
- $:.unshift File.dirname(__FILE__) + '/../lib'
2
- require 'terminal-table/import'
3
-
4
- puts
5
- puts table(['a', 'b'], [1, 2], [3, 4])
6
-
7
- puts
8
- puts table(['name', 'content'], ['ftp.example.com', '1.1.1.1'], ['www.example.com', '|lalalala|lalala|'])
9
-
10
- puts
11
- t = table ['a', 'b']
12
- t.style = {:padding_left => 2, :width => 80}
13
- t << [1, 2]
14
- t << [3, 4]
15
- t << :separator
16
- t << [4, 6]
17
- puts t
18
-
19
- puts
20
- user_table = table do |v|
21
- v.title = "Contact Information"
22
- v.headings = 'First Name', 'Last Name', 'Email'
23
- v << %w( TJ Holowaychuk tj@vision-media.ca )
24
- v << %w( Bob Someone bob@vision-media.ca )
25
- v << %w( Joe Whatever bob@vision-media.ca )
26
- end
27
- puts user_table
28
-
29
- puts
30
- user_table = table do |v|
31
- v.style.width = 80
32
- v.headings = 'First Name', 'Last Name', 'Email'
33
- v << %w( TJ Holowaychuk tj@vision-media.ca )
34
- v << %w( Bob Someone bob@vision-media.ca )
35
- v << %w( Joe Whatever bob@vision-media.ca )
36
- end
37
- puts user_table
38
-
39
- puts
40
- user_table = table do
41
- self.headings = 'First Name', 'Last Name', 'Email'
42
- add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
43
- add_row ['Bob', 'Someone', 'bob@vision-media.ca']
44
- add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
45
- add_separator
46
- add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
47
- align_column 1, :center
48
- end
49
- puts user_table
50
-
51
- puts
52
- user_table = table do
53
- self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
54
- add_row ['Bob', 'Someone', '123', '456']
55
- add_row :separator
56
- add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
57
- add_row :separator
58
- add_row ['Joe', 'Whatever', '4324', '343242']
59
- end
60
- puts user_table
61
-
62
- rows = []
63
- rows << ['Lines', 100]
64
- rows << ['Comments', 20]
65
- rows << ['Ruby', 70]
66
- rows << ['JavaScript', 30]
67
- puts table([nil, 'Lines'], *rows)
68
-
69
- rows = []
70
- rows << ['Lines', 100]
71
- rows << ['Comments', 20]
72
- rows << ['Ruby', 70]
73
- rows << ['JavaScript', 30]
74
- puts table(nil, *rows)
75
-
76
- rows = []
77
- rows << ['Lines', 100]
78
- rows << ['Comments', 20]
79
- rows << ['Ruby', 70]
80
- rows << ['JavaScript', 30]
81
- table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
82
- table.align_column 1, :right
83
- puts table
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'terminal-table/import'
3
+
4
+ puts
5
+ puts table(['a', 'b'], [1, 2], [3, 4])
6
+
7
+ puts
8
+ puts table(['name', 'content'], ['ftp.example.com', '1.1.1.1'], ['www.example.com', '|lalalala|lalala|'])
9
+
10
+ puts
11
+ t = table ['a', 'b']
12
+ t.style = {:padding_left => 2, :width => 80}
13
+ t << [1, 2]
14
+ t << [3, 4]
15
+ t << :separator
16
+ t << [4, 6]
17
+ puts t
18
+
19
+ puts
20
+ user_table = table do |v|
21
+ v.title = "Contact Information"
22
+ v.headings = 'First Name', 'Last Name', 'Email'
23
+ v << %w( TJ Holowaychuk tj@vision-media.ca )
24
+ v << %w( Bob Someone bob@vision-media.ca )
25
+ v << %w( Joe Whatever bob@vision-media.ca )
26
+ end
27
+ puts user_table
28
+
29
+ puts
30
+ user_table = table do |v|
31
+ v.style.width = 80
32
+ v.headings = 'First Name', 'Last Name', 'Email'
33
+ v << %w( TJ Holowaychuk tj@vision-media.ca )
34
+ v << %w( Bob Someone bob@vision-media.ca )
35
+ v << %w( Joe Whatever bob@vision-media.ca )
36
+ end
37
+ puts user_table
38
+
39
+ puts
40
+ user_table = table do
41
+ self.headings = 'First Name', 'Last Name', 'Email'
42
+ add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
43
+ add_row ['Bob', 'Someone', 'bob@vision-media.ca']
44
+ add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
45
+ add_separator
46
+ add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
47
+ align_column 1, :center
48
+ end
49
+ puts user_table
50
+
51
+ puts
52
+ user_table = table do
53
+ self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
54
+ add_row ['Bob', 'Someone', '123', '456']
55
+ add_row :separator
56
+ add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
57
+ add_row :separator
58
+ add_row ['Joe', 'Whatever', '4324', '343242']
59
+ end
60
+ puts user_table
61
+
62
+ rows = []
63
+ rows << ['Lines', 100]
64
+ rows << ['Comments', 20]
65
+ rows << ['Ruby', 70]
66
+ rows << ['JavaScript', 30]
67
+ puts table([nil, 'Lines'], *rows)
68
+
69
+ rows = []
70
+ rows << ['Lines', 100]
71
+ rows << ['Comments', 20]
72
+ rows << ['Ruby', 70]
73
+ rows << ['JavaScript', 30]
74
+ puts table(nil, *rows)
75
+
76
+ rows = []
77
+ rows << ['Lines', 100]
78
+ rows << ['Comments', 20]
79
+ rows << ['Ruby', 70]
80
+ rows << ['JavaScript', 30]
81
+ table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
82
+ table.align_column 1, :right
83
+ puts table
@@ -1,20 +1,19 @@
1
-
2
1
  module Terminal
3
2
  class Table
4
3
  class Cell
5
4
  ##
6
5
  # Cell value.
7
-
6
+
8
7
  attr_reader :value
9
-
8
+
10
9
  ##
11
10
  # Column span.
12
-
11
+
13
12
  attr_reader :colspan
14
-
13
+
15
14
  ##
16
15
  # Initialize with _options_.
17
-
16
+
18
17
  def initialize options = nil
19
18
  @value, options = options, {} unless Hash === options
20
19
  @value = options.fetch :value, value
@@ -24,15 +23,15 @@ module Terminal
24
23
  @index = options.fetch :index
25
24
  @table = options.fetch :table
26
25
  end
27
-
26
+
28
27
  def alignment?
29
28
  !@alignment.nil?
30
29
  end
31
-
30
+
32
31
  def alignment
33
32
  @alignment || @table.style.alignment || :left
34
33
  end
35
-
34
+
36
35
  def alignment=(val)
37
36
  supported = %w(left center right)
38
37
  if supported.include?(val.to_s)
@@ -41,7 +40,7 @@ module Terminal
41
40
  raise "Aligment must be one of: #{supported.join(' ')}"
42
41
  end
43
42
  end
44
-
43
+
45
44
  def align(val, position, length)
46
45
  positions = { :left => :ljust, :right => :rjust, :center => :center }
47
46
  val.public_send(positions[position], length)
@@ -49,10 +48,10 @@ module Terminal
49
48
  def lines
50
49
  @value.to_s.split(/\n/)
51
50
  end
52
-
51
+
53
52
  ##
54
53
  # Render the cell.
55
-
54
+
56
55
  def render(line = 0)
57
56
  left = " " * @table.style.padding_left
58
57
  right = " " * @table.style.padding_right
@@ -60,18 +59,18 @@ module Terminal
60
59
  align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
61
60
  end
62
61
  alias :to_s :render
63
-
62
+
64
63
  ##
65
64
  # Returns the longest line in the cell and
66
65
  # removes all ANSI escape sequences (e.g. color)
67
-
66
+
68
67
  def value_for_column_width_recalc
69
68
  lines.map{ |s| escape(s) }.max_by{ |s| s.size }
70
69
  end
71
-
70
+
72
71
  ##
73
72
  # Returns the width of this cell
74
-
73
+
75
74
  def width
76
75
  padding = (colspan - 1) * @table.cell_spacing
77
76
  inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
@@ -81,7 +80,7 @@ module Terminal
81
80
  end
82
81
 
83
82
  ##
84
- # removes all ANSI escape sequences (e.g. color)
83
+ # removes all ANSI escape sequences (e.g. color)
85
84
  def escape(line)
86
85
  line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
87
86
  gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
@@ -1,14 +1,14 @@
1
- module Terminal
2
- class Table
3
- class Separator < Row
4
-
5
- def render
6
- arr_x = (0...@table.number_of_columns).to_a.map do |i|
7
- @table.style.border_x * (@table.column_width(i) + @table.cell_padding)
8
- end
9
- border_i = @table.style.border_i
10
- border_i + arr_x.join(border_i) + border_i
11
- end
12
- end
13
- end
14
- end
1
+ module Terminal
2
+ class Table
3
+ class Separator < Row
4
+
5
+ def render
6
+ arr_x = (0...@table.number_of_columns).to_a.map do |i|
7
+ @table.style.border_x * (@table.column_width(i) + @table.cell_padding)
8
+ end
9
+ border_i = @table.style.border_i
10
+ border_i + arr_x.join(border_i) + border_i
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,62 +1,67 @@
1
-
2
- module Terminal
3
- class Table
4
- # A Style object holds all the formatting information for a Table object
5
- #
6
- # To create a table with a certain style, use either the constructor
7
- # option <tt>:style</tt>, the Table#style object or the Table#style= method
8
- #
9
- # All these examples have the same effect:
10
- #
11
- # # by constructor
12
- # @table = Table.new(:style => {:padding_left => 2, :width => 40})
13
- #
14
- # # by object
15
- # @table.style.padding_left = 2
16
- # @table.style.width = 40
17
- #
18
- # # by method
19
- # @table.style = {:padding_left => 2, :width => 40}
20
- #
21
- # To set a default style for all tables created afterwards use Style.defaults=
22
- #
23
- # Terminal::Table::Style.defaults = {:width => 80}
24
- #
25
- class Style
26
- @@defaults = {
27
- :border_x => "-", :border_y => "|", :border_i => "+",
28
- :padding_left => 1, :padding_right => 1,
29
- :width => nil, :alignment => nil
30
- }
31
-
32
- attr_accessor :border_x
33
- attr_accessor :border_y
34
- attr_accessor :border_i
35
-
36
- attr_accessor :padding_left
37
- attr_accessor :padding_right
38
-
39
- attr_accessor :width
40
- attr_accessor :alignment
41
-
42
-
43
- def initialize options = {}
44
- apply self.class.defaults.merge(options)
45
- end
46
-
47
- def apply options
48
- options.each { |m, v| __send__ "#{m}=", v }
49
- end
50
-
51
- class << self
52
- def defaults
53
- @@defaults
54
- end
55
-
56
- def defaults= options
57
- @@defaults = defaults.merge(options)
58
- end
59
- end
60
- end
61
- end
62
- end
1
+ module Terminal
2
+ class Table
3
+ # A Style object holds all the formatting information for a Table object
4
+ #
5
+ # To create a table with a certain style, use either the constructor
6
+ # option <tt>:style</tt>, the Table#style object or the Table#style= method
7
+ #
8
+ # All these examples have the same effect:
9
+ #
10
+ # # by constructor
11
+ # @table = Table.new(:style => {:padding_left => 2, :width => 40})
12
+ #
13
+ # # by object
14
+ # @table.style.padding_left = 2
15
+ # @table.style.width = 40
16
+ #
17
+ # # by method
18
+ # @table.style = {:padding_left => 2, :width => 40}
19
+ #
20
+ # To set a default style for all tables created afterwards use Style.defaults=
21
+ #
22
+ # Terminal::Table::Style.defaults = {:width => 80}
23
+ #
24
+ class Style
25
+ @@defaults = {
26
+ :border_x => "-", :border_y => "|", :border_i => "+",
27
+ :padding_left => 1, :padding_right => 1,
28
+ :margin_left => '',
29
+ :width => nil, :alignment => nil,
30
+ :all_separators => false
31
+ }
32
+
33
+ attr_accessor :border_x
34
+ attr_accessor :border_y
35
+ attr_accessor :border_i
36
+
37
+ attr_accessor :padding_left
38
+ attr_accessor :padding_right
39
+
40
+ attr_accessor :margin_left
41
+
42
+ attr_accessor :width
43
+ attr_accessor :alignment
44
+
45
+ attr_accessor :all_separators
46
+
47
+
48
+ def initialize options = {}
49
+ apply self.class.defaults.merge(options)
50
+ end
51
+
52
+ def apply options
53
+ options.each { |m, v| __send__ "#{m}=", v }
54
+ end
55
+
56
+ class << self
57
+ def defaults
58
+ @@defaults
59
+ end
60
+
61
+ def defaults= options
62
+ @@defaults = defaults.merge(options)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end