prompt 0.0.1 → 0.0.2
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.
- data/README.md +12 -3
- data/examples/file_manager +15 -2
- data/examples/mud +1 -1
- data/lib/prompt/application.rb +2 -0
- data/lib/prompt/command.rb +5 -2
- data/lib/prompt/console/console_module.rb +1 -12
- data/lib/prompt/dsl.rb +1 -0
- data/lib/prompt/glob_variable.rb +14 -0
- data/lib/prompt/prompt_module.rb +2 -0
- data/lib/prompt/variable.rb +2 -2
- data/spec/prompt/command_spec.rb +25 -0
- metadata +2 -1
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
## What is this?
|
4
4
|
|
5
|
-
Prompt makes it easy to build slick command-line applications with
|
5
|
+
Prompt makes it easy to build slick command-line applications with Tab Completion, Command History, and Built-in Help
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -108,6 +108,15 @@ end
|
|
108
108
|
|
109
109
|
Here, the variables are named `first` and `last`. Their values are be passed as arguments to the command's block, in the order in which they appear.
|
110
110
|
|
111
|
+
|
112
|
+
Each `:variable` only matches a single word. If you want to match multiple words to one variable, use a `*variable`.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
command "say *sentence" do |sentence|
|
116
|
+
puts "You say '#{sentence}'"
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
111
120
|
### Defining variables
|
112
121
|
|
113
122
|
It's not necessary to define a variable before using it in a command, but doing so will allow you to provide a useful description and valid values for the variable.
|
@@ -136,8 +145,8 @@ end
|
|
136
145
|
|
137
146
|
## Configuration options
|
138
147
|
|
139
|
-
The default prompt `"> "` can be changed before starting the console.
|
148
|
+
The default prompt `"> "` can be changed before starting the console, or while it's running.
|
140
149
|
|
141
150
|
```ruby
|
142
|
-
Prompt
|
151
|
+
Prompt.application.prompt = "#{Dir.pwd}> "
|
143
152
|
```
|
data/examples/file_manager
CHANGED
@@ -4,21 +4,34 @@ require 'prompt'
|
|
4
4
|
|
5
5
|
extend Prompt::DSL
|
6
6
|
|
7
|
+
def change_prompt dir
|
8
|
+
Prompt.application.prompt = "#{@pwd}> "
|
9
|
+
end
|
10
|
+
|
7
11
|
@pwd = File.absolute_path Dir.pwd
|
12
|
+
change_prompt @pwd
|
8
13
|
|
9
14
|
dynamic_variable :dir, "Directory" do
|
10
15
|
Dir.entries(@pwd).select do |e|
|
11
|
-
File.directory? e
|
16
|
+
File.directory?(File.join(@pwd, e))
|
12
17
|
end
|
13
18
|
end
|
14
19
|
|
15
20
|
command "cd :dir", "Change directory" do |dir|
|
16
21
|
@pwd = File.absolute_path File.join(@pwd, dir)
|
22
|
+
change_prompt @pwd
|
17
23
|
end
|
18
24
|
|
19
25
|
command "ls", "List files" do
|
20
26
|
puts @pwd
|
21
|
-
|
27
|
+
Dir.entries(@pwd).select do |e|
|
28
|
+
path = File.join(@pwd, e)
|
29
|
+
if File.directory?(path)
|
30
|
+
puts "#{e}/"
|
31
|
+
else
|
32
|
+
puts e
|
33
|
+
end
|
34
|
+
end
|
22
35
|
end
|
23
36
|
|
24
37
|
command "pwd", "Print current directory" do
|
data/examples/mud
CHANGED
data/lib/prompt/application.rb
CHANGED
data/lib/prompt/command.rb
CHANGED
@@ -17,7 +17,7 @@ module Prompt
|
|
17
17
|
|
18
18
|
def match(str)
|
19
19
|
if m = regex.match(str.strip)
|
20
|
-
m[
|
20
|
+
variables.map {|v| m[v.name.to_s] }
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -43,7 +43,7 @@ module Prompt
|
|
43
43
|
private
|
44
44
|
|
45
45
|
def regex
|
46
|
-
|
46
|
+
begin
|
47
47
|
regex_strs = words.map do |word|
|
48
48
|
case word
|
49
49
|
when Variable
|
@@ -61,6 +61,9 @@ module Prompt
|
|
61
61
|
if word[0] == ":"
|
62
62
|
sym = word[1..-1].to_sym
|
63
63
|
@all_variables.find {|v| v.name == sym} || Variable.new(sym, sym.to_s)
|
64
|
+
elsif word[0] == "*"
|
65
|
+
sym = word[1..-1].to_sym
|
66
|
+
@all_variables.find {|v| v.name == sym} || GlobVariable.new(sym, sym.to_s)
|
64
67
|
else
|
65
68
|
word
|
66
69
|
end
|
@@ -5,15 +5,12 @@ module Prompt
|
|
5
5
|
module Console
|
6
6
|
|
7
7
|
HISTORY_MAX_SIZE = 100
|
8
|
-
PROMPT = "> "
|
9
8
|
|
10
9
|
CompletionProc = proc do |s|
|
11
10
|
Prompt.application.completions(s)
|
12
11
|
end
|
13
12
|
|
14
13
|
def self.start(history_file = nil)
|
15
|
-
@prompt = PROMPT
|
16
|
-
|
17
14
|
# Store the state of the terminal
|
18
15
|
stty_save = `stty -g`.chomp
|
19
16
|
# and restore it when exiting
|
@@ -28,7 +25,7 @@ module Prompt
|
|
28
25
|
|
29
26
|
load_history history_file if history_file
|
30
27
|
|
31
|
-
while line = Readline.readline(
|
28
|
+
while line = Readline.readline(Prompt.application.prompt, true)
|
32
29
|
begin
|
33
30
|
Prompt.application.exec(line).nil?
|
34
31
|
rescue ::Prompt::CommandNotFound => e
|
@@ -37,14 +34,6 @@ module Prompt
|
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
40
|
-
def self.prompt= prompt
|
41
|
-
@prompt = prompt
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.prompt
|
45
|
-
@prompt
|
46
|
-
end
|
47
|
-
|
48
37
|
def self.save_history file
|
49
38
|
history_no_dups = Readline::HISTORY.to_a.reverse.uniq[0,HISTORY_MAX_SIZE].reverse
|
50
39
|
File.open(file, "w") do |f|
|
data/lib/prompt/dsl.rb
CHANGED
data/lib/prompt/prompt_module.rb
CHANGED
data/lib/prompt/variable.rb
CHANGED
@@ -13,9 +13,9 @@ module Prompt
|
|
13
13
|
|
14
14
|
def regex
|
15
15
|
if values
|
16
|
-
"(
|
16
|
+
"(?<#{name}>#{values.map{|v| Regexp.escape(v)}.join("|")})"
|
17
17
|
else
|
18
|
-
"([^\s]+)"
|
18
|
+
"('(?<#{name}>[^\']*)'|\"(?<#{name}>[^\"]*)\"|(?<#{name}>[^\s]+))"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
data/spec/prompt/command_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'prompt/command'
|
2
2
|
require 'prompt/variable'
|
3
|
+
require 'prompt/glob_variable'
|
3
4
|
|
4
5
|
include Prompt
|
5
6
|
|
@@ -25,6 +26,15 @@ describe Prompt::Command do
|
|
25
26
|
c = Command.new("hi :name")
|
26
27
|
|
27
28
|
c.match("hi guy").should == ["guy"]
|
29
|
+
c.match("hi 'some guy'").should == ["some guy"]
|
30
|
+
c.match("hi ''").should == [""]
|
31
|
+
c.match('hi "some guy"').should == ["some guy"]
|
32
|
+
c.match('hi ""').should == [""]
|
33
|
+
|
34
|
+
c.match("hi '").should == ["'"]
|
35
|
+
c.match('hi "').should == ['"']
|
36
|
+
c.match('hi \'"').should == ['\'"']
|
37
|
+
|
28
38
|
c.match("higuy").should be_nil
|
29
39
|
c.match("higuy guy").should be_nil
|
30
40
|
end
|
@@ -54,6 +64,21 @@ describe Prompt::Command do
|
|
54
64
|
c.match("go x").should be_nil
|
55
65
|
c.match("go nn").should be_nil
|
56
66
|
end
|
67
|
+
|
68
|
+
it "matches correctly with glob variable" do
|
69
|
+
c = Command.new("say *stuff")
|
70
|
+
|
71
|
+
c.match("say hello").should == ["hello"]
|
72
|
+
c.match("say hello world").should == ["hello world"]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "matches correctly with glob variable and other variable" do
|
76
|
+
c = Command.new("say *stuff :adverb")
|
77
|
+
|
78
|
+
c.match("say hello").should be_nil
|
79
|
+
c.match("say hello loudly").should == ["hello", "loudly"]
|
80
|
+
c.match("say hello world loudly").should == ["hello world", "loudly"]
|
81
|
+
end
|
57
82
|
end
|
58
83
|
|
59
84
|
describe "#variables" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prompt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,7 @@ files:
|
|
27
27
|
- lib/prompt/console/console_module.rb
|
28
28
|
- lib/prompt/console.rb
|
29
29
|
- lib/prompt/dsl.rb
|
30
|
+
- lib/prompt/glob_variable.rb
|
30
31
|
- lib/prompt/proc_variable.rb
|
31
32
|
- lib/prompt/prompt_module.rb
|
32
33
|
- lib/prompt/variable.rb
|