command_tree 0.1.2 → 0.1.3

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: 176b3e5d77f912e73b5c72557c96fbb1582eed07f3c533ee6837770fbc6de53f
4
- data.tar.gz: eab685983241b03ef1f6e49b034a98ec9aa70c57c62c8d5235002d448bc9f254
3
+ metadata.gz: 05ab09a1469a4d3a4ffc2df192457f327cca0a2e3e984bcd554dae62b9e81019
4
+ data.tar.gz: 760f177dcc67462b17931ff4617142165d35539a82a15f5aeecc772824adb073
5
5
  SHA512:
6
- metadata.gz: 637cfb3d7ee3570fb0f39ef9f447f4880729bb3053c9816c3e4514c4e6df8000f14179688ff4a44f3a6c72c5ca9ea23b722fbaecec9a097e2f9bf769e0b50d8c
7
- data.tar.gz: cf69dbace893ff6d3537e26cc681df533260d6473aca76817a1ddbd8d3b444bbf1cbfb370db4863d6ea9e38b94a7d13d3622717157b7c938f1499d1701ee9684
6
+ metadata.gz: 5d6b22a9224d94d7756ac716c3b24e1ca6954ea3d7d2db003658429858d0c5f20034803754c609abd14013bd85924fb5c5ba09609961c40a32a1027aa1842675
7
+ data.tar.gz: '058775d799989ee2d609791937770105569891f979811e2b678f87fd70cdbdfea6f55394e699ebdd911baa9126337cf387739bb54a0995315ec545a4d237698c'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- command_tree (0.1.2)
4
+ command_tree (0.1.3)
5
5
  colorize (~> 0.8)
6
6
 
7
7
  GEM
@@ -0,0 +1,33 @@
1
+ module CommandTree
2
+ # A command class
3
+ class Command
4
+ attr_reader :name, :desc, :prefix, :block
5
+
6
+ def initialize(prefix, name, options = {}, &block)
7
+ @prefix = prefix
8
+ @name = name
9
+ @desc = options[:desc]
10
+ @block = block
11
+ end
12
+
13
+ def execute
14
+ print_banner
15
+ block.call
16
+ end
17
+
18
+ private
19
+
20
+ def print_banner
21
+ puts pretty_name if name
22
+ puts pretty_description if desc
23
+ end
24
+
25
+ def pretty_name
26
+ name.light_magenta.bold if name
27
+ end
28
+
29
+ def pretty_description
30
+ desc.to_s.light_black if desc
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ module CommandTree
2
+ # A group of commands
3
+ class Group
4
+ attr_reader :name, :desc, :prefix
5
+
6
+ def initialize(prefix, name, options = {})
7
+ @prefix = prefix
8
+ @name = name
9
+ @desc = options[:desc]
10
+ end
11
+
12
+ def execute
13
+ print_banner
14
+ end
15
+
16
+ private
17
+
18
+ def print_banner
19
+ puts pretty_name if name
20
+ puts pretty_description if desc
21
+ end
22
+
23
+ def pretty_name
24
+ name.light_magenta.bold if name
25
+ end
26
+
27
+ def pretty_description
28
+ desc.to_s.light_black if desc
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ require 'colorize'
2
+ require 'colorized_string'
3
+
4
+ module CommandTree
5
+ # responsible for showing the menu in terminal
6
+ class TextMenu
7
+ def initialize(item_width)
8
+ @item_width = item_width
9
+ @items = []
10
+ end
11
+
12
+ def render
13
+ _, screen_width = IO.console.winsize
14
+ items_per_row = screen_width / item_width
15
+
16
+ names = items.dup.map! { |item| item_name(item) }
17
+ descs = items.dup.map! { |item| item_desc(item) }
18
+
19
+ until names.empty?
20
+ puts names.shift(items_per_row).join
21
+ puts descs.shift(items_per_row).join
22
+ end
23
+ end
24
+
25
+ def add(item)
26
+ items << item
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :items, :item_width
32
+
33
+ def item_name(item)
34
+ prefix = item.prefix
35
+ arrow = ' → '
36
+
37
+ name_width = item_width - prefix.length - arrow.length
38
+ name = (item.is_a?(Group) ? '+' : '') + item.name
39
+ name = name.ljust(name_width)
40
+
41
+ colored_name = item.is_a?(Group) ? name.light_magenta.bold : name.cyan
42
+
43
+ (prefix.green + arrow.light_black + colored_name)
44
+ end
45
+
46
+ def item_desc(item)
47
+ desc = item.desc.to_s
48
+
49
+ return (desc[0...item_width - 3] + '...').light_black if desc.length >= item_width
50
+ return desc.ljust(item_width).light_black
51
+ end
52
+ end
53
+ end
@@ -1,12 +1,14 @@
1
1
  require 'io/console'
2
- require 'colorize'
3
- require 'colorized_string'
2
+
3
+ require_relative 'group'
4
+ require_relative 'command'
5
+ require_relative 'text_menu'
4
6
 
5
7
  module CommandTree
6
8
  # A tree of commands and associated keys for every node
7
9
  class Tree
8
10
  def initialize
9
- @calls = { '' => {} }
11
+ @calls = { '' => nil }
10
12
  end
11
13
 
12
14
  # register a `path` to a `name` with a block of code if
@@ -14,10 +16,14 @@ module CommandTree
14
16
  # supported:
15
17
  # desc: a description of the item, as a help text for the user
16
18
  def register(path, name, options = {}, &block)
19
+ path = path.to_s
20
+ name = name.to_s
21
+ prefix = path[-1]
22
+
17
23
  insure_path(path, name, options)
18
24
  return unless block_given?
19
25
 
20
- calls[path] = { name: name, options: options, block: block }
26
+ calls[path] = Command.new(prefix, name, options, &block)
21
27
  end
22
28
 
23
29
  # define a group of commands (subtree)
@@ -41,7 +47,7 @@ module CommandTree
41
47
  def merge(subtree, prefix, name, options = {})
42
48
  register(prefix, name, options)
43
49
  subtree.calls.each do |key, command|
44
- next if key.empty?
50
+ next unless command
45
51
 
46
52
  calls["#{prefix}#{key}"] = command
47
53
  end
@@ -57,25 +63,16 @@ module CommandTree
57
63
  return if path.empty?
58
64
 
59
65
  insure_path(path[0...-1], name, options)
60
- calls[path] = { name: name, options: options } unless calls[path]
66
+ calls[path] = Group.new(path[-1], name, options) unless calls[path]
61
67
  end
62
68
 
63
69
  def execute_path(path)
64
70
  return puts "#{path} couldn't be found..." unless calls.key?(path)
65
71
 
66
72
  node = calls[path]
67
- children = calls.keys.select do |key|
68
- key.start_with?(path) && key.length == (path.length + 1)
69
- end
70
- children.sort!
71
-
72
- puts "#{node[:name]}:".light_magenta.bold if node.key?(:name)
73
-
74
- description = node.dig(:options, :desc)
75
- puts description.to_s.light_black if description
76
-
77
- node[:block].call if node.key?(:block)
73
+ node.execute if node
78
74
 
75
+ children = get_children(path)
79
76
  return if children.empty?
80
77
 
81
78
  print_children(children)
@@ -83,34 +80,21 @@ module CommandTree
83
80
  execute_path(path + choice)
84
81
  end
85
82
 
83
+ def get_children(path)
84
+ calls.keys.select do |key|
85
+ key.start_with?(path) && key.length == (path.length + 1)
86
+ end.sort
87
+ end
88
+
86
89
  def print_children(children)
87
- table_content = []
90
+ menu = TextMenu.new(40)
88
91
 
89
92
  children.each do |child|
90
- child_node = calls[child]
91
-
92
- output = child[-1].green
93
- output << ' → '.light_black
94
-
95
- output << if child_node.key?(:block)
96
- " #{child_node[:name].ljust(40)}".cyan
97
- else
98
- "+#{child_node[:name].ljust(40)}".light_magenta.bold
99
- end
100
-
101
- table_content << output
93
+ menu.add calls[child]
102
94
  end
103
95
 
104
- table(table_content, 40)
96
+ menu.render
105
97
  print "\n"
106
98
  end
107
-
108
- def table(items, item_width)
109
- _, screen_width = IO.console.winsize
110
- items_per_row = screen_width / item_width
111
- items_dup = items.dup
112
-
113
- puts items_dup.shift(items_per_row).join until items_dup.empty?
114
- end
115
99
  end
116
100
  end
@@ -1,3 +1,3 @@
1
1
  module CommandTree
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
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.2
4
+ version: 0.1.3
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-28 00:00:00.000000000 Z
11
+ date: 2018-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -86,6 +86,9 @@ files:
86
86
  - bin/setup
87
87
  - command_tree.gemspec
88
88
  - lib/command_tree.rb
89
+ - lib/command_tree/command.rb
90
+ - lib/command_tree/group.rb
91
+ - lib/command_tree/text_menu.rb
89
92
  - lib/command_tree/tree.rb
90
93
  - lib/command_tree/version.rb
91
94
  homepage: https://www.github.com/emad-elsaid/command_tree