menu_commander 0.1.5 → 0.1.6

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: 75da68a2ccd79c2b07b8e61c047dd1047234cb3665c71450cc0bd09402c08cff
4
- data.tar.gz: 44400006ab0b7525e94459032c0f51393aee61f2bccec3185434676441a8aa78
3
+ metadata.gz: ac6321af9d45e46d217819d6659c88786ad23f6946d8c7f63076ed98ad8c3ba5
4
+ data.tar.gz: 650c78d949042c8a3651edad05bc82491fcd7b7095fc4b638b4e52dcfe9b0b74
5
5
  SHA512:
6
- metadata.gz: fea9fd9cf567cc67ce6c0bea3edb78fb5819c2ef83dcdce541f00868aba2a508711bb169bdb58bef496bdb5b15afe99863fec968b7545217c916da3a0f278e6c
7
- data.tar.gz: a29a5eaf1a29b696bab6d36e17a88b57c07366993b0445750c3e03dc6c700fcdcea934bb376497d8216cd03dceb2d517a2036c2f579fad490124cec68c5dc733
6
+ metadata.gz: d53c7fc17bd0ee211262624b0973d49eff0c433d5b819847989b211d452514e531d3183a662a9d1b878c916086a92b57ed3359d6b624a414cef470ee8f8410c2
7
+ data.tar.gz: 446c8c2a2f6d99af790e1f011251a6005a83173aec102b2b6bf792bcce57bd9dd343a9d5488ce969da5a1f956203f22cf475a056234092b737786f6b32a3ef40
data/README.md CHANGED
@@ -13,6 +13,7 @@ Easily create menus for any command line tool using simple YAML configuration.
13
13
 
14
14
  * [Installation](#installation)
15
15
  * [Usage](#usage)
16
+ * [Menu Navigation](#menu-navigation)
16
17
  * [Menu Configuration Features](#menu-configuration-features)
17
18
  * [Minimal menu requirements](#minimal-menu-requirements)
18
19
  * [Argument sub-menus](#argument-sub-menus)
@@ -67,9 +68,18 @@ $ menu
67
68
  $ menu some-other-file
68
69
  ```
69
70
 
70
-
71
71
  ![Demo](/demo/demo.gif)
72
72
 
73
+ ### Menu Navigation
74
+
75
+ - When a menu or sub menu has more than 10 items, it will become paginated
76
+ and a search filter will be added.
77
+ - Pressing <kbd>Home</kbd> from any nested menu will go back to the first
78
+ menu.
79
+ - Pressing <kbd>Page Up</kbd> from any nested menu will go back to the
80
+ previous menu.
81
+
82
+
73
83
 
74
84
  Menu Configuration Features
75
85
  --------------------------------------------------
@@ -7,11 +7,27 @@ module MenuCommander
7
7
  # :nocov: - covered by external process
8
8
  attr_reader :paths, :config
9
9
 
10
- def initialize(message = nil, paths: nil, config: nil)
10
+ def initialize(message=nil, paths: nil, config: nil)
11
11
  message ||= "Could not find menu configuration file"
12
12
  @paths, @config = paths, config
13
13
  super message
14
14
  end
15
15
  # :nocov:
16
16
  end
17
+
18
+ class ExitMenu < Interrupt
19
+ # :nocov:
20
+ def initialize(message=nil)
21
+ super (message || "> Exit")
22
+ end
23
+ # :nocov:
24
+ end
25
+
26
+ class MenuNavigation < StandardError
27
+ attr_reader :menu
28
+
29
+ def initialize(menu)
30
+ @menu = menu
31
+ end
32
+ end
17
33
  end
@@ -15,8 +15,13 @@ module MenuCommander
15
15
  menu ||= config['menu']
16
16
  response = select menu
17
17
  response = combine_commands response if response.is_a? Array
18
-
18
+
19
+ history << menu
20
+
19
21
  response.is_a?(String) ? evaluate(response) : call(response)
22
+ rescue MenuNavigation => e
23
+ call e.menu
24
+
20
25
  end
21
26
 
22
27
  def header
@@ -25,6 +30,10 @@ module MenuCommander
25
30
 
26
31
  private
27
32
 
33
+ def history
34
+ @history ||= []
35
+ end
36
+
28
37
  def combine_commands(command_array)
29
38
  command_array.map { |cmd| "(#{cmd})" }.join ' && '
30
39
  end
@@ -72,26 +81,50 @@ module MenuCommander
72
81
  end
73
82
 
74
83
  def prompt
75
- @prompt ||= TTY::Prompt.new
84
+ @prompt ||= prompt!
85
+ end
86
+
87
+ def prompt!
88
+ result = TTY::Prompt.new
89
+ result.on(:keypress) { |event| handle_keypress event }
90
+ result
76
91
  end
77
92
 
93
+ def handle_keypress(event)
94
+ case event.key.name
95
+ when :page_up
96
+ parent_menu = history.pop
97
+ raise MenuNavigation.new(parent_menu) if parent_menu
98
+
99
+ when :home
100
+ home_menu = history.first
101
+ if home_menu
102
+ @history = []
103
+ raise MenuNavigation.new(home_menu)
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+
78
110
  def ask(title)
79
111
  prompt.ask "> #{title}:"
80
112
 
81
113
  rescue TTY::Reader::InputInterrupt
82
114
  # :nocov:
83
- raise Interrupt, "Goodbye"
115
+ raise ExitMenu
84
116
  # :nocov:
85
117
 
86
118
  end
87
119
 
88
120
  def select(options, title=nil)
89
121
  title = title ? "> #{title}:" : ">"
90
- prompt.select title, options, symbols: { marker: '>' }, per_page: 10, filter: true
122
+ enable_filter = options.size > 10
123
+ prompt.select title, options, symbols: { marker: '>' }, per_page: 10, filter: enable_filter
91
124
 
92
125
  rescue TTY::Reader::InputInterrupt
93
126
  # :nocov:
94
- raise Interrupt, "Goodbye"
127
+ raise ExitMenu
95
128
  # :nocov:
96
129
 
97
130
  end
@@ -1,3 +1,3 @@
1
1
  module MenuCommander
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -3,4 +3,7 @@ require 'menu_commander/menu'
3
3
  require 'menu_commander/command'
4
4
  require 'menu_commander/cli'
5
5
 
6
- require 'byebug' if ENV['BYEBUG']
6
+ if ENV['BYEBUG']
7
+ require 'byebug'
8
+ require 'lp'
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menu_commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-18 00:00:00.000000000 Z
11
+ date: 2019-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mister_bin
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubygems_version: 3.0.4
103
+ rubygems_version: 3.0.3
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Create menus for any CLI tool