Class: Gloo::Objs::Menu
- Inherits:
-
Core::Obj
- Object
- Core::Baseo
- Core::Obj
- Gloo::Objs::Menu
- Defined in:
- lib/gloo/objs/cli/menu.rb
Constant Summary collapse
- KEYWORD =
'menu'.freeze
- KEYWORD_SHORT =
'menu'.freeze
- PROMPT =
'prompt'.freeze
- ITEMS =
'items'.freeze
- LOOP =
'loop'.freeze
- HIDE_ITEMS =
'hide_items'.freeze
- BEFORE_MENU =
'before_menu'.freeze
- DEFAULT =
'default'.freeze
- TITLE =
'title'.freeze
- TITLE_STYLE =
'straight'.freeze
- TITLE_COLOR =
'bright_cyan'.freeze
- QUIT_ITEM_NAME =
'q'.freeze
[]
Constants inherited from Core::Baseo
Core::Baseo::NOT_IMPLEMENTED_ERR
Instance Attribute Summary
Attributes inherited from Core::Obj
Attributes inherited from Core::Baseo
Class Method Summary collapse
-
.messages ⇒ Object
Get a list of message names that this object receives.
-
.short_typename ⇒ Object
The short name of the object type.
-
.typename ⇒ Object
The name of the object type.
Instance Method Summary collapse
-
#add_children_on_create? ⇒ Boolean
Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.
-
#add_default_children ⇒ Object
Add children to this object.
-
#add_loop_child ⇒ Object
If there is no loop child, add it.
-
#add_quit_item ⇒ Object
Add a Quit menu item.
-
#begin_menu ⇒ Object
Begin the menu execution.
-
#find_cmd(cmd) ⇒ Object
Find the command matching user input.
-
#lazy_add_children ⇒ Object
Add any required children not specified in the source.
-
#loop? ⇒ Boolean
Get the value of the loop child object.
-
#msg_run ⇒ Object
Show the menu options, and prompt for user input.
-
#pop_menu(menu) ⇒ Object
Pop a menu from the stack.
-
#pop_to_top_level_menu ⇒ Object
Quit all menus and drop into gloo.
-
#prompt_value ⇒ Object
Get the value of the prompt child object.
-
#push_menu(obj) ⇒ Object
Add a menu to the stack.
-
#quit_all_menus ⇒ Object
Quit all menus and drop into gloo.
-
#run_before_menu ⇒ Object
If there is a before menu script, run it now.
-
#run_command(cmd) ⇒ Object
Run the selected command.
-
#run_default ⇒ Object
Run the default option.
-
#run_default_title ⇒ Object
There is a title, so show it.
-
#show_menu_stack ⇒ Object
Show the bread-crumbs for the menu stack.
-
#show_options ⇒ Object
Show the list of menu options.
-
#title ⇒ Object
Get the Menu’s Title.
-
#title? ⇒ Boolean
Does the menu have a title?.
Methods inherited from Core::Obj
#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #is_alias?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?
Methods inherited from Core::Baseo
Constructor Details
This class inherits a constructor from Gloo::Core::Obj
Class Method Details
.messages ⇒ Object
Get a list of message names that this object receives.
220 221 222 |
# File 'lib/gloo/objs/cli/menu.rb', line 220 def self. return super + [ 'run' ] end |
.short_typename ⇒ Object
The short name of the object type.
38 39 40 |
# File 'lib/gloo/objs/cli/menu.rb', line 38 def self.short_typename return KEYWORD_SHORT end |
.typename ⇒ Object
The name of the object type.
31 32 33 |
# File 'lib/gloo/objs/cli/menu.rb', line 31 def self.typename return KEYWORD end |
Instance Method Details
#add_children_on_create? ⇒ Boolean
Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.
133 134 135 |
# File 'lib/gloo/objs/cli/menu.rb', line 133 def add_children_on_create? return true end |
#add_default_children ⇒ Object
Add children to this object. This is used by containers to add children needed for default configurations.
142 143 144 145 146 147 148 |
# File 'lib/gloo/objs/cli/menu.rb', line 142 def add_default_children fac = @engine.factory fac.create_string PROMPT, '> ', self fac.create_can ITEMS, self fac.create_bool LOOP, true, self fac.create_script DEFAULT, '', self end |
#add_loop_child ⇒ Object
If there is no loop child, add it.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gloo/objs/cli/menu.rb', line 69 def add_loop_child o = find_child LOOP if o o.set_value true return end fac = @engine.factory fac.create_bool LOOP, true, self end |
#add_quit_item ⇒ Object
Add a Quit menu item
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gloo/objs/cli/menu.rb', line 83 def add_quit_item items = find_child ITEMS q = items.find_child QUIT_ITEM_NAME return if q fac = @engine.factory fac.create_bool LOOP, true, self params = { :name => QUIT_ITEM_NAME, :type => 'mitem', :value => 'Quit', :parent => items } mitem = fac.create params script = "put false into #{self.pn}.loop" fac.create_script 'do', script, mitem end |
#begin_menu ⇒ Object
Begin the menu execution. Run the before menu script if there is one, then show options unless we are hiding them by default.
256 257 258 259 260 261 262 263 264 |
# File 'lib/gloo/objs/cli/menu.rb', line 256 def # Check to see if we should show items at all. o = find_child HIDE_ITEMS return if o && o.value == true end |
#find_cmd(cmd) ⇒ Object
Find the command matching user input.
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/gloo/objs/cli/menu.rb', line 292 def find_cmd( cmd ) o = find_child ITEMS return nil unless o o.children.each do |mitem| mitem = Gloo::Objs::Alias.resolve_alias( @engine, mitem ) return mitem if mitem.shortcut_value.downcase == cmd.downcase end return nil end |
#lazy_add_children ⇒ Object
Add any required children not specified in the source.
103 104 105 106 |
# File 'lib/gloo/objs/cli/menu.rb', line 103 def lazy_add_children add_loop_child add_quit_item end |
#loop? ⇒ Boolean
Get the value of the loop child object. Should we keep looping or should we stop?
57 58 59 60 61 62 63 64 |
# File 'lib/gloo/objs/cli/menu.rb', line 57 def loop? return false unless @engine.running o = find_child LOOP return false unless o return o.value end |
#msg_run ⇒ Object
Show the menu options, and prompt for user input.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/gloo/objs/cli/menu.rb', line 227 def msg_run lazy_add_children self run_default loop do if prompt_value.empty? dt = DateTime.now d = dt.strftime( '%Y.%m.%d' ) t = dt.strftime( '%I:%M:%S' ) cmd = @engine.platform.prompt.ask( "\n#{d.yellow} #{t.white} >" ) else cmd = @engine.platform.prompt.ask( prompt_value ) end cmd ? run_command( cmd ) : run_default break unless loop? end self end |
#pop_menu(menu) ⇒ Object
Pop a menu from the stack. If the last item isn’t the given menu, it won’t be popped.
183 184 185 186 187 |
# File 'lib/gloo/objs/cli/menu.rb', line 183 def if [-1] == .pop end end |
#pop_to_top_level_menu ⇒ Object
Quit all menus and drop into gloo.
192 193 194 195 196 197 198 199 |
# File 'lib/gloo/objs/cli/menu.rb', line 192 def @engine.log.debug 'Quitting to top level menu' while .count > 1 = .pop o = .find_child LOOP o.set_value( false ) if o end end |
#prompt_value ⇒ Object
Get the value of the prompt child object. Returns nil if there is none.
46 47 48 49 50 51 |
# File 'lib/gloo/objs/cli/menu.rb', line 46 def prompt_value o = find_child PROMPT return '' unless o return o.value end |
#push_menu(obj) ⇒ Object
Add a menu to the stack.
174 175 176 |
# File 'lib/gloo/objs/cli/menu.rb', line 174 def obj << obj end |
#quit_all_menus ⇒ Object
Quit all menus and drop into gloo.
204 205 206 207 208 209 210 211 |
# File 'lib/gloo/objs/cli/menu.rb', line 204 def @engine.log.debug 'Dropping into Gloo' .each do || o = .find_child LOOP o.set_value( false ) if o end @engine.loop end |
#run_before_menu ⇒ Object
If there is a before menu script, run it now.
269 270 271 272 273 274 |
# File 'lib/gloo/objs/cli/menu.rb', line 269 def o = find_child BEFORE_MENU return unless o Gloo::Exec::Dispatch.( @engine, 'run', o ) end |
#run_command(cmd) ⇒ Object
Run the selected command.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/gloo/objs/cli/menu.rb', line 329 def run_command( cmd ) @engine.log.info "Menu Command: #{cmd}" obj = find_cmd cmd if obj script = obj.do_script return unless script s = Gloo::Exec::Script.new( @engine, script ) s.run else if cmd == '?' @engine.log.debug 'Showing options' elsif cmd == 'q!' @engine.log.debug 'Quitting Gloo' @engine.stop_running elsif cmd == 'qq' @engine.log.debug 'Quitting to top level menu' elsif cmd.starts_with? ':' gloo_cmd = cmd[1..-1].strip if gloo_cmd.blank? @engine.log.debug 'Quitting all menus and dropping into Gloo' else @engine.log.debug "Running Gloo command: #{gloo_cmd}" @engine.process_cmd gloo_cmd end else msg = "#{cmd} is not a valid option" @engine.log.warn msg end return end end |
#run_default ⇒ Object
Run the default option.
307 308 309 310 311 312 313 314 315 |
# File 'lib/gloo/objs/cli/menu.rb', line 307 def run_default obj = find_child DEFAULT if obj s = Gloo::Exec::Script.new( @engine, obj ) s.run elsif title? run_default_title end end |
#run_default_title ⇒ Object
There is a title, so show it.
320 321 322 323 324 |
# File 'lib/gloo/objs/cli/menu.rb', line 320 def run_default_title @engine.platform&.clear_screen Banner.( title, TITLE_STYLE, TITLE_COLOR ) end |
#show_menu_stack ⇒ Object
Show the bread-crumbs for the menu stack.
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/gloo/objs/cli/menu.rb', line 157 def if .count < 2 puts '...' else msg = '' [0..-2].each do || msg << ' | ' unless msg.blank? msg << .title end msg << ' | ... ' puts msg end end |
#show_options ⇒ Object
Show the list of menu options.
279 280 281 282 283 284 285 286 287 |
# File 'lib/gloo/objs/cli/menu.rb', line 279 def o = find_child ITEMS return unless o o.children.each do |mitem| mitem = Gloo::Objs::Alias.resolve_alias( @engine, mitem ) puts " #{mitem.shortcut_value} - #{mitem.description_value}" end end |
#title ⇒ Object
Get the Menu’s Title.
119 120 121 122 |
# File 'lib/gloo/objs/cli/menu.rb', line 119 def title obj = find_child TITLE return obj.value end |
#title? ⇒ Boolean
Does the menu have a title?
111 112 113 114 |
# File 'lib/gloo/objs/cli/menu.rb', line 111 def title? o = find_child TITLE return o ? true : false end |