tty 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +45 -115
  4. data/lib/tty.rb +3 -31
  5. data/lib/tty/plugins/plugin.rb +2 -2
  6. data/lib/tty/terminal.rb +2 -58
  7. data/lib/tty/terminal/home.rb +27 -9
  8. data/lib/tty/version.rb +1 -1
  9. data/spec/tty/plugins/plugin/load_spec.rb +10 -18
  10. data/spec/tty/system/editor/open_spec.rb +1 -1
  11. data/spec/tty/terminal/home_spec.rb +18 -26
  12. data/spec/tty/tty_spec.rb +1 -1
  13. data/spec/tty/vector/new_spec.rb +1 -1
  14. metadata +2 -83
  15. data/lib/tty/shell.rb +0 -211
  16. data/lib/tty/shell/distance.rb +0 -49
  17. data/lib/tty/shell/question.rb +0 -335
  18. data/lib/tty/shell/question/modifier.rb +0 -93
  19. data/lib/tty/shell/question/validation.rb +0 -92
  20. data/lib/tty/shell/reader.rb +0 -110
  21. data/lib/tty/shell/response.rb +0 -249
  22. data/lib/tty/shell/response_delegation.rb +0 -55
  23. data/lib/tty/shell/statement.rb +0 -60
  24. data/lib/tty/shell/suggestion.rb +0 -126
  25. data/lib/tty/support/utils.rb +0 -16
  26. data/lib/tty/terminal/echo.rb +0 -38
  27. data/lib/tty/terminal/raw.rb +0 -38
  28. data/spec/tty/shell/ask_spec.rb +0 -77
  29. data/spec/tty/shell/distance/distance_spec.rb +0 -75
  30. data/spec/tty/shell/distance/initialize_spec.rb +0 -14
  31. data/spec/tty/shell/error_spec.rb +0 -30
  32. data/spec/tty/shell/print_table_spec.rb +0 -24
  33. data/spec/tty/shell/question/argument_spec.rb +0 -30
  34. data/spec/tty/shell/question/character_spec.rb +0 -24
  35. data/spec/tty/shell/question/default_spec.rb +0 -25
  36. data/spec/tty/shell/question/in_spec.rb +0 -23
  37. data/spec/tty/shell/question/initialize_spec.rb +0 -24
  38. data/spec/tty/shell/question/modifier/apply_to_spec.rb +0 -34
  39. data/spec/tty/shell/question/modifier/letter_case_spec.rb +0 -27
  40. data/spec/tty/shell/question/modifier/whitespace_spec.rb +0 -33
  41. data/spec/tty/shell/question/modify_spec.rb +0 -44
  42. data/spec/tty/shell/question/valid_spec.rb +0 -46
  43. data/spec/tty/shell/question/validate_spec.rb +0 -30
  44. data/spec/tty/shell/question/validation/coerce_spec.rb +0 -24
  45. data/spec/tty/shell/question/validation/valid_value_spec.rb +0 -28
  46. data/spec/tty/shell/reader/getc_spec.rb +0 -42
  47. data/spec/tty/shell/response/read_bool_spec.rb +0 -40
  48. data/spec/tty/shell/response/read_char_spec.rb +0 -16
  49. data/spec/tty/shell/response/read_date_spec.rb +0 -20
  50. data/spec/tty/shell/response/read_email_spec.rb +0 -42
  51. data/spec/tty/shell/response/read_multiple_spec.rb +0 -23
  52. data/spec/tty/shell/response/read_number_spec.rb +0 -28
  53. data/spec/tty/shell/response/read_range_spec.rb +0 -31
  54. data/spec/tty/shell/response/read_spec.rb +0 -68
  55. data/spec/tty/shell/response/read_string_spec.rb +0 -19
  56. data/spec/tty/shell/say_spec.rb +0 -67
  57. data/spec/tty/shell/statement/initialize_spec.rb +0 -15
  58. data/spec/tty/shell/suggest_spec.rb +0 -50
  59. data/spec/tty/shell/warn_spec.rb +0 -30
  60. data/spec/tty/terminal/color_spec.rb +0 -16
  61. data/spec/tty/terminal/echo_spec.rb +0 -21
@@ -1,14 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Shell::Distance, '#initialize' do
6
- let(:strings) { ['abc', 'acb'] }
7
-
8
- # subject(:distance) { described_class.new(*strings) }
9
- #
10
- # it { expect(distance.first).to eq(strings.first) }
11
- #
12
- # it { expect(distance.second).to eq(strings.last) }
13
-
14
- end # initialize
@@ -1,30 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe TTY::Shell, '.error' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:color) { Pastel.new(enabled: true) }
9
-
10
- subject(:shell) { TTY::Shell.new(input, output) }
11
-
12
- before { allow(Pastel).to receive(:new).and_return(color) }
13
-
14
- after { output.rewind }
15
-
16
- it 'displays one message' do
17
- shell.error "Nothing is fine!"
18
- expect(output.string).to eql "\e[31mNothing is fine!\e[0m\n"
19
- end
20
-
21
- it 'displays many messages' do
22
- shell.error "Nothing is fine!", "All is broken!"
23
- expect(output.string).to eql "\e[31mNothing is fine!\e[0m\n\e[31mAll is broken!\e[0m\n"
24
- end
25
-
26
- it 'displays message with option' do
27
- shell.error "Nothing is fine!", :newline => false
28
- expect(output.string).to eql "\e[31mNothing is fine!\e[0m"
29
- end
30
- end # error
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell, '#print_table' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:header) { ['h1', 'h2'] }
9
- let(:rows) { [['a1', 'a2'], ['b1', 'b2']] }
10
-
11
- subject(:shell) { TTY::Shell.new(input, output) }
12
-
13
- it 'prints a table' do
14
- shell.print_table header, rows, :renderer => :ascii
15
- expect(output.string).to eql <<-EOS.normalize
16
- +--+--+
17
- |h1|h2|
18
- +--+--+
19
- |a1|a2|
20
- |b1|b2|
21
- +--+--+\n
22
- EOS
23
- end
24
- end # print_table
@@ -1,30 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#argument' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'requires value to be present with helper' do
11
- input << ''
12
- input.rewind
13
- q = shell.ask("What is your name?").argument(:required)
14
- expect { q.read }.to raise_error(ArgumentError)
15
- end
16
-
17
- it 'requires value to be present with option' do
18
- input << ''
19
- input.rewind
20
- q = shell.ask("What is your name?", :required => true)
21
- expect { q.read }.to raise_error(ArgumentError)
22
- end
23
-
24
- it "doesn't require value to be present" do
25
- input << ''
26
- input.rewind
27
- q = shell.ask("What is your name?").argument(:optional)
28
- expect(q.read).to be_nil
29
- end
30
- end
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#character' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'switches to character input' do
11
- input << "abcd"
12
- input.rewind
13
- q = shell.ask("Which one do you prefer a, b, c or d?").char(true)
14
- expect(q.character)
15
- expect(q.read_string).to eq("abcd")
16
- end
17
-
18
- it 'acts as reader without arguments' do
19
- input << "abcd"
20
- input.rewind
21
- q = shell.ask("Which one do you prefer a, b, c or d?")
22
- expect(q.char).to eq(false)
23
- end
24
- end # character
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#default' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'uses default value' do
11
- name = 'Anonymous'
12
- q = shell.ask("What is your name?").default(name)
13
- answer = q.read
14
- expect(answer).to eq(name)
15
- end
16
-
17
- it 'uses default value in block' do
18
- name = 'Anonymous'
19
- q = shell.ask "What is your name?" do
20
- default name
21
- end
22
- answer = q.read
23
- expect(answer).to eq(name)
24
- end
25
- end # default
@@ -1,23 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#in' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'reads number within range' do
11
- input << 8
12
- input.rewind
13
- q = shell.ask("How do you like it on scale 1-10").in('1-10')
14
- expect(q.read_int).to eql 8
15
- end
16
-
17
- it "reads number outside of range" do
18
- input << 12
19
- input.rewind
20
- q = shell.ask("How do you like it on scale 1-10").in('1-10')
21
- expect { q.read_int }.to raise_error(ArgumentError)
22
- end
23
- end # in
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#initialize' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
- let(:message) { 'Do you like me?' }
10
-
11
- subject(:question) { described_class.new shell }
12
-
13
- it { expect(question.required?).to eq(false) }
14
-
15
- it { expect(question.echo).to eq(true) }
16
-
17
- it { expect(question.mask).to eq(false) }
18
-
19
- it { expect(question.character).to eq(false) }
20
-
21
- it { expect(question.modifier).to be_kind_of(TTY::Shell::Question::Modifier) }
22
-
23
- it { expect(question.validation).to be_kind_of(TTY::Shell::Question::Validation) }
24
- end # initialize
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question::Modifier, '#apply_to' do
6
- let(:instance) { described_class.new modifiers }
7
- let(:string) { "Text to be modified"}
8
-
9
- subject { instance.apply_to string }
10
-
11
- context 'when no modifiers specified' do
12
- let(:modifiers) { [] }
13
-
14
- it { should == string }
15
- end
16
-
17
- context 'when modifiers specified' do
18
- let(:modifiers) { [:down, :capitalize] }
19
-
20
- it 'applies letter case modifications' do
21
- allow(described_class).to receive(:letter_case)
22
- subject
23
- expect(described_class).to have_received(:letter_case).
24
- with(modifiers, string)
25
- end
26
-
27
- it 'applies whitespace modifications' do
28
- allow(described_class).to receive(:whitespace)
29
- subject
30
- expect(described_class).to have_received(:whitespace).
31
- with(modifiers, string)
32
- end
33
- end
34
- end # apply_to
@@ -1,27 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question::Modifier, '#letter_case' do
6
- let(:string) { 'text to modify' }
7
-
8
- subject { described_class.letter_case modifier, string}
9
-
10
- context 'when upper case' do
11
- let(:modifier) { :up }
12
-
13
- it { is_expected.to eq('TEXT TO MODIFY') }
14
- end
15
-
16
- context 'when lower case' do
17
- let(:modifier) { :down }
18
-
19
- it { is_expected.to eq('text to modify') }
20
- end
21
-
22
- context 'when capitalize' do
23
- let(:modifier) { :capitalize }
24
-
25
- it { is_expected.to eq('Text to modify') }
26
- end
27
- end # lettercase
@@ -1,33 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question::Modifier, '#whitespace' do
6
- let(:string) { " text\t \n to\t modify\r\n" }
7
-
8
- subject { described_class.whitespace modifier, string}
9
-
10
- context 'when stripping whitespace' do
11
- let(:modifier) { :trim }
12
-
13
- it { is_expected.to eq("text\t \n to\t modify") }
14
- end
15
-
16
- context 'when chomping whitespace' do
17
- let(:modifier) { :chomp }
18
-
19
- it { is_expected.to eq(" text\t \n to\t modify") }
20
- end
21
-
22
- context 'when capitalize' do
23
- let(:modifier) { :collapse }
24
-
25
- it { is_expected.to eq(" text to modify ") }
26
- end
27
-
28
- context 'when removing whitespace' do
29
- let(:modifier) { :remove }
30
-
31
- it { is_expected.to eq("texttomodify") }
32
- end
33
- end # whitespace
@@ -1,44 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#modify' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'preserves answer for unkown modification' do
11
- input << 'piotr'
12
- input.rewind
13
- q = shell.ask("What is your name?").modify(:none)
14
- expect(q.read_string).to eql 'piotr'
15
- end
16
-
17
- it 'converts to upper case' do
18
- input << 'piotr'
19
- input.rewind
20
- q = shell.ask("What is your name?").modify(:upcase)
21
- expect(q.read_string).to eql 'PIOTR'
22
- end
23
-
24
- it 'trims whitespace' do
25
- input << " Some white\t space\t \there! \n"
26
- input.rewind
27
- q = shell.ask("Enter some text: ").modify(:trim)
28
- expect(q.read_string).to eql "Some white\t space\t \there!"
29
- end
30
-
31
- it 'collapses whitespace' do
32
- input << " Some white\t space\t \there! \n"
33
- input.rewind
34
- q = shell.ask("Enter some text: ").modify(:collapse)
35
- expect(q.read_string).to eql "Some white space here!"
36
- end
37
-
38
- it 'strips and collapses whitespace' do
39
- input << " Some white\t space\t \there! \n"
40
- input.rewind
41
- q = shell.ask("Enter some text: ").modify(:strip, :collapse)
42
- expect(q.read_string).to eql "Some white space here!"
43
- end
44
- end
@@ -1,46 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#valid' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- let(:cards) { %w[ club diamond spade heart ] }
11
-
12
- it 'reads valid optios with helper' do
13
- input << 'club'
14
- input.rewind
15
- q = shell.ask("What is your card suit sir?").valid(cards)
16
- expect(q.read_choice).to eq('club')
17
- end
18
-
19
- it 'reads valid options with option hash' do
20
- input << 'club'
21
- input.rewind
22
- q = shell.ask("What is your card suit sir?", :valid => cards)
23
- expect(q.read_choice).to eq('club')
24
- end
25
-
26
- it 'reads invalid option' do
27
- input << 'clover'
28
- input.rewind
29
- q = shell.ask("What is your card suit sir?").valid(cards)
30
- expect { q.read_choice }.to raise_error(TTY::InvalidArgument)
31
- end
32
-
33
- it 'needs argument' do
34
- input << ''
35
- input.rewind
36
- q = shell.ask("What is your card suit sir?").valid(cards)
37
- expect { q.read_choice }.to raise_error(TTY::ArgumentRequired)
38
- end
39
-
40
- it 'reads with default' do
41
- input << ''
42
- input.rewind
43
- q = shell.ask("What is your card suit sir?").valid(cards).default('club')
44
- expect(q.read_choice).to eq('club')
45
- end
46
- end
@@ -1,30 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question, '#validate' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:shell) { TTY::Shell.new(input, output) }
9
-
10
- it 'fails to validate input' do
11
- input << 'piotrmurach'
12
- input.rewind
13
- q = shell.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
14
- expect { q.read_string }.to raise_error(ArgumentError)
15
- end
16
-
17
- it 'validates input with regex' do
18
- input << 'piotr.murach'
19
- input.rewind
20
- q = shell.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
21
- expect(q.read_string).to eq('piotr.murach')
22
- end
23
-
24
- it 'validates input in block' do
25
- input << 'piotr.murach'
26
- input.rewind
27
- q = shell.ask("What is your username?").validate { |arg| arg =~ /^[^\.]+\.[^\.]+/ }
28
- expect(q.read_string).to eq('piotr.murach')
29
- end
30
- end
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe TTY::Shell::Question::Validation, '#coerce' do
6
- let(:validation) { "^[^\.]+\.[^\.]+" }
7
- let(:instance) { described_class.new }
8
-
9
- subject { instance.coerce validation }
10
-
11
- it { should be_kind_of Regexp }
12
-
13
- context 'when proc' do
14
- let(:validation) { lambda { "^[^\.]+\.[^\.]+" } }
15
-
16
- it { is_expected.to be_kind_of Proc }
17
- end
18
-
19
- context 'when unkown type' do
20
- let(:validation) { Object.new }
21
-
22
- it { expect { subject }.to raise_error(TTY::ValidationCoercion) }
23
- end
24
- end