ttable 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/lib/terminal/table/version.rb +1 -1
- data/lib/terminal/table.rb +32 -14
- data/spec/terminal/table_spec.rb +154 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9ad6164cd3372852f9897c81e17e3d9e6b5739f
|
4
|
+
data.tar.gz: e787f4da84c71d9bea389f894fe3c68455dbfbe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55fde9d65d87587463d56104122d8ec653aa42860239117b967cf8bccdbb37240d8742d4be606ebeb972bf1a8c4cfc13e7a89088374e19f18febb81ee0bd9bca
|
7
|
+
data.tar.gz: 7900c006ff4e09e3b5da319b883826de11164e9086aca865dafd9ec208bf2ed6e1a2ff9a1a9e8e58318daa0b911db20b76531f851d8dd9790f68828be5e75970
|
data/lib/terminal/table.rb
CHANGED
@@ -6,14 +6,11 @@ class String
|
|
6
6
|
def twidth
|
7
7
|
result = 0
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# 3 code point emoji
|
15
|
-
%w{ ♍️ }.each do |c|
|
16
|
-
result += -2 * scan(c).size
|
9
|
+
%w{ ☺️ ❤️ ♍️ }.each do |c|
|
10
|
+
if include?(c)
|
11
|
+
result += 1 * scan(c).size
|
12
|
+
gsub!(c, '')
|
13
|
+
end
|
17
14
|
end
|
18
15
|
|
19
16
|
chars.inject(result) do |result, c|
|
@@ -22,7 +19,7 @@ class String
|
|
22
19
|
elsif %w{ ͡ ͜ }.include?(c)
|
23
20
|
# zero width
|
24
21
|
result += 0
|
25
|
-
elsif %w{ ě ì • é · ♪ … ω ˊ ˋ √ “ ” ☻ ※ ◎ ◆ ‘ ★ ’ — ° ʖ ¯ ≥ ≤ }.include?(c)
|
22
|
+
elsif %w{ ě ì • é · ♪ … ω ˊ ˋ √ “ ” ☻ ※ ◎ ◆ ‘ ★ ’ — ° ʖ ¯ ≥ ≤ ≧ ∇ ≦ ❤ }.include?(c)
|
26
23
|
result += 1
|
27
24
|
elsif c == ' ' # ord == 8198
|
28
25
|
result += 1
|
@@ -49,25 +46,44 @@ module Terminal
|
|
49
46
|
attr_accessor :headings
|
50
47
|
attr_accessor :column_widths
|
51
48
|
|
52
|
-
def initialize
|
49
|
+
def initialize(object = nil, options = {})
|
53
50
|
@rows = []
|
54
51
|
@headings = []
|
55
52
|
@column_widths = []
|
53
|
+
|
54
|
+
if object
|
55
|
+
if object.respond_to?(:each)
|
56
|
+
object.each { |o| add_object(o, options) }
|
57
|
+
else
|
58
|
+
add_object(object, options)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
56
62
|
yield self if block_given?
|
57
63
|
recalculate_column_widths!
|
58
64
|
end
|
59
65
|
|
60
|
-
def
|
61
|
-
|
62
|
-
|
66
|
+
def add_object(object, options)
|
67
|
+
if object.respond_to?(:to_hash)
|
68
|
+
hash = object.to_hash
|
69
|
+
if options[:only]
|
70
|
+
hash.keep_if { |k, v| options[:only].map(&:to_sym).include?(k) }
|
71
|
+
elsif options[:except]
|
72
|
+
hash.delete_if { |k, v| options[:except].map(&:to_sym).include?(k) }
|
73
|
+
end
|
74
|
+
|
75
|
+
@headings = hash.keys.map(&:to_s)
|
76
|
+
@rows << hash.values
|
77
|
+
end
|
63
78
|
end
|
64
79
|
|
65
80
|
def headings=(headings)
|
66
81
|
@headings = headings
|
67
|
-
recalculate_column_widths!
|
68
82
|
end
|
69
83
|
|
70
84
|
def recalculate_column_widths!
|
85
|
+
@rows = rows.map { |row| row.map { |item| item.to_s.gsub("\r\n", " ").gsub("\n", " ").gsub("\r", " ") } }
|
86
|
+
|
71
87
|
if @rows.count > 0
|
72
88
|
(0...@rows.first.size).each do |col|
|
73
89
|
@column_widths[col] = @rows.map { |row| row[col].to_s.twidth }.max
|
@@ -82,6 +98,8 @@ module Terminal
|
|
82
98
|
end
|
83
99
|
|
84
100
|
def to_s
|
101
|
+
recalculate_column_widths!
|
102
|
+
|
85
103
|
result = ''
|
86
104
|
|
87
105
|
header_and_footer = '+' + @column_widths.map { |w| '-' * (w + 2) }.join('+') + '+' + "\n"
|
data/spec/terminal/table_spec.rb
CHANGED
@@ -2,17 +2,106 @@
|
|
2
2
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
|
+
class Dummy
|
6
|
+
attr_accessor :foo
|
7
|
+
|
8
|
+
def to_hash
|
9
|
+
{ foo: @foo }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Dummy2
|
14
|
+
attr_accessor :foo1, :foo2, :foo3
|
15
|
+
|
16
|
+
def to_hash
|
17
|
+
{ foo1: @foo1, foo2: @foo2, foo3: @foo3 }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
5
21
|
module Terminal
|
6
22
|
describe Table do
|
7
23
|
it { should respond_to :to_s }
|
8
24
|
|
25
|
+
describe 'initialize with object#to_hash' do
|
26
|
+
let(:object) { Dummy.new.tap { |d| d.foo = 'bar' } }
|
27
|
+
subject { Table.new(object) }
|
28
|
+
|
29
|
+
expected = <<END
|
30
|
+
+-----+
|
31
|
+
| foo |
|
32
|
+
+-----+
|
33
|
+
| bar |
|
34
|
+
+-----+
|
35
|
+
END
|
36
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'initialize with objects' do
|
40
|
+
let(:object1) { Dummy.new.tap { |d| d.foo = 'bar1' } }
|
41
|
+
let(:object2) { Dummy.new.tap { |d| d.foo = 'bar2' } }
|
42
|
+
|
43
|
+
subject { Table.new([object1, object2]) }
|
44
|
+
|
45
|
+
expected = <<END
|
46
|
+
+------+
|
47
|
+
| foo |
|
48
|
+
+------+
|
49
|
+
| bar1 |
|
50
|
+
| bar2 |
|
51
|
+
+------+
|
52
|
+
END
|
53
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'initialize with object#to_hash and :only option' do
|
57
|
+
let(:object) { Dummy2.new.tap { |d| d.foo1 = 'bar1'; d.foo2 = 'bar2'; d.foo3 = 'bar3' } }
|
58
|
+
subject { Table.new(object, only: [:foo1, :foo2]) }
|
59
|
+
|
60
|
+
expected = <<END
|
61
|
+
+------+------+
|
62
|
+
| foo1 | foo2 |
|
63
|
+
+------+------+
|
64
|
+
| bar1 | bar2 |
|
65
|
+
+------+------+
|
66
|
+
END
|
67
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'initialize with object#to_hash and :only option' do
|
71
|
+
let(:object) { Dummy2.new.tap { |d| d.foo1 = 'bar1'; d.foo2 = 'bar2'; d.foo3 = 'bar3' } }
|
72
|
+
subject { Table.new(object, only: %w{ foo1 foo2 }) }
|
73
|
+
|
74
|
+
expected = <<END
|
75
|
+
+------+------+
|
76
|
+
| foo1 | foo2 |
|
77
|
+
+------+------+
|
78
|
+
| bar1 | bar2 |
|
79
|
+
+------+------+
|
80
|
+
END
|
81
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'initialize with object#to_hash and :except option' do
|
85
|
+
let(:object) { Dummy2.new.tap { |d| d.foo1 = 'bar1'; d.foo2 = 'bar2'; d.foo3 = 'bar3' } }
|
86
|
+
subject { Table.new(object, except: [:foo1, :foo3]) }
|
87
|
+
|
88
|
+
expected = <<END
|
89
|
+
+------+
|
90
|
+
| foo2 |
|
91
|
+
+------+
|
92
|
+
| bar2 |
|
93
|
+
+------+
|
94
|
+
END
|
95
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
96
|
+
end
|
97
|
+
|
9
98
|
describe '#to_s' do
|
10
99
|
context 'when empty' do
|
11
100
|
subject { Table.new }
|
12
101
|
its(:to_s) { should == "++\n++\n" }
|
13
102
|
end
|
14
103
|
|
15
|
-
context 'new lines' do
|
104
|
+
context 'new lines \n' do
|
16
105
|
subject { Table.new { |t| t.rows = [["a\nb"]] } }
|
17
106
|
|
18
107
|
expected = <<END
|
@@ -23,6 +112,45 @@ END
|
|
23
112
|
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
24
113
|
end
|
25
114
|
|
115
|
+
context 'new lines \n when <<' do
|
116
|
+
subject { Table.new { |t| t.rows << ["a\nb"] } }
|
117
|
+
|
118
|
+
expected = <<END
|
119
|
+
+-----+
|
120
|
+
| a b |
|
121
|
+
+-----+
|
122
|
+
END
|
123
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'new lines \r' do
|
127
|
+
subject { Table.new { |t| t.rows = [["a\rb"]] } }
|
128
|
+
|
129
|
+
expected = <<END
|
130
|
+
+-----+
|
131
|
+
| a b |
|
132
|
+
+-----+
|
133
|
+
END
|
134
|
+
its(:to_s) { should == expected.gsub(/^(\s+)/, '') }
|
135
|
+
end
|
136
|
+
|
137
|
+
context 'mutli calls to <<' do
|
138
|
+
it 'works as well' do
|
139
|
+
table = Table.new
|
140
|
+
table.rows << %w{ a }
|
141
|
+
table.rows << %w{ b }
|
142
|
+
|
143
|
+
expected = <<END
|
144
|
+
+---+
|
145
|
+
| a |
|
146
|
+
| b |
|
147
|
+
+---+
|
148
|
+
END
|
149
|
+
|
150
|
+
table.to_s.should == expected.gsub(/^(\s+)/, '')
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
26
154
|
context 'when only heading' do
|
27
155
|
subject { Table.new { |t| t.headings = %w{ head } } }
|
28
156
|
its(:to_s) { should == "+------+\n| head |\n+------+\n+------+\n" }
|
@@ -80,6 +208,11 @@ describe String do
|
|
80
208
|
its(:twidth) { should == 1 }
|
81
209
|
end
|
82
210
|
|
211
|
+
context '☺️☺️' do
|
212
|
+
subject { '☺️☺️' }
|
213
|
+
its(:twidth) { should == 2 }
|
214
|
+
end
|
215
|
+
|
83
216
|
context '❤️' do
|
84
217
|
subject { '❤️' }
|
85
218
|
its(:twidth) { should == 1 }
|
@@ -105,6 +238,11 @@ describe String do
|
|
105
238
|
its(:twidth) { should == 1 }
|
106
239
|
end
|
107
240
|
|
241
|
+
context '♍️♍️' do
|
242
|
+
subject { '♍️♍️' }
|
243
|
+
its(:twidth) { should == 2 }
|
244
|
+
end
|
245
|
+
|
108
246
|
context '☻' do
|
109
247
|
subject { '☻' }
|
110
248
|
its(:twidth) { should == 1 }
|
@@ -164,5 +302,20 @@ describe String do
|
|
164
302
|
subject { '≥≤' }
|
165
303
|
its(:twidth) { should == 2 }
|
166
304
|
end
|
305
|
+
|
306
|
+
context '(≧∇≦)' do
|
307
|
+
subject { '(≧∇≦)' }
|
308
|
+
its(:twidth) { should == 7 }
|
309
|
+
end
|
310
|
+
|
311
|
+
context '' do
|
312
|
+
subject { '' }
|
313
|
+
its(:twidth) { should == 1 }
|
314
|
+
end
|
315
|
+
|
316
|
+
context '❤' do
|
317
|
+
subject { '❤' }
|
318
|
+
its(:twidth) { should == 1 }
|
319
|
+
end
|
167
320
|
end
|
168
321
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ttable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Forrest Ye
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|