command_tree 0.1.0 → 0.1.1
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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -1
- data/lib/command_tree/tree.rb +18 -16
- data/lib/command_tree/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 669d97d138e5a9b26b34f51561538f6a40e306f3f22682c854f74d8d1a6e76a6
|
4
|
+
data.tar.gz: bdf8eea29094e6ec232bd32b9ed2b5f0b619b851c48b6e1049325c91dd6610a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b169b9c1795f6d82cf335292a53c624b0ecf8cda0c99582753ef8beee229b43833a569bb4698d6d6c3e05aadf83152ffa25b8bb0727843777d19383dee22e4fb
|
7
|
+
data.tar.gz: b2cc19380bff9655d04e0847624f042781a4359fd548bea49eced3d6e898a485b0313c0f0971630a5e310380b16c391404c7216209b016627ab110d3c6b55513
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
# CommandTree
|
1
|
+
# CommandTree [](https://badge.fury.io/rb/command_tree)
|
2
2
|
|
3
3
|
Builds trees of commands for the terminal, each node is either a group of commands or the command itself, every node is associated with a character to access it.
|
4
4
|
|
5
|
+
[](https://asciinema.org/a/202202)
|
6
|
+
The previous Asciinema script is here: https://gist.github.com/emad-elsaid/b259894caa9a78863b582ecc7a31811a
|
7
|
+
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
data/lib/command_tree/tree.rb
CHANGED
@@ -8,9 +8,9 @@ module CommandTree
|
|
8
8
|
@calls = { '' => {} }
|
9
9
|
end
|
10
10
|
|
11
|
-
def register(path, name, &block)
|
12
|
-
insure_path(path, name)
|
13
|
-
calls[path] = { name: name, block: block } if block_given?
|
11
|
+
def register(path, name, options = {}, &block)
|
12
|
+
insure_path(path, name, options)
|
13
|
+
calls[path] = { name: name, options: options, block: block } if block_given?
|
14
14
|
end
|
15
15
|
|
16
16
|
def show
|
@@ -21,11 +21,11 @@ module CommandTree
|
|
21
21
|
|
22
22
|
attr_accessor :calls
|
23
23
|
|
24
|
-
def insure_path(path, name)
|
24
|
+
def insure_path(path, name, options = {})
|
25
25
|
return if path.empty?
|
26
26
|
|
27
|
-
insure_path(path[0...-1], name)
|
28
|
-
calls[path] = { name: name } unless calls[path]
|
27
|
+
insure_path(path[0...-1], name, options)
|
28
|
+
calls[path] = { name: name, options: options } unless calls[path]
|
29
29
|
end
|
30
30
|
|
31
31
|
def execute_path(path)
|
@@ -33,15 +33,18 @@ module CommandTree
|
|
33
33
|
|
34
34
|
node = calls[path]
|
35
35
|
children = calls.keys.select { |key| key.start_with?(path) && key.length == (path.length + 1) }
|
36
|
+
children.sort!
|
37
|
+
|
38
|
+
puts "#{node[:name]}:".light_magenta.bold if node.key?(:name)
|
39
|
+
|
40
|
+
description = node.dig(:options, :desc)
|
41
|
+
puts description.to_s.light_black if description
|
36
42
|
|
37
43
|
node[:block].call if node.key?(:block)
|
38
44
|
|
39
45
|
return if children.empty?
|
40
46
|
|
41
|
-
puts "#{node[:name]}:".light_magenta.bold if node.key?(:name)
|
42
|
-
|
43
47
|
print_children(children)
|
44
|
-
|
45
48
|
choice = STDIN.getch
|
46
49
|
execute_path(path + choice)
|
47
50
|
end
|
@@ -65,16 +68,15 @@ module CommandTree
|
|
65
68
|
end
|
66
69
|
|
67
70
|
table(table_content, 40)
|
68
|
-
print "\n
|
71
|
+
print "\n"
|
69
72
|
end
|
70
73
|
|
71
74
|
def table(items, item_width)
|
72
|
-
_,
|
73
|
-
items_per_row =
|
74
|
-
items.
|
75
|
-
|
76
|
-
|
77
|
-
end
|
75
|
+
_, screen_width = IO.console.winsize
|
76
|
+
items_per_row = screen_width / item_width
|
77
|
+
items_dup = items.dup
|
78
|
+
|
79
|
+
puts items_dup.shift(items_per_row).join until items_dup.empty?
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
data/lib/command_tree/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emad Elsaid
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|