tty-table 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/README.md +458 -142
  4. data/lib/tty-table.rb +6 -6
  5. data/lib/tty/table.rb +34 -34
  6. data/lib/tty/table/alignment_set.rb +73 -0
  7. data/lib/tty/table/border.rb +54 -36
  8. data/lib/tty/table/border/null.rb +4 -4
  9. data/lib/tty/table/{columns.rb → column_constraint.rb} +30 -32
  10. data/lib/tty/table/column_set.rb +18 -17
  11. data/lib/tty/table/field.rb +50 -25
  12. data/lib/tty/table/header.rb +6 -2
  13. data/lib/tty/table/indentation.rb +7 -12
  14. data/lib/tty/table/operation/alignment.rb +59 -0
  15. data/lib/tty/table/operation/escape.rb +1 -1
  16. data/lib/tty/table/operation/filter.rb +1 -1
  17. data/lib/tty/table/operation/padding.rb +12 -61
  18. data/lib/tty/table/operation/truncation.rb +2 -2
  19. data/lib/tty/table/operation/wrapped.rb +2 -5
  20. data/lib/tty/table/operations.rb +35 -17
  21. data/lib/tty/table/orientation/vertical.rb +4 -4
  22. data/lib/tty/table/renderer.rb +1 -7
  23. data/lib/tty/table/renderer/basic.rb +69 -63
  24. data/lib/tty/table/version.rb +1 -1
  25. data/spec/spec_helper.rb +3 -4
  26. data/spec/unit/access_spec.rb +8 -8
  27. data/spec/unit/{operation/alignment_set → alignment_set}/each_spec.rb +1 -1
  28. data/spec/unit/{operation/alignment_set → alignment_set}/new_spec.rb +4 -4
  29. data/spec/unit/{operation/alignment_set → alignment_set}/to_ary_spec.rb +1 -1
  30. data/spec/unit/alignment_spec.rb +71 -0
  31. data/spec/unit/border/ascii/rendering_spec.rb +12 -12
  32. data/spec/unit/border/new_spec.rb +2 -2
  33. data/spec/unit/border/null/rendering_spec.rb +2 -2
  34. data/spec/unit/border/unicode/rendering_spec.rb +10 -10
  35. data/spec/unit/{columns → column_constraint}/enforce_spec.rb +15 -12
  36. data/spec/unit/{columns → column_constraint}/widths_spec.rb +6 -6
  37. data/spec/unit/column_set/extract_widths_spec.rb +39 -6
  38. data/spec/unit/data_spec.rb +4 -6
  39. data/spec/unit/each_spec.rb +8 -23
  40. data/spec/unit/each_with_index_spec.rb +27 -33
  41. data/spec/unit/field/length_spec.rb +23 -9
  42. data/spec/unit/field/width_spec.rb +1 -1
  43. data/spec/unit/filter_spec.rb +7 -8
  44. data/spec/unit/header/new_spec.rb +6 -15
  45. data/spec/unit/indentation/indent_spec.rb +21 -0
  46. data/spec/unit/new_spec.rb +73 -0
  47. data/spec/unit/operation/{alignment_set → alignment}/call_spec.rb +1 -1
  48. data/spec/unit/operation/escape/call_spec.rb +2 -3
  49. data/spec/unit/operation/filter/call_spec.rb +2 -3
  50. data/spec/unit/operation/truncation/call_spec.rb +6 -8
  51. data/spec/unit/operation/wrapped/call_spec.rb +15 -8
  52. data/spec/unit/operations/new_spec.rb +1 -1
  53. data/spec/unit/orientation_spec.rb +6 -6
  54. data/spec/unit/padding_spec.rb +29 -32
  55. data/spec/unit/properties_spec.rb +4 -4
  56. data/spec/unit/render_repeat_spec.rb +42 -0
  57. data/spec/unit/render_spec.rb +1 -1
  58. data/spec/unit/render_with_spec.rb +3 -3
  59. data/spec/unit/renderer/ascii/coloring_spec.rb +70 -0
  60. data/spec/unit/renderer/ascii/multiline_spec.rb +101 -0
  61. data/spec/unit/renderer/ascii/padding_spec.rb +37 -10
  62. data/spec/unit/renderer/ascii/render_spec.rb +4 -4
  63. data/spec/unit/renderer/ascii/resizing_spec.rb +22 -22
  64. data/spec/unit/renderer/ascii/separator_spec.rb +1 -1
  65. data/spec/unit/renderer/basic/alignment_spec.rb +20 -20
  66. data/spec/unit/renderer/basic/coloring_spec.rb +43 -28
  67. data/spec/unit/renderer/basic/filter_spec.rb +3 -3
  68. data/spec/unit/renderer/basic/multiline_spec.rb +74 -0
  69. data/spec/unit/renderer/basic/options_spec.rb +9 -9
  70. data/spec/unit/renderer/basic/padding_spec.rb +26 -2
  71. data/spec/unit/renderer/basic/render_spec.rb +4 -4
  72. data/spec/unit/renderer/basic/resizing_spec.rb +18 -18
  73. data/spec/unit/renderer/basic/separator_spec.rb +1 -1
  74. data/spec/unit/renderer/basic/truncation_spec.rb +6 -6
  75. data/spec/unit/renderer/basic/wrapping_spec.rb +3 -3
  76. data/spec/unit/renderer/border_spec.rb +4 -4
  77. data/spec/unit/renderer/unicode/coloring_spec.rb +70 -0
  78. data/spec/unit/renderer/unicode/indentation_spec.rb +1 -1
  79. data/spec/unit/renderer/unicode/padding_spec.rb +26 -26
  80. data/spec/unit/renderer/unicode/render_spec.rb +4 -4
  81. data/spec/unit/renderer/unicode/separator_spec.rb +1 -1
  82. data/spec/unit/to_s_spec.rb +4 -11
  83. data/spec/unit/utf_spec.rb +33 -0
  84. data/tty-table.gemspec +2 -1
  85. metadata +52 -32
  86. data/lib/tty/table/operation/alignment_set.rb +0 -103
  87. data/lib/tty/table/padder.rb +0 -180
  88. data/lib/tty/table/renderer/color.rb +0 -12
  89. data/spec/unit/indentation/insert_indent_spec.rb +0 -27
  90. data/spec/unit/initialize_spec.rb +0 -88
  91. data/spec/unit/padder/parse_spec.rb +0 -45
  92. data/spec/unit/padder/to_s_spec.rb +0 -14
  93. data/spec/unit/renderer/basic/multiline_content_spec.rb +0 -135
  94. data/spec/unit/renderer/style_spec.rb +0 -72
@@ -45,16 +45,43 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'padding' do
45
45
  let(:options) { {padding: [1,1,1,1], multiline: true} }
46
46
 
47
47
  it 'pads each field' do
48
- expect(renderer.render).to eql <<-EOS.chomp
49
- +-------+---------+------+-----+---------+-------+
50
- | | | | | | |
51
- | Field | Type | Null | Key | Default | Extra |
52
- | | | | | | |
53
- +-------+---------+------+-----+---------+-------+
54
- | | | | | | |
55
- | id | int(11) | YES | nil | NULL | |
56
- | | | | | | |
57
- +-------+---------+------+-----+---------+-------+
48
+ expect(renderer.render).to eql unindent(<<-EOS)
49
+ +-------+---------+------+-----+---------+-------+
50
+ | | | | | | |
51
+ | Field | Type | Null | Key | Default | Extra |
52
+ | | | | | | |
53
+ +-------+---------+------+-----+---------+-------+
54
+ | | | | | | |
55
+ | id | int(11) | YES | nil | NULL | |
56
+ | | | | | | |
57
+ +-------+---------+------+-----+---------+-------+
58
+ EOS
59
+ end
60
+ end
61
+
62
+ context "with multiline content padding" do
63
+ it "pads around fields" do
64
+ padding = [1,2,1,2]
65
+ table = TTY::Table.new header: ['header1', 'header2']
66
+ table << ["a1\na1\na1",'a2']
67
+ table << ["b1","b2\nb2"]
68
+ renderer = described_class.new(table, padding: padding, multiline: true)
69
+ expect(renderer.render).to eql unindent(<<-EOS)
70
+ +-----------+-----------+
71
+ | | |
72
+ | header1 | header2 |
73
+ | | |
74
+ +-----------+-----------+
75
+ | | |
76
+ | a1 | a2 |
77
+ | a1 | |
78
+ | a1 | |
79
+ | | |
80
+ | | |
81
+ | b1 | b2 |
82
+ | | b2 |
83
+ | | |
84
+ +-----------+-----------+
58
85
  EOS
59
86
  end
60
87
  end
@@ -13,7 +13,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
13
13
  let(:table) { TTY::Table.new rows }
14
14
 
15
15
  it 'display table rows' do
16
- expect(renderer.render).to eq <<-EOS.normalize
16
+ expect(renderer.render).to eq unindent(<<-EOS)
17
17
  +--+--+--+
18
18
  |a1|a2|a3|
19
19
  |b1|b2|b3|
@@ -24,7 +24,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
24
24
 
25
25
  context 'with header' do
26
26
  it 'displays table with header' do
27
- expect(renderer.render).to eq <<-EOS.normalize
27
+ expect(renderer.render).to eq unindent(<<-EOS)
28
28
  +--+--+--+
29
29
  |h1|h2|h3|
30
30
  +--+--+--+
@@ -40,7 +40,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
40
40
  let(:rows) { [['aaa1', 'a2'], ['b1', 'bb1']] }
41
41
 
42
42
  it 'displays table according to widths' do
43
- expect(renderer.render).to eq <<-EOS.normalize
43
+ expect(renderer.render).to eq unindent(<<-EOS)
44
44
  +----+---+
45
45
  |h1 |h2 |
46
46
  +----+---+
@@ -55,7 +55,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, '#render' do
55
55
  let(:header) { ['header1', 'header2', 'header3'] }
56
56
 
57
57
  it 'header greater than row sizes' do
58
- expect(renderer.render).to eq <<-EOS.normalize
58
+ expect(renderer.render).to eq unindent(<<-EOS)
59
59
  +-------+-------+-------+
60
60
  |header1|header2|header3|
61
61
  +-------+-------+-------+
@@ -14,7 +14,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
14
14
  let(:options) { {width: 16, resize: true} }
15
15
 
16
16
  it 'resizes each column' do
17
- expect(renderer.render).to eql <<-EOS.normalize
17
+ expect(renderer.render).to eql unindent(<<-EOS)
18
18
  +----+----+----+
19
19
  |h1 |h2 |h3 |
20
20
  +----+----+----+
@@ -31,7 +31,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
31
31
  let(:options) { {width: 21, resize: true} }
32
32
 
33
33
  it 'resizes each column' do
34
- expect(renderer.render).to eql <<-EOS.normalize
34
+ expect(renderer.render).to eql unindent(<<-EOS)
35
35
  +----+----+----+----+
36
36
  |h1 |h2 |h3 |h4 |
37
37
  +----+----+----+----+
@@ -48,7 +48,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
48
48
  let(:options) { {width: 32, resize: true} }
49
49
 
50
50
  it 'resizes each column' do
51
- expect(renderer.render).to eql <<-EOS.normalize
51
+ expect(renderer.render).to eql unindent(<<-EOS)
52
52
  +---------+-------+------------+
53
53
  |h1 |h2 |h3 |
54
54
  +---------+-------+------------+
@@ -65,31 +65,31 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
65
65
  let(:rows) { [['aaaa1','aaaa2',], ['bbbb1','bbbb2']] }
66
66
 
67
67
  context 'even columns' do
68
- let(:options) { {width: 7, resize: true} }
68
+ let(:options) { {width: 9, resize: true} }
69
69
 
70
70
  it 'resizes each column' do
71
- expect(renderer.render).to eql <<-EOS.normalize
72
- +--+--+
73
- |h…|h…|
74
- +--+--+
75
- |a…|a…|
76
- |b…|b…|
77
- +--+--+
71
+ expect(renderer.render).to eql unindent(<<-EOS)
72
+ +---+---+
73
+ |h… |h… |
74
+ +---+---+
75
+ |a… |a… |
76
+ |b… |b… |
77
+ +---+---+
78
78
  EOS
79
79
  end
80
80
  end
81
81
 
82
82
  context 'even columns with extra width' do
83
- let(:options) { {width: 8, resize: true} }
83
+ let(:options) { {width: 10, resize: true} }
84
84
 
85
85
  it 'resizes each column' do
86
- expect(renderer.render).to eql <<-EOS.normalize
87
- +---+--+
88
- |he…|h…|
89
- +---+--+
90
- |aa…|a…|
91
- |bb…|b…|
92
- +---+--+
86
+ expect(renderer.render).to eql unindent(<<-EOS)
87
+ +----+---+
88
+ |he… |h… |
89
+ +----+---+
90
+ |aa… |a… |
91
+ |bb… |b… |
92
+ +----+---+
93
93
  EOS
94
94
  end
95
95
  end
@@ -100,11 +100,11 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'resizing' do
100
100
  let(:options) { {width: 15, resize: true} }
101
101
 
102
102
  it 'resizes each column' do
103
- expect(renderer.render).to eql <<-EOS.normalize
103
+ expect(renderer.render).to eql unindent(<<-EOS)
104
104
  +---+---+-----+
105
- |he…|he…|head3|
105
+ |h… |h… |head3|
106
106
  +---+---+-----+
107
- |aa…|aa2|aaaa…|
107
+ |a… |aa2|aaa… |
108
108
  |b1 |b2 |b3 |
109
109
  +---+---+-----+
110
110
  EOS
@@ -14,7 +14,7 @@ RSpec.describe TTY::Table::Renderer::ASCII, 'with separator' do
14
14
  context 'when ascii' do
15
15
  it "renders each row" do
16
16
  renderer.border.separator= :each_row
17
- expect(renderer.render).to eq <<-EOS.normalize
17
+ expect(renderer.render).to eq unindent(<<-EOS)
18
18
  +--+--+--+
19
19
  |h1|h2|h3|
20
20
  +--+--+--+
@@ -5,18 +5,18 @@ require 'spec_helper'
5
5
  RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
6
6
  let(:header) { ['h1', 'h2', 'h3'] }
7
7
  let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
- let(:options) { { column_aligns: column_aligns } }
8
+ let(:options) { { alignments: alignments } }
9
9
  let(:table) { TTY::Table.new(header, rows) }
10
10
 
11
11
  subject(:renderer) { described_class.new table, options }
12
12
 
13
13
  context 'with default' do
14
- let(:header) { ['h1', 'h2'] }
15
- let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
16
- let(:column_aligns) { nil }
14
+ let(:header) { ['h1', 'h2'] }
15
+ let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
16
+ let(:alignments) { nil }
17
17
 
18
18
  it 'aligns left by default' do
19
- expect(renderer.render).to eql <<-EOS.normalize
19
+ expect(renderer.render).to eql unindent(<<-EOS)
20
20
  h1 h2
21
21
  aaaaa a
22
22
  b bbbbb
@@ -25,11 +25,11 @@ RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
25
25
  end
26
26
 
27
27
  context 'with different headers' do
28
- let(:header) { ['header1', 'head2', 'h3'] }
29
- let(:column_aligns) { [:left, :center, :right] }
28
+ let(:header) { ['header1', 'head2', 'h3'] }
29
+ let(:alignments) { [:left, :center, :right] }
30
30
 
31
31
  it 'aligns headers' do
32
- expect(renderer.render).to eql <<-EOS.normalize
32
+ expect(renderer.render).to eql unindent(<<-EOS)
33
33
  header1 head2 h3
34
34
  a1 a2 a3
35
35
  b1 b2 b3
@@ -38,12 +38,12 @@ RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
38
38
  end
39
39
 
40
40
  context 'with different aligns' do
41
- let(:header) { nil }
42
- let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
43
- let(:column_aligns) { [:left, :right] }
41
+ let(:header) { nil }
42
+ let(:rows) { [['aaaaa', 'a'], ['b', 'bbbbb']] }
43
+ let(:alignments) { [:left, :right] }
44
44
 
45
45
  it 'aligns table rows' do
46
- expect(renderer.render.to_s).to eql <<-EOS.normalize
46
+ expect(renderer.render.to_s).to eql unindent(<<-EOS)
47
47
  aaaaa a
48
48
  b bbbbb
49
49
  EOS
@@ -51,19 +51,19 @@ RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
51
51
  end
52
52
 
53
53
  context 'with individual field aligns' do
54
- let(:header) { ['header1', 'header2', 'header3'] }
55
- let(:column_aligns) { [:left, :center, :right] }
56
- let(:options) { {column_aligns: column_aligns} }
54
+ let(:header) { ['header1', 'header2', 'header3'] }
55
+ let(:alignments) { [:left, :center, :right] }
56
+ let(:options) { {alignments: alignments} }
57
57
  let(:table) {
58
58
  TTY::Table.new header: header do |t|
59
59
  t << ['a1', 'a2', 'a3']
60
- t << ['b1', {value: 'b2', align: :right}, 'b3']
61
- t << ['c1', 'c2', {value: 'c3', align: :center}]
60
+ t << ['b1', {value: 'b2', alignment: :right}, 'b3']
61
+ t << ['c1', 'c2', {value: 'c3', alignment: :center}]
62
62
  end
63
63
  }
64
64
 
65
65
  it "takes individual fields over global aligns" do
66
- expect(renderer.render).to eq <<-EOS.normalize
66
+ expect(renderer.render).to eq unindent(<<-EOS)
67
67
  header1 header2 header3
68
68
  a1 a2 a3
69
69
  b1 b2 b3
@@ -74,11 +74,11 @@ RSpec.describe TTY::Table::Renderer::Basic, 'alignment' do
74
74
 
75
75
  context 'with aligned header' do
76
76
  let(:rows) { [['aaaaa1', 'a2', 'aaa3'], ['b1', 'bbbb2', 'bb3']] }
77
- let(:header) {['h1', {:value => 'h2', :align => :right}, {:value => 'h3', :align => :center}] }
77
+ let(:header) {['h1', {value: 'h2', alignment: :right}, {value: 'h3', alignment: :center}] }
78
78
  let(:options) { { renderer: :basic } }
79
79
 
80
80
  it "aligns headres" do
81
- expect(renderer.render).to eq <<-EOS.normalize
81
+ expect(renderer.render).to eq unindent(<<-EOS)
82
82
  h1 h2 h3
83
83
  aaaaa1 a2 aaa3
84
84
  b1 bbbb2 bb3
@@ -3,43 +3,58 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Table::Renderer::Basic, 'coloring' do
6
- let(:header) { ['h1', 'h2'] }
7
- let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
8
6
  let(:clear) { "\e[0m" }
9
- let(:options) { {filter: filter } }
10
- let(:table) { TTY::Table.new(header, rows) }
11
7
  let(:color) { Pastel.new(enabled: true) }
12
8
 
13
- subject(:renderer) { described_class.new(table, options) }
14
-
15
9
  before { allow(Pastel).to receive(:new).and_return(color) }
16
10
 
17
- context 'with filter on all fields' do
18
- let(:filter) {
19
- proc { |val, row, col| color.decorate val, :blue, :on_green }
20
- }
21
-
22
- it 'colors all elements' do
23
- expect(renderer.render).to eql <<-EOS.normalize
24
- \e[34;42mh1#{clear} \e[34;42mh2#{clear}
25
- \e[34;42ma1#{clear} \e[34;42ma2#{clear}
26
- \e[34;42mb1#{clear} \e[34;42mb2#{clear}
11
+ context 'when border ' do
12
+ it "doesn't color" do
13
+ table = TTY::Table.new header: ['header1', 'header2']
14
+ table << ['a1', 'a2']
15
+ table << ['b1', 'b2']
16
+ renderer = described_class.new(table)
17
+ renderer.border = {style: :green}
18
+
19
+ expect(renderer.render).to eql unindent(<<-EOS)
20
+ header1 header2
21
+ a1 a2
22
+ b1 b2
27
23
  EOS
28
24
  end
29
25
  end
30
26
 
31
- context 'with filter only on header' do
32
- let(:filter) {
33
- proc { |val, row, col|
34
- row.zero? ? color.decorate(val, :blue, :on_green) : val
35
- }
36
- }
37
-
38
- it 'colors only header' do
39
- expect(renderer.render).to eql <<-EOS.normalize
40
- \e[34;42mh1#{clear} \e[34;42mh2#{clear}
41
- a1 a2
42
- b1 b2
27
+ context 'when content' do
28
+ it "colors individual field" do
29
+ header = [color.green('header1'), 'header2']
30
+ table = TTY::Table.new header: header
31
+ table << [color.green.on_blue('a1'), 'a2']
32
+ table << ['b1', color.red.on_yellow('b2')]
33
+ renderer = described_class.new(table)
34
+
35
+ expect(renderer.render).to eq unindent(<<-EOS)
36
+ #{color.green('header1')} header2
37
+ #{color.green.on_blue('a1')} a2
38
+ b1 #{color.red.on_yellow('b2')}
39
+ EOS
40
+ end
41
+
42
+ it "colors multiline content" do
43
+ header = [color.yellow("Multi\nHeader"), 'header2']
44
+ table = TTY::Table.new header: header
45
+ table << [color.green.on_blue("Multi\nLine\nContent"), 'a2']
46
+ table << ['b1', color.red.on_yellow("Multi\nLine\nContent")]
47
+ renderer = described_class.new(table, multiline: true)
48
+
49
+ expect(renderer.render).to eq unindent(<<-EOS)
50
+ #{color.yellow('Multi ')} header2
51
+ #{color.yellow('Header')}
52
+ #{color.green.on_blue('Multi ')} a2
53
+ #{color.green.on_blue('Line ')}
54
+ #{color.green.on_blue('Content')}
55
+ b1 #{color.red.on_yellow("Multi ")}
56
+ #{color.red.on_yellow("Line ")}
57
+ #{color.red.on_yellow("Content")}
43
58
  EOS
44
59
  end
45
60
  end
@@ -17,7 +17,7 @@ RSpec.describe TTY::Table::Renderer::Basic, 'filter' do
17
17
  }
18
18
 
19
19
  it 'filters only rows' do
20
- expect(renderer.render).to eq <<-EOS.normalize
20
+ expect(renderer.render).to eq unindent(<<-EOS)
21
21
  h1 h2 h3
22
22
  a1 A2 a3
23
23
  b1 B2 b3
@@ -29,7 +29,7 @@ RSpec.describe TTY::Table::Renderer::Basic, 'filter' do
29
29
  let(:filter) { Proc.new { |val, row, col| col == 1 ? val.capitalize : val }}
30
30
 
31
31
  it 'filters only rows' do
32
- expect(renderer.render).to eq <<-EOS.normalize
32
+ expect(renderer.render).to eq unindent(<<-EOS)
33
33
  h1 H2 h3
34
34
  a1 A2 a3
35
35
  b1 B2 b3
@@ -44,7 +44,7 @@ RSpec.describe TTY::Table::Renderer::Basic, 'filter' do
44
44
  let(:filter) { Proc.new { |val, row, col| col == 1 ? val.capitalize : val } }
45
45
 
46
46
  it 'filters only rows' do
47
- expect(renderer.render).to eq <<-EOS.normalize
47
+ expect(renderer.render).to eq unindent(<<-EOS)
48
48
  a1 A2 a3
49
49
  b1 B2 b3
50
50
  EOS
@@ -0,0 +1,74 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Renderer::Basic, 'multiline content' do
6
+ context 'with escaping' do
7
+ it "renders multiline as single line" do
8
+ rows = [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
9
+ table = TTY::Table.new rows
10
+ renderer = described_class.new(table, multiline: false)
11
+ expect(renderer.render).to eq unindent(<<-EOS)
12
+ First 1
13
+ Multiline\\nContent 2
14
+ Third 3
15
+ EOS
16
+ end
17
+
18
+ it "truncates multiline content" do
19
+ rows = [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
20
+ table = TTY::Table.new rows
21
+ renderer = described_class.new(table, multiline: false, column_widths: [8,1])
22
+ expect(renderer.render).to eq unindent(<<-EOS)
23
+ First 1
24
+ Multil… 2
25
+ Third 3
26
+ EOS
27
+ end
28
+ end
29
+
30
+ context 'without escaping' do
31
+ it "renders every line" do
32
+ rows = [["First", '1'],
33
+ ["Multi\nLine\nContent", '2'],
34
+ ["Third", "Multi\nLine"]]
35
+ table = TTY::Table.new rows
36
+ renderer = described_class.new(table, multiline: true)
37
+ expect(renderer.render).to eq unindent(<<-EOS)
38
+ First 1
39
+ Multi 2
40
+ Line
41
+ Content
42
+ Third Multi
43
+ Line
44
+ EOS
45
+ end
46
+
47
+ it "renders multiline with column widths" do
48
+ rows = [["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
49
+ table = TTY::Table.new rows
50
+ renderer = described_class.new(table, multiline: true, column_widths: [8,1])
51
+ expect(renderer.render).to eq unindent(<<-EOS)
52
+ First 1
53
+ Multi 2
54
+ Line
55
+ Content
56
+ Third 3
57
+ EOS
58
+ end
59
+
60
+ it 'wraps multi line' do
61
+ rows = [["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
62
+ table = TTY::Table.new rows
63
+ renderer = described_class.new(table, multiline: true, column_widths: [5,1])
64
+ expect(renderer.render).to eq unindent(<<-EOS)
65
+ First 1
66
+ Multi 2
67
+ Line
68
+ Conte
69
+ nt
70
+ Third 3
71
+ EOS
72
+ end
73
+ end
74
+ end