prompt 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,15 @@
2
2
 
3
3
  This is updated when a new version is pushed to http://rubygems.org
4
4
 
5
+ ## 1.2.0 (Apr 21, 2012)
6
+
7
+ * DSL change: Command descriptions can be provided using `desc`. You can still
8
+ pass the description as an argument to `command`, but will be deprecated
9
+ in a future version. (thanks @kowsik)
10
+ * Partially-typed strings are now passed when calling a parameter's block. You
11
+ can use this to more efficiently build the list of completions. This is
12
+ especially useful for things like database queries or other remote API calls.
13
+
5
14
  ## 1.1.0 (Apr 18, 2012)
6
15
 
7
16
  * Format help command so that descriptions are always aligned
data/README.md CHANGED
@@ -1,5 +1,3 @@
1
- # Prompt
2
-
3
1
  ## What is this?
4
2
 
5
3
  Prompt makes it easy to build slick command-line applications with Tab Completion, Command History, and Built-in Help
@@ -8,7 +6,7 @@ Prompt makes it easy to build slick command-line applications with Tab Completio
8
6
 
9
7
  gem install prompt
10
8
 
11
- ## An example
9
+ ## Overview
12
10
 
13
11
  Commands are defined with a Sinatra-inspired DSL:
14
12
 
@@ -20,17 +18,20 @@ group "Move"
20
18
 
21
19
  param :direction, "A cardinal direction", %w(north east south west)
22
20
 
23
- command "go :direction", "Walk in the specified direction" do |direction|
21
+ desc "Walk in the specified direction"
22
+ command "go :direction" do |direction|
24
23
  puts "You walked #{direction} and were eaten by a grue."
25
24
  end
26
25
 
27
26
  group "Interact"
28
27
 
29
- command "look", "Look around" do
28
+ desc "Look around"
29
+ command "look" do
30
30
  puts "You're in a dark room."
31
31
  end
32
32
 
33
- command "say :something", "Say something" do |something|
33
+ desc "Say something"
34
+ command "say :something" do |something|
34
35
  puts "You say '#{something}'"
35
36
  end
36
37
 
@@ -68,18 +69,31 @@ The `help` command is built-in. It will print all of the commands that you've d
68
69
  > help
69
70
  Console commands
70
71
 
71
- help List all commands
72
- help -v List all commands, including parameters
73
- exit
72
+ help List all commands
73
+ help -v List all commands, including parameters
74
+ exit Exit the console
74
75
 
75
76
  Move
76
77
 
77
- go <direction> Walk in the specified direction
78
+ go <direction> Walk in the specified direction
78
79
 
79
80
  Interact
80
81
 
81
- look Look around
82
- say <something> Say something
82
+ look Look around
83
+ say <something> Say something
84
+
85
+ ## Describing commands
86
+
87
+ You can provide a description for a command before defining it. Descriptions
88
+ are displayed next to each comand in the built-in help.
89
+
90
+ ```ruby
91
+ desc "Look around"
92
+ command "look" do
93
+ ...
94
+ end
95
+ ```
96
+
83
97
 
84
98
  ## Grouping commands
85
99
 
@@ -141,6 +155,16 @@ param :file, "JPG file" do
141
155
  end
142
156
  ```
143
157
 
158
+ A dynamic parameter's block may optionally take the partially-typed word as
159
+ a parameter, to further limit the completions.
160
+
161
+ ```ruby
162
+ param :username do |starting_with|
163
+ # slow DB query
164
+ User.where("username LIKE '?%'", starting_with).map(&:username)
165
+ end
166
+ ```
167
+
144
168
  ## Configuration options
145
169
 
146
170
  The default prompt `"> "` can be changed before starting the console, or while it's running.
@@ -11,18 +11,21 @@ end
11
11
  @pwd = File.expand_path Dir.pwd
12
12
  change_prompt @pwd
13
13
 
14
- param :dir, "Directory" do
14
+ desc "Directory"
15
+ param :dir do
15
16
  Dir.entries(@pwd).select do |e|
16
17
  File.directory?(File.join(@pwd, e))
17
18
  end
18
19
  end
19
20
 
20
- command "cd :dir", "Change directory" do |dir|
21
+ desc "Change directory"
22
+ command "cd :dir" do |dir|
21
23
  @pwd = File.expand_path File.join(@pwd, dir)
22
24
  change_prompt @pwd
23
25
  end
24
26
 
25
- command "ls", "List files" do
27
+ desc "List files"
28
+ command "ls" do
26
29
  puts @pwd
27
30
  Dir.entries(@pwd).select do |e|
28
31
  path = File.join(@pwd, e)
@@ -34,17 +37,20 @@ command "ls", "List files" do
34
37
  end
35
38
  end
36
39
 
37
- command "pwd", "Print current directory" do
40
+ desc "Print current directory"
41
+ command "pwd" do
38
42
  puts @pwd
39
43
  end
40
44
 
41
- param :file, "File" do
45
+ desc "File"
46
+ param :file do
42
47
  Dir.entries(@pwd).select do |e|
43
48
  File.file?(File.join(@pwd, e))
44
49
  end
45
50
  end
46
51
 
47
- command "cp *file :dir", "Copy one or more files to dest" do |files, dir|
52
+ desc "Copy one or more files to dest"
53
+ command "cp *file :dir" do |files, dir|
48
54
  puts "Copying #{files.length} file(s) to #{dir}"
49
55
  # doesn't actually do it...
50
56
  end
@@ -11,7 +11,8 @@ group "Move"
11
11
 
12
12
  param :direction, "A cardinal direction", %w(north east south west)
13
13
 
14
- command "go :direction", "Walk in the specified direction" do |direction|
14
+ desc "Walk in the specified direction"
15
+ command "go :direction" do |direction|
15
16
  puts "You walked #{direction}"
16
17
  @moves += 1
17
18
  if @moves > GRUE
@@ -22,7 +23,8 @@ end
22
23
 
23
24
  group "Interact"
24
25
 
25
- command "look", "Look around" do
26
+ desc "Look around"
27
+ command "look" do
26
28
  if @moves < GRUE
27
29
  puts "You're in a nice, well-lit room"
28
30
  else
@@ -30,7 +32,8 @@ command "look", "Look around" do
30
32
  end
31
33
  end
32
34
 
33
- command "say *something", "Say something" do |something|
35
+ desc "Say something"
36
+ command "say *something" do |something|
34
37
  puts "You say '#{something.join(" ")}'"
35
38
  end
36
39
 
@@ -61,7 +61,7 @@ module Prompt
61
61
  def all_completions(args, partial_arg)
62
62
  commands.select { |c| c.could_match? args }.map do |c|
63
63
  c.completions(args.length, partial_arg)
64
- end.flatten(1)
64
+ end.flatten(1).sort.uniq
65
65
  end
66
66
 
67
67
  def current_command_group
@@ -12,18 +12,26 @@ module Prompt
12
12
  end
13
13
  Prompt.application.select_group(name)
14
14
  end
15
+
16
+ def desc text
17
+ @@last_desc = text
18
+ end
15
19
 
16
20
  def group desc
17
21
  Prompt.application.select_group(desc)
18
22
  end
19
23
 
20
24
  def command(name, desc = nil, &block)
25
+ desc ||= @@last_desc
26
+ @@last_desc = nil
21
27
  words = DSLHelper.words(name, @parameters || [])
22
28
  command = Command.new(words, desc, &block)
23
29
  Prompt.application.add_command(command)
24
30
  end
25
31
 
26
- def param(name, desc, values = nil, &block)
32
+ def param(name, desc = nil, values = nil, &block)
33
+ desc ||= @@last_desc
34
+ @@last_desc = nil
27
35
  @parameters = [] unless defined? @parameters
28
36
  raise "parameter :#{name} is already defined" if @parameters.find {|v| v.name == name}
29
37
  @parameters << if block
@@ -9,15 +9,16 @@ module Prompt
9
9
  @desc = desc
10
10
  @values = values || []
11
11
  @proc = block if block_given?
12
+ @cached_values = {}
12
13
  end
13
14
 
14
15
  def clear_cached_values
15
- @cached_values = nil
16
+ @cached_values = {}
16
17
  end
17
18
 
18
19
  def completions(starting_with = "")
19
20
  all = if @proc
20
- @cached_values ||= @proc.call
21
+ @cached_values[starting_with] ||= @proc.call(starting_with)
21
22
  else
22
23
  @values
23
24
  end
@@ -1,5 +1,5 @@
1
1
  module Prompt
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
 
4
4
  class << self
5
5
 
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: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-18 00:00:00.000000000 Z
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Prompt makes it easy to build slick command-line applications with tab
15
15
  completion, command history, and built-in help