tty 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +45 -115
- data/lib/tty.rb +3 -31
- data/lib/tty/plugins/plugin.rb +2 -2
- data/lib/tty/terminal.rb +2 -58
- data/lib/tty/terminal/home.rb +27 -9
- data/lib/tty/version.rb +1 -1
- data/spec/tty/plugins/plugin/load_spec.rb +10 -18
- data/spec/tty/system/editor/open_spec.rb +1 -1
- data/spec/tty/terminal/home_spec.rb +18 -26
- data/spec/tty/tty_spec.rb +1 -1
- data/spec/tty/vector/new_spec.rb +1 -1
- metadata +2 -83
- data/lib/tty/shell.rb +0 -211
- data/lib/tty/shell/distance.rb +0 -49
- data/lib/tty/shell/question.rb +0 -335
- data/lib/tty/shell/question/modifier.rb +0 -93
- data/lib/tty/shell/question/validation.rb +0 -92
- data/lib/tty/shell/reader.rb +0 -110
- data/lib/tty/shell/response.rb +0 -249
- data/lib/tty/shell/response_delegation.rb +0 -55
- data/lib/tty/shell/statement.rb +0 -60
- data/lib/tty/shell/suggestion.rb +0 -126
- data/lib/tty/support/utils.rb +0 -16
- data/lib/tty/terminal/echo.rb +0 -38
- data/lib/tty/terminal/raw.rb +0 -38
- data/spec/tty/shell/ask_spec.rb +0 -77
- data/spec/tty/shell/distance/distance_spec.rb +0 -75
- data/spec/tty/shell/distance/initialize_spec.rb +0 -14
- data/spec/tty/shell/error_spec.rb +0 -30
- data/spec/tty/shell/print_table_spec.rb +0 -24
- data/spec/tty/shell/question/argument_spec.rb +0 -30
- data/spec/tty/shell/question/character_spec.rb +0 -24
- data/spec/tty/shell/question/default_spec.rb +0 -25
- data/spec/tty/shell/question/in_spec.rb +0 -23
- data/spec/tty/shell/question/initialize_spec.rb +0 -24
- data/spec/tty/shell/question/modifier/apply_to_spec.rb +0 -34
- data/spec/tty/shell/question/modifier/letter_case_spec.rb +0 -27
- data/spec/tty/shell/question/modifier/whitespace_spec.rb +0 -33
- data/spec/tty/shell/question/modify_spec.rb +0 -44
- data/spec/tty/shell/question/valid_spec.rb +0 -46
- data/spec/tty/shell/question/validate_spec.rb +0 -30
- data/spec/tty/shell/question/validation/coerce_spec.rb +0 -24
- data/spec/tty/shell/question/validation/valid_value_spec.rb +0 -28
- data/spec/tty/shell/reader/getc_spec.rb +0 -42
- data/spec/tty/shell/response/read_bool_spec.rb +0 -40
- data/spec/tty/shell/response/read_char_spec.rb +0 -16
- data/spec/tty/shell/response/read_date_spec.rb +0 -20
- data/spec/tty/shell/response/read_email_spec.rb +0 -42
- data/spec/tty/shell/response/read_multiple_spec.rb +0 -23
- data/spec/tty/shell/response/read_number_spec.rb +0 -28
- data/spec/tty/shell/response/read_range_spec.rb +0 -31
- data/spec/tty/shell/response/read_spec.rb +0 -68
- data/spec/tty/shell/response/read_string_spec.rb +0 -19
- data/spec/tty/shell/say_spec.rb +0 -67
- data/spec/tty/shell/statement/initialize_spec.rb +0 -15
- data/spec/tty/shell/suggest_spec.rb +0 -50
- data/spec/tty/shell/warn_spec.rb +0 -30
- data/spec/tty/terminal/color_spec.rb +0 -16
- data/spec/tty/terminal/echo_spec.rb +0 -21
@@ -1,55 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Shell
|
5
|
-
module ResponseDelegation
|
6
|
-
extend TTY::Delegatable
|
7
|
-
|
8
|
-
delegatable_method :dispatch, :read
|
9
|
-
|
10
|
-
delegatable_method :dispatch, :read_string
|
11
|
-
|
12
|
-
delegatable_method :dispatch, :read_char
|
13
|
-
|
14
|
-
delegatable_method :dispatch, :read_text
|
15
|
-
|
16
|
-
delegatable_method :dispatch, :read_symbol
|
17
|
-
|
18
|
-
delegatable_method :dispatch, :read_choice
|
19
|
-
|
20
|
-
delegatable_method :dispatch, :read_int
|
21
|
-
|
22
|
-
delegatable_method :dispatch, :read_float
|
23
|
-
|
24
|
-
delegatable_method :dispatch, :read_regex
|
25
|
-
|
26
|
-
delegatable_method :dispatch, :read_range
|
27
|
-
|
28
|
-
delegatable_method :dispatch, :read_date
|
29
|
-
|
30
|
-
delegatable_method :dispatch, :read_datetime
|
31
|
-
|
32
|
-
delegatable_method :dispatch, :read_bool
|
33
|
-
|
34
|
-
delegatable_method :dispatch, :read_file
|
35
|
-
|
36
|
-
delegatable_method :dispatch, :read_email
|
37
|
-
|
38
|
-
delegatable_method :dispatch, :read_multiple
|
39
|
-
|
40
|
-
delegatable_method :dispatch, :read_password
|
41
|
-
|
42
|
-
delegatable_method :dispatch, :read_keypress
|
43
|
-
|
44
|
-
# Create response instance when question readed is invoked
|
45
|
-
#
|
46
|
-
# @param [TTY::Shell::Response] response
|
47
|
-
#
|
48
|
-
# @api private
|
49
|
-
def dispatch(response = Response.new(self, shell))
|
50
|
-
@response ||= response
|
51
|
-
end
|
52
|
-
|
53
|
-
end # ResponseDelegation
|
54
|
-
end # Shell
|
55
|
-
end # TTY
|
data/lib/tty/shell/statement.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
# A class responsible for shell prompt interactions.
|
5
|
-
class Shell
|
6
|
-
# A class representing a statement output to shell.
|
7
|
-
class Statement
|
8
|
-
# @api private
|
9
|
-
attr_reader :shell
|
10
|
-
private :shell
|
11
|
-
|
12
|
-
# Flag to display newline
|
13
|
-
#
|
14
|
-
# @api public
|
15
|
-
attr_reader :newline
|
16
|
-
|
17
|
-
# Color used to display statement
|
18
|
-
#
|
19
|
-
# @api public
|
20
|
-
attr_reader :color
|
21
|
-
|
22
|
-
# Initialize a Statement
|
23
|
-
#
|
24
|
-
# @param [TTY::Shell] shell
|
25
|
-
#
|
26
|
-
# @param [Hash] options
|
27
|
-
#
|
28
|
-
# @option options [Symbol] :newline
|
29
|
-
# force a newline break after the message
|
30
|
-
#
|
31
|
-
# @option options [Symbol] :color
|
32
|
-
# change the message display to color
|
33
|
-
#
|
34
|
-
# @api public
|
35
|
-
def initialize(shell = Shell.new, options = {})
|
36
|
-
@shell = shell
|
37
|
-
@pastel = Pastel.new
|
38
|
-
@newline = options.fetch(:newline, true)
|
39
|
-
@color = options.fetch(:color, false)
|
40
|
-
end
|
41
|
-
|
42
|
-
# Output the message to the shell
|
43
|
-
#
|
44
|
-
# @param [String] message
|
45
|
-
# the message to be printed to stdout
|
46
|
-
#
|
47
|
-
# @api public
|
48
|
-
def declare(message)
|
49
|
-
message = @pastel.decorate message, *color if color
|
50
|
-
|
51
|
-
if newline && /( |\t)(\e\[\d+(;\d+)*m)?\Z/ !~ message
|
52
|
-
shell.output.puts message
|
53
|
-
else
|
54
|
-
shell.output.print message
|
55
|
-
shell.output.flush
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end # Statement
|
59
|
-
end # Shell
|
60
|
-
end # TTY
|
data/lib/tty/shell/suggestion.rb
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
# A class responsible for shell prompt interactions.
|
5
|
-
class Shell
|
6
|
-
# A class representing a suggestion
|
7
|
-
class Suggestion
|
8
|
-
DEFAULT_INDENT = 8
|
9
|
-
|
10
|
-
SINGLE_TEXT = 'Did you mean this?'
|
11
|
-
|
12
|
-
PLURAL_TEXT = 'Did you mean one of these?'
|
13
|
-
|
14
|
-
# Number of spaces
|
15
|
-
#
|
16
|
-
# @api public
|
17
|
-
attr_reader :indent
|
18
|
-
|
19
|
-
# Text for a single suggestion
|
20
|
-
#
|
21
|
-
# @api public
|
22
|
-
attr_reader :single_text
|
23
|
-
|
24
|
-
# Text for multiple suggestions
|
25
|
-
#
|
26
|
-
# @api public
|
27
|
-
attr_reader :plural_text
|
28
|
-
|
29
|
-
# Initialize a Suggestion
|
30
|
-
#
|
31
|
-
# @api public
|
32
|
-
def initialize(options = {})
|
33
|
-
@indent = options.fetch(:indent) { DEFAULT_INDENT }
|
34
|
-
@single_text = options.fetch(:single_text) { SINGLE_TEXT }
|
35
|
-
@plural_text = options.fetch(:plural_text) { PLURAL_TEXT }
|
36
|
-
@suggestions = []
|
37
|
-
@comparator = Distance.new
|
38
|
-
end
|
39
|
-
|
40
|
-
# Suggest matches out of possibile strings
|
41
|
-
#
|
42
|
-
# @param [String] message
|
43
|
-
#
|
44
|
-
# @param [Array[String]] possibilities
|
45
|
-
#
|
46
|
-
# @api public
|
47
|
-
def suggest(message, possibilities)
|
48
|
-
distances = measure_distances(message, possibilities)
|
49
|
-
minimum_distance = distances.keys.min
|
50
|
-
max_distance = distances.keys.max
|
51
|
-
|
52
|
-
if minimum_distance < max_distance
|
53
|
-
@suggestions = distances[minimum_distance].sort
|
54
|
-
end
|
55
|
-
evaluate
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
# Measure distances between messag and possibilities
|
61
|
-
#
|
62
|
-
# @param [String] message
|
63
|
-
#
|
64
|
-
# @param [Array[String]] possibilities
|
65
|
-
#
|
66
|
-
# @return [Hash]
|
67
|
-
#
|
68
|
-
# @api private
|
69
|
-
def measure_distances(message, possibilities)
|
70
|
-
distances = Hash.new { |hash, key| hash[key] = [] }
|
71
|
-
|
72
|
-
possibilities.each do |possibility|
|
73
|
-
distances[comparator.distance(message, possibility)] << possibility
|
74
|
-
end
|
75
|
-
distances
|
76
|
-
end
|
77
|
-
|
78
|
-
# Build up a suggestion string
|
79
|
-
#
|
80
|
-
# @param [Array[String]] suggestions
|
81
|
-
#
|
82
|
-
# @return [String]
|
83
|
-
#
|
84
|
-
# @api private
|
85
|
-
def evaluate
|
86
|
-
return suggestions if suggestions.empty?
|
87
|
-
if suggestions.one?
|
88
|
-
build_single_suggestion
|
89
|
-
else
|
90
|
-
build_multiple_suggestions
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
# @api private
|
95
|
-
def build_single_suggestion
|
96
|
-
suggestion = ''
|
97
|
-
suggestion << single_text + "\n"
|
98
|
-
suggestion << (' ' * indent + suggestions.first)
|
99
|
-
end
|
100
|
-
|
101
|
-
# @api private
|
102
|
-
def build_multiple_suggestions
|
103
|
-
suggestion = ''
|
104
|
-
suggestion << plural_text + "\n"
|
105
|
-
suggestion << suggestions.map do |sugest|
|
106
|
-
' ' * indent + sugest
|
107
|
-
end.join("\n")
|
108
|
-
end
|
109
|
-
|
110
|
-
# Distance comparator
|
111
|
-
#
|
112
|
-
# @api private
|
113
|
-
attr_reader :comparator
|
114
|
-
|
115
|
-
# Reference to the current shell
|
116
|
-
#
|
117
|
-
# @api private
|
118
|
-
attr_reader :shell
|
119
|
-
|
120
|
-
# Possible suggestions
|
121
|
-
#
|
122
|
-
# @api private
|
123
|
-
attr_reader :suggestions
|
124
|
-
end # Suggestion
|
125
|
-
end # Shell
|
126
|
-
end # TTY
|
data/lib/tty/support/utils.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
module Utils
|
5
|
-
extend self
|
6
|
-
|
7
|
-
def extract_options!(args)
|
8
|
-
args.last.respond_to?(:to_hash) ? args.pop : {}
|
9
|
-
end
|
10
|
-
|
11
|
-
def extract_options(args)
|
12
|
-
options = args.last
|
13
|
-
options.respond_to?(:to_hash) ? options.to_hash.dup : {}
|
14
|
-
end
|
15
|
-
end # Utils
|
16
|
-
end # TTY
|
data/lib/tty/terminal/echo.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Terminal
|
5
|
-
# A class responsible for toggling echo.
|
6
|
-
class Echo
|
7
|
-
# Turn echo on
|
8
|
-
#
|
9
|
-
# @api public
|
10
|
-
def on
|
11
|
-
%x{stty echo} if TTY::Platform.unix?
|
12
|
-
end
|
13
|
-
|
14
|
-
# Turn echo off
|
15
|
-
#
|
16
|
-
# @api public
|
17
|
-
def off
|
18
|
-
%x{stty -echo} if TTY::Platform.unix?
|
19
|
-
end
|
20
|
-
|
21
|
-
# Wrap code block inside echo
|
22
|
-
#
|
23
|
-
# @api public
|
24
|
-
def echo(is_on=true, &block)
|
25
|
-
value = nil
|
26
|
-
begin
|
27
|
-
off unless is_on
|
28
|
-
value = block.call if block_given?
|
29
|
-
on
|
30
|
-
return value
|
31
|
-
rescue NoMethodError, Interrupt
|
32
|
-
on
|
33
|
-
exit
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end # Echo
|
37
|
-
end # Terminal
|
38
|
-
end # TTY
|
data/lib/tty/terminal/raw.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Terminal
|
5
|
-
# A class responsible for toggling raw mode.
|
6
|
-
class Raw
|
7
|
-
# Turn raw mode on
|
8
|
-
#
|
9
|
-
# @api public
|
10
|
-
def on
|
11
|
-
%x{stty raw} if TTY::Platform.unix?
|
12
|
-
end
|
13
|
-
|
14
|
-
# Turn raw mode off
|
15
|
-
#
|
16
|
-
# @api public
|
17
|
-
def off
|
18
|
-
%x{stty -raw} if TTY::Platform.unix?
|
19
|
-
end
|
20
|
-
|
21
|
-
# Wrap code block inside raw mode
|
22
|
-
#
|
23
|
-
# @api public
|
24
|
-
def raw(is_on=true, &block)
|
25
|
-
value = nil
|
26
|
-
begin
|
27
|
-
on if is_on
|
28
|
-
value = block.call if block_given?
|
29
|
-
off
|
30
|
-
return value
|
31
|
-
rescue NoMethodError, Interrupt
|
32
|
-
off
|
33
|
-
exit
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end # Echo
|
37
|
-
end # Terminal
|
38
|
-
end # TTY
|
data/spec/tty/shell/ask_spec.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe TTY::Shell, '#ask' do
|
6
|
-
let(:input) { StringIO.new }
|
7
|
-
let(:output) { StringIO.new }
|
8
|
-
let(:prefix) { '' }
|
9
|
-
let(:options) { { :prefix => prefix } }
|
10
|
-
|
11
|
-
subject(:shell) { TTY::Shell.new(input, output, options) }
|
12
|
-
|
13
|
-
it 'prints message' do
|
14
|
-
shell.ask "What is your name?"
|
15
|
-
expect(output.string).to eql "What is your name?\n"
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'prints an empty message ' do
|
19
|
-
shell.ask ""
|
20
|
-
expect(output.string).to eql ""
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'prints an empty message and returns nil if EOF is sent to stdin' do
|
24
|
-
input << nil
|
25
|
-
input.rewind
|
26
|
-
q = shell.ask ""
|
27
|
-
expect(q.read).to eql nil
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'with a prompt prefix' do
|
31
|
-
let(:prefix) { ' > ' }
|
32
|
-
|
33
|
-
it "asks a question with '>'" do
|
34
|
-
input << ''
|
35
|
-
input.rewind
|
36
|
-
shell.ask "Are you Polish?"
|
37
|
-
expect(output.string).to eql " > Are you Polish?\n"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'asks a question with block' do
|
42
|
-
input << ''
|
43
|
-
input.rewind
|
44
|
-
q = shell.ask "What is your name?" do
|
45
|
-
default 'Piotr'
|
46
|
-
end
|
47
|
-
expect(q.read).to eql "Piotr"
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'yes?' do
|
51
|
-
it 'agrees' do
|
52
|
-
input << 'yes'
|
53
|
-
input.rewind
|
54
|
-
expect(shell.yes?("Are you a human?")).to eq(true)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'disagrees' do
|
58
|
-
input << 'no'
|
59
|
-
input.rewind
|
60
|
-
expect(shell.yes?("Are you a human?")).to eq(false)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'no?' do
|
65
|
-
it 'agrees' do
|
66
|
-
input << 'no'
|
67
|
-
input.rewind
|
68
|
-
expect(shell.no?("Are you a human?")).to eq(true)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'disagrees' do
|
72
|
-
input << 'yes'
|
73
|
-
input.rewind
|
74
|
-
expect(shell.no?("Are you a human?")).to eq(false)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
RSpec.describe TTY::Shell::Distance, '.distance' do
|
6
|
-
let(:object) { described_class.new }
|
7
|
-
|
8
|
-
subject(:distance) { object.distance(*strings) }
|
9
|
-
|
10
|
-
context 'when nil' do
|
11
|
-
let(:strings) { [nil, nil] }
|
12
|
-
|
13
|
-
it { is_expected.to eql(0) }
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'when empty' do
|
17
|
-
let(:strings) { ['', ''] }
|
18
|
-
|
19
|
-
it { is_expected.to eql(0) }
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'with one non empty' do
|
23
|
-
let(:strings) { ['abc', ''] }
|
24
|
-
|
25
|
-
it { is_expected.to eql(3) }
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'when single char' do
|
29
|
-
let(:strings) { ['a', 'abc'] }
|
30
|
-
|
31
|
-
it { is_expected.to eql(2) }
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when similar' do
|
35
|
-
let(:strings) { ['abc', 'abc'] }
|
36
|
-
|
37
|
-
it { is_expected.to eql(0) }
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'when similar' do
|
41
|
-
let(:strings) { ['abc', 'acb'] }
|
42
|
-
|
43
|
-
it { is_expected.to eql(1) }
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'when end similar' do
|
47
|
-
let(:strings) { ['saturday', 'sunday'] }
|
48
|
-
|
49
|
-
it { is_expected.to eql(3) }
|
50
|
-
end
|
51
|
-
|
52
|
-
context 'when contain similar' do
|
53
|
-
let(:strings) { ['which', 'witch'] }
|
54
|
-
|
55
|
-
it { is_expected.to eql(2) }
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'when prefix' do
|
59
|
-
let(:strings) { ['sta', 'status'] }
|
60
|
-
|
61
|
-
it { is_expected.to eql(3) }
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when similar' do
|
65
|
-
let(:strings) { ['smellyfish','jellyfish'] }
|
66
|
-
|
67
|
-
it { is_expected.to eql(2) }
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'when unicode' do
|
71
|
-
let(:strings) { ['マラソン五輪代表', 'ララソン五輪代表'] }
|
72
|
-
|
73
|
-
it { is_expected.to eql(1) }
|
74
|
-
end
|
75
|
-
end
|