tabularize 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ 0.2.2
2
+ -----
3
+ - `screen_width` and `ellipsis` options to cut off trailing cells exceeding specified screen width
4
+
1
5
  0.2.1
2
6
  -----
3
7
  - Multi-line support
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tabularize (0.2.1)
4
+ tabularize (0.2.2)
5
5
  unicode-display_width (~> 0.1.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -52,30 +52,34 @@ puts table
52
52
  * Alignment
53
53
  * `:align` Horizontal alignment. `:left`, `:center`, `:right`, or Array of the three options
54
54
  * `:valign` Vertical alignment. `:top`, `:middle`, `:bottom`, or Array of the three options
55
+ * Ellipses: Cut off trailing cells if the total width exceeds the specified screen width
56
+ * `:screen_width` The number of columns for the current terminal. Default: unlimited.
57
+ * `:ellipsis` Ellipsis string when cells are cut off. Default: `>`
55
58
 
56
59
  ```ruby
57
60
  table = Tabularize.new :pad => '.', :pad_left => 2, :pad_right => 0,
58
61
  :hborder => '~', :vborder => 'I', :iborder => '#',
59
62
  :align => [:left, :center, :right],
60
- :valign => [:top, :bottom, :middle, :middle]
61
- table << %w[Name Dept Location Phone]
63
+ :valign => [:top, :bottom, :middle, :middle],
64
+ :screen_width => 75, :ellipsis => 'X'
65
+ table << %w[Name Dept Location Phone Description]
62
66
  table.separator!
63
- table << ['John Doe', 'Finance', 'Los Angeles CA 90089', "555-1555"]
64
- table << ['Average Joe', 'Engineering', 'Somewhere over the rainbow', 'N/A']
65
- table << ['홍길동', '탁상 3부', "서울역 3번 출구 김씨 옆자리\n\n맞습니다", 'N/A']
67
+ table << ['John Doe', 'Finance', 'Los Angeles CA 90089', '555-1555', 'Just a guy']
68
+ table << ['Average Joe', 'Engineering', 'Somewhere over the rainbow', 'N/A', 'Unknown']
69
+ table << ['홍길동', '탁상 3부', "서울역 3번 출구 김씨 옆자리\n\n맞습니다", 'N/A', 'No description']
66
70
  puts table
67
71
  ```
68
72
 
69
73
  ```
70
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~#
71
- I..Name.......I.....Dept....I.....................LocationI.....PhoneI
72
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~#
73
- I..John Doe...I....Finance..I.........Los Angeles CA 90089I..555-1555I
74
- I..Average JoeI..EngineeringI...Somewhere over the rainbowI.......N/AI
75
- I..홍길동.....I.............I..서울역 3번 출구 김씨 옆자리I..........I
76
- I.............I.............I.............................I.......N/AI
77
- I.............I...탁상 3부..I.....................맞습니다I..........I
78
- #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~#
74
+ #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
75
+ I..Name.......I.....Dept....I.....................LocationI.....PhoneX
76
+ #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
77
+ I..John Doe...I....Finance..I.........Los Angeles CA 90089I..555-1555X
78
+ I..Average JoeI..EngineeringI...Somewhere over the rainbowI.......N/AX
79
+ I..홍길동.....I.............I..서울역 3번 출구 김씨 옆자리I..........X
80
+ I.............I.............I.............................I.......N/AX
81
+ I.............I...탁상 3부..I.....................맞습니다I..........X
82
+ #~~~~~~~~~~~~~#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#~~~~~~~~~~X
79
83
  ```
80
84
 
81
85
  Tabularize.it
@@ -167,7 +171,7 @@ Average Joe | Engineering | Somewhere over the rainbow | N/A
167
171
  Hong Gildong | HR | Nowhere | 555-5555
168
172
  ```
169
173
 
170
- #### Padding with other characters
174
+ #### Padding with characters other than space
171
175
 
172
176
  ```ruby
173
177
  puts Tabularize.it(data, :pad => '_').map { |row| row.join ' | ' }
@@ -178,13 +182,13 @@ Name________ | Dept_______ | Location__________________ | Phone___
178
182
  John Doe____ | Finance____ | Los Angeles, CA 90089_____ | 555-1555
179
183
  Average Joe_ | Engineering | Somewhere over the rainbow | N/A_____
180
184
  Hong Gildong | HR_________ | Nowhere___________________ | 555-5555
181
- 홍길동______ | 탁상 3부___ | 서울역 3번 출구 김씨 옆자리 | N/A
185
+ 홍길동______ | 탁상 3부___ | 서울역 3번 출구 김씨 옆자리 | N/A_____
182
186
  ```
183
187
 
184
188
  ANSI codes and CJK wide characters
185
189
  ----------------------------------
186
190
  [tabularize](https://github.com/junegunn/tabularize) correctly calculates each cell width even in the presence of ANSI codes and CJK wide characters.
187
- If your data doesn't have any of them, unset `:unicode` and `:ansi` options
191
+ However, if your data doesn't have any of them, unset `:unicode` and `:ansi` in options hash,
188
192
  so that [tabularize](https://github.com/junegunn/tabularize) can process data more efficiently.
189
193
 
190
194
  ```ruby
@@ -198,9 +202,10 @@ when I blindly started building [tabularize](https://github.com/junegunn/tabular
198
202
  It has more features and clearly is more mature than [tabularize](https://github.com/junegunn/tabularize),
199
203
  you should definitely check it out.
200
204
 
201
- There are a couple of things, however, [tabularize](https://github.com/junegunn/tabularize) does better:
205
+ There are a few things, however, [tabularize](https://github.com/junegunn/tabularize) does better:
202
206
  - It correctly formats cells containing CJK wide characters.
203
207
  - Vertical alignment for multi-line cells
208
+ - Cutting off trailing cells when the width of the output string exceeds specified screen width
204
209
 
205
210
  Copyright
206
211
  ---------
@@ -16,6 +16,9 @@ class Tabularize
16
16
 
17
17
  :unicode => true,
18
18
  :ansi => true,
19
+
20
+ :ellipsis => '>',
21
+ :screen_width => nil,
19
22
  }
20
23
 
21
24
  DEFAULT_OPTIONS_GENERATOR = {
@@ -51,15 +54,29 @@ class Tabularize
51
54
  rows = Tabularize.it(@rows, @options)
52
55
  return nil if rows.empty?
53
56
 
54
- h = @options[:hborder]
55
- v = @options[:vborder]
56
- i = @options[:iborder]
57
- u = @options[:unicode]
58
- a = @options[:ansi]
59
-
60
- separator = i + rows[0].map { |c|
61
- h * Tabularize.cell_width(c, u, a)
62
- }.join( i ) + i
57
+ h = @options[:hborder]
58
+ v = @options[:vborder]
59
+ i = @options[:iborder]
60
+ vl = @options[:vborder]
61
+ il = @options[:iborder]
62
+ u = @options[:unicode]
63
+ a = @options[:ansi]
64
+ sw = @options[:screen_width]
65
+ el = @options[:ellipsis].length
66
+
67
+ separator = ''
68
+ rows[0].each_with_index do |c, idx|
69
+ new_sep = separator + i + h * Tabularize.cell_width(c, u, a)
70
+
71
+ if sw && Tabularize.cell_width(new_sep, u, a) > sw - el
72
+ rows = rows.map { |line| line[0, idx] }
73
+ vl = il = @options[:ellipsis]
74
+ break
75
+ else
76
+ separator = new_sep
77
+ end
78
+ end
79
+ separator += il
63
80
 
64
81
  output = StringIO.new
65
82
  output.puts separator
@@ -70,9 +87,10 @@ class Tabularize
70
87
  output.puts separator
71
88
  end
72
89
  (0...height).each do |line|
73
- output.puts v + row.map { |lines|
90
+ output.print v unless row.empty?
91
+ output.puts row.map { |lines|
74
92
  lines[line] || @options[:pad] * Tabularize.cell_width(lines[0], u, a)
75
- }.join(v) + v
93
+ }.join(v) + vl
76
94
  end
77
95
  end
78
96
 
@@ -113,15 +131,16 @@ class Tabularize
113
131
  valign = [*options[:valign]]
114
132
  unicode = options[:unicode]
115
133
  ansi = options[:ansi]
134
+ screenw = options[:screen_width]
116
135
 
117
136
  unless pad.length == 1
118
137
  raise ArgumentError.new("Invalid padding")
119
138
  end
120
139
  unless padl.is_a?(Fixnum) && padl >= 0
121
- raise ArgumentError.new(":pad_left must be a non-negative Fixnum")
140
+ raise ArgumentError.new(":pad_left must be a non-negative integer")
122
141
  end
123
142
  unless padr.is_a?(Fixnum) && padr >= 0
124
- raise ArgumentError.new(":pad_right must be a non-negative Fixnum")
143
+ raise ArgumentError.new(":pad_right must be a non-negative integer")
125
144
  end
126
145
  unless align.all? { |a| [:left, :right, :center].include?(a) }
127
146
  raise ArgumentError.new("Invalid alignment")
@@ -129,6 +148,9 @@ class Tabularize
129
148
  unless valign.all? { |a| [:top, :bottom, :middle].include?(a) }
130
149
  raise ArgumentError.new("Invalid vertical alignment")
131
150
  end
151
+ unless screenw.nil? || (screenw.is_a?(Fixnum) && screenw > 0)
152
+ raise ArgumentError.new(":screen_width must be a positive integer")
153
+ end
132
154
 
133
155
  rows = []
134
156
  max_widths = []
@@ -1,3 +1,3 @@
1
1
  class Tabularize
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -11,10 +11,11 @@ table = Tabularize.new
11
11
  table = Tabularize.new :pad => '.', :pad_left => 2, :pad_right => 0,
12
12
  :hborder => '~', :vborder => 'I', :iborder => '#',
13
13
  :align => [:left, :center, :right],
14
- :valign => [:top, :bottom, :middle, :middle]
15
- table << %w[Name Dept Location Phone]
14
+ :valign => [:top, :bottom, :middle, :middle],
15
+ :screen_width => 75, :ellipsis => 'X'
16
+ table << %w[Name Dept Location Phone Description]
16
17
  table.separator!
17
- table << ['John Doe', 'Finance', 'Los Angeles CA 90089', '555-1555']
18
- table << ['Average Joe', 'Engineering', 'Somewhere over the rainbow', 'N/A']
19
- table << ['홍길동', '탁상 3부', "서울역 3번 출구 김씨 옆자리\n\n맞습니다", 'N/A']
18
+ table << ['John Doe', 'Finance', 'Los Angeles CA 90089', '555-1555', 'Just a guy']
19
+ table << ['Average Joe', 'Engineering', 'Somewhere over the rainbow', 'N/A', 'Unknown']
20
+ table << ['홍길동', '탁상 3부', "서울역 3번 출구 김씨 옆자리\n\n맞습니다", 'N/A', 'No description']
20
21
  puts table
@@ -140,6 +140,7 @@ class TestTabularize < Test::Unit::TestCase
140
140
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :pad => ' ', :pad_left => '') }
141
141
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :pad => ' ', :pad_right => -1) }
142
142
  assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :pad => ' ', :pad_right => '') }
143
+ assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :screen_width => -2) }
143
144
  end
144
145
 
145
146
  def test_table
@@ -192,4 +193,15 @@ I.............I...탁상 3부..I.....................맞습니다I..........I
192
193
  table.separator!
193
194
  assert_equal output, table.to_s.strip
194
195
  end
196
+
197
+ def test_screen_width
198
+ [1, 3, 9, 50, 80].each do |w|
199
+ t = Tabularize.new :screen_width => w
200
+ 10.times do
201
+ t << ['12345'] * 80
202
+ end
203
+ puts t
204
+ assert t.to_s.lines.all? { |line| line.chomp.length <= w }
205
+ end
206
+ end
195
207
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabularize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-11 00:00:00.000000000 Z
12
+ date: 2012-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print