command_tree 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +14 -1
- data/lib/command_tree/tree.rb +41 -7
- data/lib/command_tree/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 176b3e5d77f912e73b5c72557c96fbb1582eed07f3c533ee6837770fbc6de53f
|
4
|
+
data.tar.gz: eab685983241b03ef1f6e49b034a98ec9aa70c57c62c8d5235002d448bc9f254
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 637cfb3d7ee3570fb0f39ef9f447f4880729bb3053c9816c3e4514c4e6df8000f14179688ff4a44f3a6c72c5ca9ea23b722fbaecec9a097e2f9bf769e0b50d8c
|
7
|
+
data.tar.gz: cf69dbace893ff6d3537e26cc681df533260d6473aca76817a1ddbd8d3b444bbf1cbfb370db4863d6ea9e38b94a7d13d3622717157b7c938f1499d1701ee9684
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -29,7 +29,7 @@ You start by creating a new tree
|
|
29
29
|
t = CommandTree::Tree.new
|
30
30
|
```
|
31
31
|
|
32
|
-
Then you
|
32
|
+
Then you register a command category (a node that contains a group of commands)
|
33
33
|
|
34
34
|
```ruby
|
35
35
|
t.register 'a', 'Applications' # associate the character 'a' to a category called 'applications'
|
@@ -52,6 +52,19 @@ it will print the toplevel categories and commands and wait for you to press a c
|
|
52
52
|
|
53
53
|
when the tree reachs a leaf it'll exit, if a command is the leaf it will execute it and exit the tree giving your code the control again.
|
54
54
|
|
55
|
+
|
56
|
+
There is another way to define a group of commands in a nested way using `Tree#group` method as follows
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
t = CommandTree::Tree.new
|
60
|
+
t.group 'a', 'Applications' do |g|
|
61
|
+
g.register 'g','Google Chrome' do
|
62
|
+
system 'google-chrome-stable'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
t.show
|
66
|
+
```
|
67
|
+
|
55
68
|
## Development
|
56
69
|
|
57
70
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/command_tree/tree.rb
CHANGED
@@ -3,24 +3,56 @@ require 'colorize'
|
|
3
3
|
require 'colorized_string'
|
4
4
|
|
5
5
|
module CommandTree
|
6
|
+
# A tree of commands and associated keys for every node
|
6
7
|
class Tree
|
7
8
|
def initialize
|
8
9
|
@calls = { '' => {} }
|
9
10
|
end
|
10
11
|
|
12
|
+
# register a `path` to a `name` with a block of code if
|
13
|
+
# you wish it to be a command, the following `options` are
|
14
|
+
# supported:
|
15
|
+
# desc: a description of the item, as a help text for the user
|
11
16
|
def register(path, name, options = {}, &block)
|
12
17
|
insure_path(path, name, options)
|
13
|
-
|
18
|
+
return unless block_given?
|
19
|
+
|
20
|
+
calls[path] = { name: name, options: options, block: block }
|
21
|
+
end
|
22
|
+
|
23
|
+
# define a group of commands (subtree)
|
24
|
+
# the method will create a subtree and pass it to
|
25
|
+
# the given block of code if you passed a block
|
26
|
+
# otherwise it works in a similar way to register
|
27
|
+
def group(prefix, name, options = {})
|
28
|
+
subtree = self.class.new
|
29
|
+
yield(subtree) if block_given?
|
30
|
+
|
31
|
+
merge(subtree, prefix, name, options)
|
14
32
|
end
|
15
33
|
|
34
|
+
# Start the tree, prints the first level and walk
|
35
|
+
# the user through the tree with keystroks
|
16
36
|
def show
|
17
37
|
execute_path('')
|
18
38
|
end
|
19
39
|
|
20
|
-
|
40
|
+
# merge a subtree with a prefix and a name
|
41
|
+
def merge(subtree, prefix, name, options = {})
|
42
|
+
register(prefix, name, options)
|
43
|
+
subtree.calls.each do |key, command|
|
44
|
+
next if key.empty?
|
45
|
+
|
46
|
+
calls["#{prefix}#{key}"] = command
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
21
51
|
|
22
52
|
attr_accessor :calls
|
23
53
|
|
54
|
+
private
|
55
|
+
|
24
56
|
def insure_path(path, name, options = {})
|
25
57
|
return if path.empty?
|
26
58
|
|
@@ -32,7 +64,9 @@ module CommandTree
|
|
32
64
|
return puts "#{path} couldn't be found..." unless calls.key?(path)
|
33
65
|
|
34
66
|
node = calls[path]
|
35
|
-
children = calls.keys.select
|
67
|
+
children = calls.keys.select do |key|
|
68
|
+
key.start_with?(path) && key.length == (path.length + 1)
|
69
|
+
end
|
36
70
|
children.sort!
|
37
71
|
|
38
72
|
puts "#{node[:name]}:".light_magenta.bold if node.key?(:name)
|
@@ -59,10 +93,10 @@ module CommandTree
|
|
59
93
|
output << ' → '.light_black
|
60
94
|
|
61
95
|
output << if child_node.key?(:block)
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
96
|
+
" #{child_node[:name].ljust(40)}".cyan
|
97
|
+
else
|
98
|
+
"+#{child_node[:name].ljust(40)}".light_magenta.bold
|
99
|
+
end
|
66
100
|
|
67
101
|
table_content << output
|
68
102
|
end
|
data/lib/command_tree/version.rb
CHANGED