command_tree 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15fb161d51f86da4d50b5c0748051e91373d0258a3c8459e124e238dd79b3cd8
4
- data.tar.gz: 97f50232b6db9de2aeebaf01681911ab67570b98fe6a54953eaa982c7e83b9ef
3
+ metadata.gz: 669d97d138e5a9b26b34f51561538f6a40e306f3f22682c854f74d8d1a6e76a6
4
+ data.tar.gz: bdf8eea29094e6ec232bd32b9ed2b5f0b619b851c48b6e1049325c91dd6610a4
5
5
  SHA512:
6
- metadata.gz: d7462dc4a273fdf456e879b9320a54222a204c9192efec5ae294a0113a6fb9cec96fb94b3fe8c472bf6676996f317c682181146609cbb419457dba4322706fbe
7
- data.tar.gz: d2ed71c2a8b8f587d1fc5d623386a6773e80f370f0207bcd334fb11aa615b7fd7723aac67c5e87d4e1ab62c0e5f490f1ede8d79804d6f9c50590cb0d6a4aaecf
6
+ metadata.gz: b169b9c1795f6d82cf335292a53c624b0ecf8cda0c99582753ef8beee229b43833a569bb4698d6d6c3e05aadf83152ffa25b8bb0727843777d19383dee22e4fb
7
+ data.tar.gz: b2cc19380bff9655d04e0847624f042781a4359fd548bea49eced3d6e898a485b0313c0f0971630a5e310380b16c391404c7216209b016627ab110d3c6b55513
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .DS_Store
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- command_tree (0.1.0)
4
+ command_tree (0.1.1)
5
5
  colorize (~> 0.8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
- # CommandTree
1
+ # CommandTree [![Gem Version](https://badge.fury.io/rb/command_tree.svg)](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
+ [![asciicast](https://asciinema.org/a/202202.png)](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:
@@ -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\n"
71
+ print "\n"
69
72
  end
70
73
 
71
74
  def table(items, item_width)
72
- _, width = IO.console.winsize
73
- items_per_row = width / item_width
74
- items.each_with_index do |item, index|
75
- print item
76
- print "\n" if ((index + 1) % items_per_row).zero? && index > 0
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
@@ -1,3 +1,3 @@
1
1
  module CommandTree
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-21 00:00:00.000000000 Z
11
+ date: 2018-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize