command_tree 0.1.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 669d97d138e5a9b26b34f51561538f6a40e306f3f22682c854f74d8d1a6e76a6
4
- data.tar.gz: bdf8eea29094e6ec232bd32b9ed2b5f0b619b851c48b6e1049325c91dd6610a4
3
+ metadata.gz: 176b3e5d77f912e73b5c72557c96fbb1582eed07f3c533ee6837770fbc6de53f
4
+ data.tar.gz: eab685983241b03ef1f6e49b034a98ec9aa70c57c62c8d5235002d448bc9f254
5
5
  SHA512:
6
- metadata.gz: b169b9c1795f6d82cf335292a53c624b0ecf8cda0c99582753ef8beee229b43833a569bb4698d6d6c3e05aadf83152ffa25b8bb0727843777d19383dee22e4fb
7
- data.tar.gz: b2cc19380bff9655d04e0847624f042781a4359fd548bea49eced3d6e898a485b0313c0f0971630a5e310380b16c391404c7216209b016627ab110d3c6b55513
6
+ metadata.gz: 637cfb3d7ee3570fb0f39ef9f447f4880729bb3053c9816c3e4514c4e6df8000f14179688ff4a44f3a6c72c5ca9ea23b722fbaecec9a097e2f9bf769e0b50d8c
7
+ data.tar.gz: cf69dbace893ff6d3537e26cc681df533260d6473aca76817a1ddbd8d3b444bbf1cbfb370db4863d6ea9e38b94a7d13d3622717157b7c938f1499d1701ee9684
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- command_tree (0.1.1)
4
+ command_tree (0.1.2)
5
5
  colorize (~> 0.8)
6
6
 
7
7
  GEM
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 can register a command category (a node that contains commands)
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.
@@ -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
- calls[path] = { name: name, options: options, block: block } if block_given?
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
- private
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 { |key| key.start_with?(path) && key.length == (path.length + 1) }
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
- " #{child_node[:name].ljust(40)}".cyan
63
- else
64
- "+#{child_node[:name].ljust(40)}".light_magenta.bold
65
- end
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
@@ -1,3 +1,3 @@
1
1
  module CommandTree
2
- VERSION = "0.1.1"
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emad Elsaid