Class: Gloo::Objs::Menu

Inherits:
Core::Obj show all
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

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

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

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



142
143
144
# File 'lib/gloo/objs/cli/menu.rb', line 142

def self.messages
  return super + [ 'run' ]
end

.short_typenameObject

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

.typenameObject

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.

Returns:



118
119
120
# File 'lib/gloo/objs/cli/menu.rb', line 118

def add_children_on_create?
  return true
end

#add_default_childrenObject

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_childObject

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_itemObject

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_menuObject

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 begin_menu
  run_before_menu

  # Check to see if we should show items at all.
  o = find_child HIDE_ITEMS
  return if o && o.value == true

  show_options
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_childrenObject

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?

Returns:



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_runObject

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
    begin_menu
    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_valueObject

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_menuObject

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 run_before_menu
  o = find_child BEFORE_MENU
  return unless o

  Gloo::Exec::Dispatch.message( @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 == '?'
      show_options
    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_defaultObject

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_titleObject

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.show_banner( title, TITLE_STYLE, TITLE_COLOR )
end

#show_optionsObject

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 show_options
  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?Boolean

Does the menu have a title?

Returns:



104
105
106
107
# File 'lib/gloo/objs/cli/menu.rb', line 104

def title?
  o = find_child TITLE
  return o ? true : false
end