Class: Select

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/select.rb

Overview

Author

Eric Crane (mailto:eric.crane@mac.com)

Copyright

Copyright (c) 2020 Eric Crane. All rights reserved.

Show a CLI prompt and user selection from a list.

Constant Summary collapse

KEYWORD =
'select'.freeze
KEYWORD_SHORT =
'sel'.freeze
PROMPT =
'prompt'.freeze
OPTIONS =
'options'.freeze
RESULT =
'result'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the object's documentation data.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/select.rb', line 130

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Prompt for user to select from a list of options.',
    :children => [
      "prompt (string) — Default: '> '. The prompt displayed to the user.",
      'options (container) — The list of options for the selection list. The name of each option is presented to the user, but the value is put in the result.',
      "result (string) — The result with the user's selection."
    ],
    :messages => [
      'run — Prompt the user for a selection and then set the result.'
    ],
    :examples => <<~EXAMPLES.strip
      select [select] :
        prompt [string] : What is your favorite color?
        options [can] :
          red : r
          green : g
          blue : b
        result [string] :
        on_load [script] :
          run select
          show select.result
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



106
107
108
# File 'lib/select.rb', line 106

def self.messages
  return super + %w[run]
end

.short_typenameObject

The short name of the object type.



25
26
27
# File 'lib/select.rb', line 25

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



18
19
20
# File 'lib/select.rb', line 18

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:

  • (Boolean)


83
84
85
# File 'lib/select.rb', line 83

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.



92
93
94
95
96
97
# File 'lib/select.rb', line 92

def add_default_children
  fac = @engine.factory
  fac.create_string PROMPT, '>', self
  fac.create_can OPTIONS, self
  fac.create_string RESULT, nil, self
end

#key_for_option(selected) ⇒ Object

Get the value of the selected item.



53
54
55
56
57
58
59
60
61
62
# File 'lib/select.rb', line 53

def key_for_option( selected )
  o = find_child OPTIONS
  return nil unless o

  o.children.each do |c|
    return c.value if c.name == selected
  end

  return nil
end

#msg_runObject

Show the prompt and get the user's selection.



113
114
115
116
117
118
119
120
121
# File 'lib/select.rb', line 113

def msg_run
  prompt = prompt_value
  return unless prompt

  # Page size was part of the tty-prompt but not used now.
  # per = Gloo::App::Settings.page_size( @engine )
  result = @engine.platform.prompt.select( prompt, options )
  set_result self.key_for_option( result )
end

#optionsObject

Get the list of options for selection.



43
44
45
46
47
48
# File 'lib/select.rb', line 43

def options
  o = find_child OPTIONS
  return [] unless o

  return o.children.map( &:name )
end

#prompt_valueObject

Get the prompt from the child object. Returns nil if there is none.



33
34
35
36
37
38
# File 'lib/select.rb', line 33

def prompt_value
  o = find_child PROMPT
  return nil unless o

  return o.value
end

#set_result(data) ⇒ Object

Set the result of the system call.



67
68
69
70
71
72
# File 'lib/select.rb', line 67

def set_result( data )
  r = find_child RESULT
  return nil unless r

  r.set_value data
end