tty-table 0.1.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.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +22 -0
  6. data/Gemfile +19 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +389 -0
  9. data/Rakefile +8 -0
  10. data/benchmarks/speed.rb +36 -0
  11. data/lib/tty/table/border/ascii.rb +32 -0
  12. data/lib/tty/table/border/null.rb +37 -0
  13. data/lib/tty/table/border/row_line.rb +18 -0
  14. data/lib/tty/table/border/unicode.rb +32 -0
  15. data/lib/tty/table/border.rb +219 -0
  16. data/lib/tty/table/border_dsl.rb +251 -0
  17. data/lib/tty/table/border_options.rb +53 -0
  18. data/lib/tty/table/column_set.rb +121 -0
  19. data/lib/tty/table/columns.rb +170 -0
  20. data/lib/tty/table/empty.rb +26 -0
  21. data/lib/tty/table/error.rb +39 -0
  22. data/lib/tty/table/field.rb +139 -0
  23. data/lib/tty/table/header.rb +163 -0
  24. data/lib/tty/table/indentation.rb +52 -0
  25. data/lib/tty/table/operation/alignment_set.rb +103 -0
  26. data/lib/tty/table/operation/escape.rb +30 -0
  27. data/lib/tty/table/operation/filter.rb +34 -0
  28. data/lib/tty/table/operation/padding.rb +95 -0
  29. data/lib/tty/table/operation/truncation.rb +41 -0
  30. data/lib/tty/table/operation/wrapped.rb +43 -0
  31. data/lib/tty/table/operations.rb +69 -0
  32. data/lib/tty/table/options.rb +30 -0
  33. data/lib/tty/table/orientation/horizontal.rb +48 -0
  34. data/lib/tty/table/orientation/vertical.rb +38 -0
  35. data/lib/tty/table/orientation.rb +57 -0
  36. data/lib/tty/table/padder.rb +180 -0
  37. data/lib/tty/table/renderer/ascii.rb +16 -0
  38. data/lib/tty/table/renderer/basic.rb +294 -0
  39. data/lib/tty/table/renderer/color.rb +12 -0
  40. data/lib/tty/table/renderer/unicode.rb +21 -0
  41. data/lib/tty/table/renderer.rb +101 -0
  42. data/lib/tty/table/row.rb +248 -0
  43. data/lib/tty/table/transformation.rb +39 -0
  44. data/lib/tty/table/validatable.rb +64 -0
  45. data/lib/tty/table/version.rb +7 -0
  46. data/lib/tty/table.rb +469 -0
  47. data/lib/tty-table.rb +48 -0
  48. data/spec/spec_helper.rb +51 -0
  49. data/spec/unit/access_spec.rb +86 -0
  50. data/spec/unit/add_row_spec.rb +28 -0
  51. data/spec/unit/border/ascii/rendering_spec.rb +90 -0
  52. data/spec/unit/border/new_spec.rb +27 -0
  53. data/spec/unit/border/null/rendering_spec.rb +130 -0
  54. data/spec/unit/border/options/from_spec.rb +38 -0
  55. data/spec/unit/border/options/new_spec.rb +14 -0
  56. data/spec/unit/border/unicode/rendering_spec.rb +63 -0
  57. data/spec/unit/border_options/new_spec.rb +20 -0
  58. data/spec/unit/border_options/update_spec.rb +18 -0
  59. data/spec/unit/column_set/extract_widths_spec.rb +15 -0
  60. data/spec/unit/column_set/total_width_spec.rb +15 -0
  61. data/spec/unit/column_set/widths_from_spec.rb +51 -0
  62. data/spec/unit/columns/enforce_spec.rb +67 -0
  63. data/spec/unit/columns/widths_spec.rb +35 -0
  64. data/spec/unit/data_spec.rb +14 -0
  65. data/spec/unit/each_spec.rb +41 -0
  66. data/spec/unit/each_with_index_spec.rb +57 -0
  67. data/spec/unit/empty_spec.rb +23 -0
  68. data/spec/unit/eql_spec.rb +34 -0
  69. data/spec/unit/field/equality_spec.rb +51 -0
  70. data/spec/unit/field/length_spec.rb +21 -0
  71. data/spec/unit/field/lines_spec.rb +21 -0
  72. data/spec/unit/field/new_spec.rb +29 -0
  73. data/spec/unit/field/width_spec.rb +23 -0
  74. data/spec/unit/filter_spec.rb +23 -0
  75. data/spec/unit/header/call_spec.rb +30 -0
  76. data/spec/unit/header/color_spec.rb +19 -0
  77. data/spec/unit/header/equality_spec.rb +51 -0
  78. data/spec/unit/header/height_spec.rb +27 -0
  79. data/spec/unit/header/new_spec.rb +25 -0
  80. data/spec/unit/header/set_spec.rb +20 -0
  81. data/spec/unit/header/to_ary_spec.rb +14 -0
  82. data/spec/unit/header_spec.rb +13 -0
  83. data/spec/unit/indentation/insert_indent_spec.rb +27 -0
  84. data/spec/unit/initialize_spec.rb +88 -0
  85. data/spec/unit/operation/alignment_set/call_spec.rb +39 -0
  86. data/spec/unit/operation/alignment_set/each_spec.rb +17 -0
  87. data/spec/unit/operation/alignment_set/new_spec.rb +27 -0
  88. data/spec/unit/operation/alignment_set/to_ary_spec.rb +14 -0
  89. data/spec/unit/operation/escape/call_spec.rb +16 -0
  90. data/spec/unit/operation/filter/call_spec.rb +17 -0
  91. data/spec/unit/operation/truncation/call_spec.rb +32 -0
  92. data/spec/unit/operation/wrapped/call_spec.rb +33 -0
  93. data/spec/unit/operations/new_spec.rb +30 -0
  94. data/spec/unit/options/access_spec.rb +14 -0
  95. data/spec/unit/options_spec.rb +25 -0
  96. data/spec/unit/orientation_spec.rb +145 -0
  97. data/spec/unit/padder/parse_spec.rb +45 -0
  98. data/spec/unit/padder/to_s_spec.rb +14 -0
  99. data/spec/unit/padding_spec.rb +120 -0
  100. data/spec/unit/properties_spec.rb +25 -0
  101. data/spec/unit/render_spec.rb +63 -0
  102. data/spec/unit/render_with_spec.rb +106 -0
  103. data/spec/unit/renderer/ascii/indentation_spec.rb +41 -0
  104. data/spec/unit/renderer/ascii/padding_spec.rb +61 -0
  105. data/spec/unit/renderer/ascii/render_spec.rb +68 -0
  106. data/spec/unit/renderer/ascii/resizing_spec.rb +114 -0
  107. data/spec/unit/renderer/ascii/separator_spec.rb +28 -0
  108. data/spec/unit/renderer/basic/alignment_spec.rb +88 -0
  109. data/spec/unit/renderer/basic/coloring_spec.rb +46 -0
  110. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +28 -0
  111. data/spec/unit/renderer/basic/filter_spec.rb +53 -0
  112. data/spec/unit/renderer/basic/indentation_spec.rb +48 -0
  113. data/spec/unit/renderer/basic/multiline_content_spec.rb +135 -0
  114. data/spec/unit/renderer/basic/new_spec.rb +26 -0
  115. data/spec/unit/renderer/basic/options_spec.rb +52 -0
  116. data/spec/unit/renderer/basic/padding_spec.rb +52 -0
  117. data/spec/unit/renderer/basic/render_spec.rb +57 -0
  118. data/spec/unit/renderer/basic/resizing_spec.rb +96 -0
  119. data/spec/unit/renderer/basic/separator_spec.rb +43 -0
  120. data/spec/unit/renderer/basic/truncation_spec.rb +35 -0
  121. data/spec/unit/renderer/basic/wrapping_spec.rb +40 -0
  122. data/spec/unit/renderer/border_spec.rb +104 -0
  123. data/spec/unit/renderer/render_spec.rb +36 -0
  124. data/spec/unit/renderer/select_spec.rb +22 -0
  125. data/spec/unit/renderer/style_spec.rb +72 -0
  126. data/spec/unit/renderer/unicode/indentation_spec.rb +41 -0
  127. data/spec/unit/renderer/unicode/padding_spec.rb +61 -0
  128. data/spec/unit/renderer/unicode/render_spec.rb +68 -0
  129. data/spec/unit/renderer/unicode/separator_spec.rb +26 -0
  130. data/spec/unit/renderer_spec.rb +19 -0
  131. data/spec/unit/rotate_spec.rb +86 -0
  132. data/spec/unit/row/access_spec.rb +25 -0
  133. data/spec/unit/row/call_spec.rb +45 -0
  134. data/spec/unit/row/data_spec.rb +26 -0
  135. data/spec/unit/row/each_spec.rb +31 -0
  136. data/spec/unit/row/equality_spec.rb +73 -0
  137. data/spec/unit/row/height_spec.rb +27 -0
  138. data/spec/unit/row/new_spec.rb +41 -0
  139. data/spec/unit/row/to_ary_spec.rb +14 -0
  140. data/spec/unit/to_s_spec.rb +63 -0
  141. data/spec/unit/transformation/extract_tuples_spec.rb +35 -0
  142. data/spec/unit/validatable/validate_options_spec.rb +33 -0
  143. data/spec/unit/validatable_spec.rb +32 -0
  144. data/tasks/console.rake +10 -0
  145. data/tasks/coverage.rake +11 -0
  146. data/tasks/spec.rake +29 -0
  147. data/tty-table.gemspec +28 -0
  148. metadata +371 -0
@@ -0,0 +1,73 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Row, '#==' do
6
+ let(:attributes) { [:id] }
7
+ let(:data) { ['1'] }
8
+ let(:object) { described_class.new(data, attributes) }
9
+
10
+ subject { object == other }
11
+
12
+ context 'with the same object' do
13
+ let(:other) { object }
14
+
15
+ it { is_expected.to eql(true) }
16
+
17
+ it 'is symmetric' do
18
+ is_expected.to eql(other == object)
19
+ end
20
+ end
21
+
22
+ context 'with an equivalent object' do
23
+ let(:other) { object.dup }
24
+
25
+ it { is_expected.to eql(true) }
26
+
27
+ it 'is symmetric' do
28
+ is_expected.to eql(other == object)
29
+ end
30
+ end
31
+
32
+ context 'with an equivalent object of subclass' do
33
+ let(:other) { Class.new(described_class).new(data, attributes: attributes) }
34
+
35
+ it { is_expected.to eql(true) }
36
+
37
+ it 'is symmetric' do
38
+ is_expected.to eql(other == object)
39
+ end
40
+ end
41
+
42
+ context 'with an object having a different attributes' do
43
+ let(:other_attributes) { [:text] }
44
+ let(:other) { described_class.new(data, attributes: other_attributes) }
45
+
46
+ it { is_expected.to eql(true) }
47
+
48
+ it 'is symmetric' do
49
+ is_expected.to eql(other == object)
50
+ end
51
+ end
52
+
53
+ context 'with an object having a different attributes' do
54
+ let(:other_data) { [2] }
55
+ let(:other) { described_class.new(other_data, attributes: attributes) }
56
+
57
+ it { is_expected.to eql(false) }
58
+
59
+ it 'is symmetric' do
60
+ is_expected.to eql(other == object)
61
+ end
62
+ end
63
+
64
+ context 'with an equivalent object responding to_ary' do
65
+ let(:other) { data }
66
+
67
+ it { is_expected.to eql(true) }
68
+
69
+ it 'is symmetric' do
70
+ is_expected.to eql(other == object)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Row, '#height' do
6
+ let(:object) { described_class.new row }
7
+
8
+ subject { object.height }
9
+
10
+ context 'single row' do
11
+ let(:row) { ['a1', 'b1'] }
12
+
13
+ it { expect(subject).to eql(1) }
14
+ end
15
+
16
+ context 'non escaped multiline' do
17
+ let(:row) { ["a1\na2\na3", 'b1'] }
18
+
19
+ it { expect(subject).to eql(3)}
20
+ end
21
+
22
+ context 'escaped multiline' do
23
+ let(:row) { ["a1\\na2\\na3", 'b1'] }
24
+
25
+ it { expect(subject).to eql(1) }
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Row, '#new' do
6
+ let(:object) { described_class }
7
+
8
+ subject { object.new data }
9
+
10
+ context 'with no arguments' do
11
+ let(:data) { [] }
12
+
13
+ it { is_expected.to be_instance_of(object) }
14
+
15
+ it { is_expected.to be_empty }
16
+
17
+ it { expect(subject.attributes).to eq([]) }
18
+
19
+ it { expect(subject.data).to eq({}) }
20
+ end
21
+
22
+ context 'with Array argument' do
23
+ let(:data) { ['a'] }
24
+
25
+ it { is_expected.to be_instance_of(object) }
26
+
27
+ it { expect(subject.attributes).to eq([0]) }
28
+
29
+ it { expect(subject.to_hash).to eq({0 => "a"}) }
30
+ end
31
+
32
+ context 'with Hash argument' do
33
+ let(:data) { {id: 'a'} }
34
+
35
+ it { should be_instance_of(object) }
36
+
37
+ it { expect(subject.attributes).to eq([:id]) }
38
+
39
+ it { expect(subject.to_hash).to eq({:id => 'a'}) }
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Row, '#to_ary' do
6
+ let(:object) { described_class.new data }
7
+ let(:data) { ['a', 'b'] }
8
+
9
+ subject { object.to_ary }
10
+
11
+ it { is_expected.to be_instance_of(Array) }
12
+
13
+ it { is_expected.to eq(data) }
14
+ end
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table, '#to_s' do
6
+ let(:header) { ['h1', 'h2', 'h3'] }
7
+ let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
+ let(:renderer) { :basic }
9
+
10
+ subject(:table) { described_class.new(header, rows) }
11
+
12
+ context 'without renderer' do
13
+ let(:renderer) { nil }
14
+
15
+ it 'displayes basic table' do
16
+ expect(table.to_s).to eq <<-EOS.normalize
17
+ h1 h2 h3
18
+ a1 a2 a3
19
+ b1 b2 b3
20
+ EOS
21
+ end
22
+ end
23
+
24
+ context 'without border' do
25
+ it 'displays table' do
26
+ expect(table.to_s).to eq <<-EOS.normalize
27
+ h1 h2 h3
28
+ a1 a2 a3
29
+ b1 b2 b3
30
+ EOS
31
+ end
32
+ end
33
+
34
+ context 'with ascii border' do
35
+ let(:renderer) { :ascii }
36
+
37
+ it 'displays table' do
38
+ expect(table.render(renderer)).to eq <<-EOS.normalize
39
+ +--+--+--+
40
+ |h1|h2|h3|
41
+ +--+--+--+
42
+ |a1|a2|a3|
43
+ |b1|b2|b3|
44
+ +--+--+--+
45
+ EOS
46
+ end
47
+ end
48
+
49
+ context 'with unicode border' do
50
+ let(:renderer) { :unicode}
51
+
52
+ it 'displays table' do
53
+ expect(table.render(renderer)).to eq <<-EOS.normalize
54
+ ┌──┬──┬──┐
55
+ │h1│h2│h3│
56
+ ├──┼──┼──┤
57
+ │a1│a2│a3│
58
+ │b1│b2│b3│
59
+ └──┴──┴──┘
60
+ EOS
61
+ end
62
+ end
63
+ end # to_s
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Transformation, '#extract_tuples' do
6
+ let(:object) { described_class }
7
+ let(:header) { ['Header1', 'Header2'] }
8
+ let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
9
+
10
+ subject { object.extract_tuples(value) }
11
+
12
+ context 'when rows' do
13
+ let(:value) { [rows] }
14
+
15
+ it { expect(subject[:header]).to be_nil }
16
+
17
+ it { expect(subject[:rows]).to eql(rows) }
18
+ end
19
+
20
+ context 'when header and rows' do
21
+ let(:value) { [header, rows] }
22
+
23
+ it { expect(subject[:header]).to eql(header) }
24
+
25
+ it { expect(subject[:rows]).to eql(rows) }
26
+ end
27
+
28
+ context 'when hash' do
29
+ let(:value) { [[{'Header1' => ['a1', 'a2'], 'Header2' => ['b1', 'b2'] }]] }
30
+
31
+ it { expect(subject[:header]).to eql(header) }
32
+
33
+ it { expect(subject[:rows]).to eql(rows) }
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Validatable, '#validate_options!' do
6
+ let(:described_class) { Class.new { include TTY::Table::Validatable } }
7
+
8
+ subject { described_class.new.validate_options! options }
9
+
10
+ context 'with empty rows' do
11
+ let(:options) { {rows: []} }
12
+
13
+ it { expect { subject }.not_to raise_error() }
14
+ end
15
+
16
+ context 'with invalid rows type' do
17
+ let(:options) { {rows: 1 } }
18
+
19
+ it { expect { subject }.to raise_error(TTY::Table::InvalidArgument) }
20
+ end
21
+
22
+ context 'with empty header' do
23
+ let(:options) { {header: []} }
24
+
25
+ it { expect { subject }.to raise_error(TTY::Table::InvalidArgument) }
26
+ end
27
+
28
+ context 'with invalid header type' do
29
+ let(:options) { {header: 1} }
30
+
31
+ it { expect { subject }.to raise_error(TTY::Table::InvalidArgument) }
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Table::Validatable do
6
+ let(:described_class) { Class.new { include TTY::Table::Validatable } }
7
+ let(:rows) { [['a1', 'a2'], ['b1']] }
8
+
9
+ subject { described_class.new }
10
+
11
+ it 'raises no exception' do
12
+ rows[1] << ['b2']
13
+ expect { subject.assert_row_sizes(rows) }.not_to raise_error
14
+ end
15
+
16
+ it 'raises exception for mismatched rows' do
17
+ expect { subject.assert_row_sizes(rows) }.
18
+ to raise_error(TTY::Table::DimensionMismatchError)
19
+ end
20
+
21
+ it "raises exception when :header key has wrong data type" do
22
+ options = {header: 'h1'}
23
+ expect { subject.validate_options!(options) }.
24
+ to raise_error(TTY::Table::InvalidArgument)
25
+ end
26
+
27
+ it "raises exception when :rows key has wrong data type" do
28
+ options = {rows: 'a1'}
29
+ expect { subject.validate_options!(options) }.
30
+ to raise_error(TTY::Table::InvalidArgument)
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/tty-table')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
data/tty-table.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tty/table/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'tty-table'
8
+ spec.version = TTY::Table::VERSION
9
+ spec.authors = ["Piotr Murach"]
10
+ spec.email = [""]
11
+ spec.summary = %q{A flexible and intuitive table generator}
12
+ spec.description = %q{A flexible and intuitive table generator}
13
+ spec.homepage = "https://github.com/peter-murach/tty-table"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'equatable', '~> 0.5'
22
+ spec.add_dependency 'necromancer', '~> 0.3'
23
+ spec.add_dependency 'pastel', '~> 0.4'
24
+ spec.add_dependency 'tty-screen', '~> 0.1'
25
+ spec.add_dependency 'verse', '~> 0.1'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.5'
28
+ end