tty 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/.rspec +2 -1
  2. data/.travis.yml +3 -6
  3. data/README.md +232 -134
  4. data/lib/tty/plugins/plugin.rb +56 -0
  5. data/lib/tty/plugins.rb +75 -0
  6. data/lib/tty/shell/suggestion.rb +102 -0
  7. data/lib/tty/shell.rb +41 -14
  8. data/lib/tty/system/editor.rb +111 -0
  9. data/lib/tty/system/which.rb +13 -1
  10. data/lib/tty/system.rb +44 -28
  11. data/lib/tty/table/border/null.rb +0 -9
  12. data/lib/tty/table/border/row_line.rb +21 -0
  13. data/lib/tty/table/border.rb +63 -32
  14. data/lib/tty/table/border_dsl.rb +1 -1
  15. data/lib/tty/table/column_set.rb +16 -17
  16. data/lib/tty/table/field.rb +27 -7
  17. data/lib/tty/table/header.rb +18 -9
  18. data/lib/tty/table/operation/alignment_set.rb +20 -25
  19. data/lib/tty/table/operation/escape.rb +30 -0
  20. data/lib/tty/table/operation/filter.rb +36 -0
  21. data/lib/tty/table/operation/truncation.rb +22 -11
  22. data/lib/tty/table/operation/wrapped.rb +21 -10
  23. data/lib/tty/table/operations.rb +10 -8
  24. data/lib/tty/table/orientation/horizontal.rb +1 -1
  25. data/lib/tty/table/renderer/ascii.rb +3 -3
  26. data/lib/tty/table/renderer/basic.rb +135 -65
  27. data/lib/tty/table/renderer/color.rb +1 -4
  28. data/lib/tty/table/renderer/unicode.rb +3 -3
  29. data/lib/tty/table/renderer.rb +48 -61
  30. data/lib/tty/table/row.rb +30 -3
  31. data/lib/tty/table/transformation.rb +38 -0
  32. data/lib/tty/table/validatable.rb +7 -5
  33. data/lib/tty/table.rb +78 -99
  34. data/lib/tty/terminal/color.rb +2 -2
  35. data/lib/tty/terminal/echo.rb +1 -1
  36. data/lib/tty/terminal/pager/basic.rb +52 -0
  37. data/lib/tty/terminal/pager/system.rb +39 -0
  38. data/lib/tty/terminal/pager.rb +95 -0
  39. data/lib/tty/terminal.rb +30 -1
  40. data/lib/tty/version.rb +1 -1
  41. data/lib/tty.rb +41 -1
  42. data/spec/spec_helper.rb +20 -0
  43. data/spec/tty/plugins/find_spec.rb +28 -0
  44. data/spec/tty/plugins/load_spec.rb +20 -0
  45. data/spec/tty/plugins/plugin/load_spec.rb +30 -0
  46. data/spec/tty/plugins/plugin/new_spec.rb +18 -0
  47. data/spec/tty/shell/suggest_spec.rb +50 -0
  48. data/spec/tty/support/conversion_spec.rb +3 -3
  49. data/spec/tty/support/delegatable_spec.rb +1 -1
  50. data/spec/tty/support/equatable_spec.rb +6 -9
  51. data/spec/tty/system/editor/available_spec.rb +40 -0
  52. data/spec/tty/system/editor/build_spec.rb +40 -0
  53. data/spec/tty/system/editor/command_spec.rb +16 -0
  54. data/spec/tty/system/editor/executables_spec.rb +13 -0
  55. data/spec/tty/system/editor/invoke_spec.rb +38 -0
  56. data/spec/tty/system/editor/open_spec.rb +27 -0
  57. data/spec/tty/system/platform_spec.rb +4 -6
  58. data/spec/tty/system/which/which_spec.rb +48 -0
  59. data/spec/tty/system/which_spec.rb +8 -34
  60. data/spec/tty/table/border/ascii/rendering_spec.rb +19 -5
  61. data/spec/tty/table/border/new_spec.rb +1 -1
  62. data/spec/tty/table/border/null/rendering_spec.rb +24 -8
  63. data/spec/tty/table/border/unicode/rendering_spec.rb +19 -5
  64. data/spec/tty/table/column_set/extract_widths_spec.rb +4 -15
  65. data/spec/tty/table/column_set/total_width_spec.rb +15 -0
  66. data/spec/tty/table/data_spec.rb +14 -0
  67. data/spec/tty/table/each_spec.rb +17 -4
  68. data/spec/tty/table/each_with_index_spec.rb +34 -6
  69. data/spec/tty/table/field/length_spec.rb +21 -0
  70. data/spec/tty/table/field/lines_spec.rb +21 -0
  71. data/spec/tty/table/filter_spec.rb +23 -0
  72. data/spec/tty/table/header/call_spec.rb +1 -1
  73. data/spec/tty/table/header/height_spec.rb +27 -0
  74. data/spec/tty/table/initialize_spec.rb +6 -6
  75. data/spec/tty/table/operation/alignment_set/call_spec.rb +39 -0
  76. data/spec/tty/table/operation/escape/call_spec.rb +16 -0
  77. data/spec/tty/table/operation/filter/call_spec.rb +17 -0
  78. data/spec/tty/table/operation/truncation/call_spec.rb +15 -10
  79. data/spec/tty/table/operation/truncation/truncate_spec.rb +1 -1
  80. data/spec/tty/table/operation/wrapped/call_spec.rb +15 -10
  81. data/spec/tty/table/operation/wrapped/wrap_spec.rb +1 -1
  82. data/spec/tty/table/operations/new_spec.rb +4 -4
  83. data/spec/tty/table/options_spec.rb +0 -28
  84. data/spec/tty/table/orientation_spec.rb +5 -6
  85. data/spec/tty/table/properties_spec.rb +1 -4
  86. data/spec/tty/table/render_spec.rb +57 -0
  87. data/spec/tty/table/{renders_with_spec.rb → render_with_spec.rb} +29 -10
  88. data/spec/tty/table/renderer/ascii/render_spec.rb +68 -0
  89. data/spec/tty/table/renderer/ascii/separator_spec.rb +28 -0
  90. data/spec/tty/table/renderer/basic/alignment_spec.rb +18 -16
  91. data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +17 -12
  92. data/spec/tty/table/renderer/basic/filter_spec.rb +53 -0
  93. data/spec/tty/table/renderer/basic/multiline_content_spec.rb +135 -0
  94. data/spec/tty/table/renderer/basic/new_spec.rb +13 -2
  95. data/spec/tty/table/renderer/basic/options_spec.rb +48 -0
  96. data/spec/tty/table/renderer/basic/render_spec.rb +19 -121
  97. data/spec/tty/table/renderer/basic/separator_spec.rb +14 -48
  98. data/spec/tty/table/renderer/basic/truncation_spec.rb +35 -0
  99. data/spec/tty/table/renderer/basic/wrapping_spec.rb +40 -0
  100. data/spec/tty/table/{border_spec.rb → renderer/border_spec.rb} +17 -20
  101. data/spec/tty/table/renderer/select_spec.rb +22 -0
  102. data/spec/tty/table/{border → renderer}/style_spec.rb +13 -14
  103. data/spec/tty/table/renderer/unicode/render_spec.rb +68 -0
  104. data/spec/tty/table/renderer/unicode/separator_spec.rb +26 -0
  105. data/spec/tty/table/rotate_spec.rb +2 -3
  106. data/spec/tty/table/row/call_spec.rb +1 -1
  107. data/spec/tty/table/row/each_spec.rb +31 -0
  108. data/spec/tty/table/row/height_spec.rb +27 -0
  109. data/spec/tty/table/to_s_spec.rb +3 -3
  110. data/spec/tty/table/transformation/extract_tuples_spec.rb +35 -0
  111. data/spec/tty/table/validatable/validate_options_spec.rb +1 -2
  112. data/spec/tty/terminal/home_spec.rb +3 -3
  113. data/spec/tty/terminal/page_spec.rb +13 -0
  114. data/spec/tty/terminal/pager/available_spec.rb +40 -0
  115. data/spec/tty/terminal/pager/basic/page_spec.rb +54 -0
  116. data/spec/tty/terminal/pager/command_spec.rb +16 -0
  117. data/spec/tty/terminal/pager/executables_spec.rb +13 -0
  118. data/spec/tty/terminal/pager/page_spec.rb +47 -0
  119. data/spec/tty/terminal/pager/system/page_spec.rb +29 -0
  120. data/spec/tty/text/distance/distance_spec.rb +12 -0
  121. data/tty.gemspec +7 -3
  122. metadata +160 -27
  123. data/spec/tty/table/operation/alignment_set/align_rows_spec.rb +0 -53
  124. data/spec/tty/table/renderer/pick_renderer_spec.rb +0 -25
  125. data/spec/tty/table/renderer_spec.rb +0 -49
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Table::Row, '#new' do
6
+ let(:yields) { [] }
7
+ let(:value) { 'a1' }
8
+ let(:header) { ['Header1']}
9
+ let(:row) { [ value ] }
10
+ let(:object) { described_class.new row, header }
11
+
12
+ context 'with block' do
13
+ subject { object.each { |field| yields << field } }
14
+
15
+ it 'yields only fields' do
16
+ subject
17
+ yields.each { |field| expect(field).to be_instance_of(value.class) }
18
+ end
19
+
20
+ it 'yields rows with expected attributes' do
21
+ subject
22
+ yields.each { |field| expect(field).to eql(value) }
23
+ end
24
+
25
+ it 'yields each row' do
26
+ expect { subject }.to change { yields }.
27
+ from( [] ).
28
+ to( yields )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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
@@ -7,7 +7,7 @@ describe TTY::Table, '#to_s' do
7
7
  let(:rows) { [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']] }
8
8
  let(:renderer) { :basic }
9
9
 
10
- subject { described_class.new header, rows, :renderer => renderer }
10
+ subject { described_class.new(header, rows) }
11
11
 
12
12
  context 'without renderer' do
13
13
  let(:renderer) { nil }
@@ -35,7 +35,7 @@ describe TTY::Table, '#to_s' do
35
35
  let(:renderer) { :ascii }
36
36
 
37
37
  it 'displays table' do
38
- subject.to_s.should == <<-EOS.normalize
38
+ subject.render(renderer).should == <<-EOS.normalize
39
39
  +--+--+--+
40
40
  |h1|h2|h3|
41
41
  +--+--+--+
@@ -50,7 +50,7 @@ describe TTY::Table, '#to_s' do
50
50
  let(:renderer) { :unicode}
51
51
 
52
52
  it 'displays table' do
53
- subject.to_s.should == <<-EOS.normalize
53
+ subject.render(renderer).should == <<-EOS.normalize
54
54
  ┌──┬──┬──┐
55
55
  │h1│h2│h3│
56
56
  ├──┼──┼──┤
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ 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
@@ -10,7 +10,7 @@ describe TTY::Table::Validatable, '#validate_options!' do
10
10
  context 'with empty rows' do
11
11
  let(:options) { {:rows => []} }
12
12
 
13
- it { expect { subject }.not_to raise_error(TTY::InvalidArgument) }
13
+ it { expect { subject }.not_to raise_error() }
14
14
  end
15
15
 
16
16
  context 'with invalid rows type' do
@@ -30,5 +30,4 @@ describe TTY::Table::Validatable, '#validate_options!' do
30
30
 
31
31
  it { expect { subject }.to raise_error(TTY::InvalidArgument) }
32
32
  end
33
-
34
33
  end
@@ -4,19 +4,19 @@ require 'spec_helper'
4
4
 
5
5
  describe TTY::Terminal, '#home' do
6
6
 
7
- before { ENV.stub!(:[]) }
7
+ before { ENV.stub(:[]) }
8
8
 
9
9
  subject(:terminal) { described_class.new.home }
10
10
 
11
11
  after { terminal.instance_variable_set(:@home, nil) }
12
12
 
13
13
  it 'expands user home path if HOME environemnt not set' do
14
- File.stub!(:expand_path).and_return('/home/user')
14
+ File.stub(:expand_path).and_return('/home/user')
15
15
  expect(terminal).to eql('/home/user')
16
16
  end
17
17
 
18
18
  it 'defaults to user HOME environment' do
19
- ENV.stub!(:[]).with('HOME').and_return('/home/user')
19
+ ENV.stub(:[]).with('HOME').and_return('/home/user')
20
20
  expect(terminal).to eq('/home/user')
21
21
  end
22
22
 
@@ -0,0 +1,13 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal, '#page' do
6
+ let(:text) { "a\n"}
7
+ let(:object) { described_class.new }
8
+
9
+ it 'invokes pager page method' do
10
+ object.pager.should_receive(:page).with(text)
11
+ object.page(text)
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Pager, '#available' do
6
+ let(:execs) { ['less', 'more'] }
7
+ let(:system) { TTY::System }
8
+ let(:command) { 'less' }
9
+
10
+ subject { described_class }
11
+
12
+ before { subject.stub(:executables).and_return(execs) }
13
+
14
+ context 'when command exists' do
15
+ before {
16
+ system.stub(:exists?).with('less').and_return(true)
17
+ system.stub(:exists?).with('more').and_return(false)
18
+ }
19
+
20
+ it 'finds single command' do
21
+ expect(subject.available).to eql('less')
22
+ end
23
+ end
24
+
25
+ context 'when no command exists' do
26
+ before { system.stub(:exists?).and_return(false) }
27
+
28
+ it "doesn't find command" do
29
+ expect(subject.available).to be_nil
30
+ end
31
+ end
32
+
33
+ context 'when custom command' do
34
+ before { system.stub(:exists?).with(command).and_return(true) }
35
+
36
+ it "takes precedence over other commands" do
37
+ expect(subject.available(command)).to eql(command)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::BasicPager, '#simple' do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+ let(:shell) { TTY::Shell.new(input, output) }
9
+ let(:terminal) { TTY.terminal }
10
+ let(:object) { described_class }
11
+
12
+ subject { object.new(text) }
13
+
14
+ before {
15
+ TTY.stub(:shell).and_return(shell)
16
+ terminal.stub(:height).and_return(10)
17
+ }
18
+
19
+ context 'when no text' do
20
+ let(:text) { "" }
21
+
22
+ it "doesn't page text" do
23
+ subject.page
24
+ expect(output.string).to eql(text)
25
+ end
26
+ end
27
+
28
+ context 'when text fits on screen' do
29
+ let(:text) { "a\n" }
30
+
31
+ it "doesn't page text not long enough" do
32
+ subject.page
33
+ expect(output.string).to eql(text)
34
+ end
35
+ end
36
+
37
+ context "when text doesn't fit on screen" do
38
+ let(:text) { "a\na\na\na\na\na\na\na\na\na\n" }
39
+
40
+ it "continues paging when enter is pressed" do
41
+ input << '\n'
42
+ input.rewind
43
+ subject.page
44
+ expect(output.string).to eql("a\na\na\na\na\na\na\n\n#{object::PAGE_BREAK}\na\na\na\n")
45
+ end
46
+
47
+ it "stops paging when q is pressed" do
48
+ input << 'q\n'
49
+ input.rewind
50
+ subject.page
51
+ expect(output.string).to eql("a\na\na\na\na\na\na\n\n#{object::PAGE_BREAK}\n")
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Pager, '#command' do
6
+ let(:pager) { 'vim' }
7
+
8
+ subject { described_class }
9
+
10
+ context 'when custom command' do
11
+ it 'searches available commands' do
12
+ subject.should_receive(:available).with(pager)
13
+ subject.command(pager)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Pager, '#executables' do
6
+ let(:object) { described_class }
7
+
8
+ subject { object.executables }
9
+
10
+ it { should be_an Array }
11
+
12
+ it { should include('less -isr') }
13
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::Pager, '#page' do
6
+ let(:text) { "a\n" }
7
+ let(:shell) { TTY.shell }
8
+ let(:system) { TTY::System }
9
+ let(:pager) { double(:pager, :page => nil) }
10
+ let(:basic_pager) { TTY::Terminal::BasicPager }
11
+ let(:system_pager) { TTY::Terminal::SystemPager }
12
+
13
+ subject { described_class }
14
+
15
+ before { shell.stub(:tty?).and_return(true) }
16
+
17
+ context 'when not tty' do
18
+ before { shell.stub(:tty?).and_return(false) }
19
+
20
+ it "doesn't page" do
21
+ expect(subject.page(text)).to eql(nil)
22
+ end
23
+ end
24
+
25
+ context 'when not unix' do
26
+ before {
27
+ system.stub(:unix?).and_return(false)
28
+ }
29
+
30
+ it 'pages with simple pager' do
31
+ basic_pager.should_receive(:new).with(text).and_return(pager)
32
+ subject.page(text)
33
+ end
34
+ end
35
+
36
+ context 'when unix and available' do
37
+ before {
38
+ system.stub(:unix?).and_return(true)
39
+ subject.stub(:available?).and_return(true)
40
+ }
41
+
42
+ it 'pages with system pager' do
43
+ system_pager.should_receive(:new).with(text).and_return(pager)
44
+ subject.page(text)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TTY::Terminal::SystemPager, '#simple' do
6
+ let(:input) { StringIO.new }
7
+ let(:output) { StringIO.new }
8
+ let(:shell) { TTY::Shell.new(input, output) }
9
+ let(:terminal) { TTY.terminal }
10
+ let(:object) { described_class }
11
+
12
+ subject { object.new(text) }
13
+
14
+ before {
15
+ TTY.stub(:shell).and_return(shell)
16
+ IO.stub(:pipe).and_return([input, output])
17
+ Kernel.stub(:fork) { true }
18
+ }
19
+
20
+ context 'when text fits on screen' do
21
+ let(:text) { "a\na\na\na\na\na\na\na\na\na\n" }
22
+
23
+ it "doesn't page text not long enough" do
24
+ Kernel.should_receive(:exec)
25
+ Kernel.should_receive(:select)
26
+ subject.page
27
+ end
28
+ end
29
+ end
@@ -25,6 +25,12 @@ describe TTY::Text::Distance, '#distance' do
25
25
  it { should eql(3) }
26
26
  end
27
27
 
28
+ context 'when single char' do
29
+ let(:strings) { ['a', 'abc'] }
30
+
31
+ it { should eql(2) }
32
+ end
33
+
28
34
  context 'when similar' do
29
35
  let(:strings) { ['abc', 'abc'] }
30
36
 
@@ -49,6 +55,12 @@ describe TTY::Text::Distance, '#distance' do
49
55
  it { should eql(2) }
50
56
  end
51
57
 
58
+ context 'when prefix' do
59
+ let(:strings) { ['sta', 'status'] }
60
+
61
+ it { should eql(3) }
62
+ end
63
+
52
64
  context 'when similar' do
53
65
  let(:strings) { ['smellyfish','jellyfish'] }
54
66
 
data/tty.gemspec CHANGED
@@ -17,8 +17,12 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_development_dependency 'rspec'
21
- gem.add_development_dependency 'rake'
22
- gem.add_development_dependency 'yard'
20
+ gem.add_development_dependency 'rspec', '~> 2.14'
21
+ gem.add_development_dependency 'rake', '~> 10.1'
22
+ gem.add_development_dependency 'yard', '~> 0.8'
23
23
  gem.add_development_dependency 'benchmark_suite'
24
+ gem.add_development_dependency 'bundler'
25
+ gem.add_development_dependency 'yard', '~> 0.8'
26
+ gem.add_development_dependency 'simplecov', '~> 0.7.1'
27
+ gem.add_development_dependency 'coveralls', '~> 0.6.7'
24
28
  end