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.
-
#prompt_value ⇒ Object
Get the value of the prompt child object.
-
#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_options ⇒ Object
Show the list of menu options.
-
#title? ⇒ Boolean
Does the menu have a title?.
Methods inherited from Core::Obj
#add_child, can_create?, #can_receive_message?, #child_count, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #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.
142 143 144 |
# File 'lib/gloo/objs/cli/menu.rb', line 142 def self. return super + [ 'run' ] end |
.short_typename ⇒ Object
The short name of the object type.
36 37 38 |
# File 'lib/gloo/objs/cli/menu.rb', line 36 def self.short_typename return KEYWORD_SHORT end |
.typename ⇒ Object
The name of the object type.
29 30 31 |
# File 'lib/gloo/objs/cli/menu.rb', line 29 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.
118 119 120 |
# File 'lib/gloo/objs/cli/menu.rb', line 118 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.
127 128 129 130 131 132 133 |
# File 'lib/gloo/objs/cli/menu.rb', line 127 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.
65 66 67 68 69 70 71 |
# File 'lib/gloo/objs/cli/menu.rb', line 65 def add_loop_child o = find_child LOOP return if o fac = @engine.factory fac.create_bool LOOP, true, self end |
#add_quit_item ⇒ Object
Add a Quit menu item
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gloo/objs/cli/menu.rb', line 76 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.
176 177 178 179 180 181 182 183 184 |
# File 'lib/gloo/objs/cli/menu.rb', line 176 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.
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/gloo/objs/cli/menu.rb', line 212 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.
96 97 98 99 |
# File 'lib/gloo/objs/cli/menu.rb', line 96 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?
55 56 57 58 59 60 |
# File 'lib/gloo/objs/cli/menu.rb', line 55 def loop? o = find_child LOOP return false unless o return o.value end |
#msg_run ⇒ Object
Show the menu options, and prompt for user input.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/gloo/objs/cli/menu.rb', line 149 def msg_run lazy_add_children 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 end |
#prompt_value ⇒ Object
Get the value of the prompt child object. Returns nil if there is none.
44 45 46 47 48 49 |
# File 'lib/gloo/objs/cli/menu.rb', line 44 def prompt_value o = find_child PROMPT return '' unless o return o.value end |
#run_before_menu ⇒ Object
If there is a before menu script, run it now.
189 190 191 192 193 194 |
# File 'lib/gloo/objs/cli/menu.rb', line 189 def o = find_child BEFORE_MENU return unless o Gloo::Exec::Dispatch.( @engine, 'run', o ) end |
#run_command(cmd) ⇒ Object
Run the selected command.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/gloo/objs/cli/menu.rb', line 250 def run_command( cmd ) obj = find_cmd cmd unless obj if cmd == '?' else puts "#{cmd} is not a valid option" end return end script = obj.do_script return unless script s = Gloo::Exec::Script.new( @engine, script ) s.run end |
#run_default ⇒ Object
Run the default option.
227 228 229 230 231 232 233 234 235 |
# File 'lib/gloo/objs/cli/menu.rb', line 227 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.
240 241 242 243 244 245 |
# File 'lib/gloo/objs/cli/menu.rb', line 240 def run_default_title obj = find_child TITLE title = obj.value @engine.platform&.clear_screen Banner.( title, TITLE_STYLE, TITLE_COLOR ) end |
#show_options ⇒ Object
Show the list of menu options.
199 200 201 202 203 204 205 206 207 |
# File 'lib/gloo/objs/cli/menu.rb', line 199 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 |