Class: Potty::Mouth::Prompt::Choose

Inherits:
Base
  • Object
show all
Defined in:
lib/potty/mouth.rb

Instance Attribute Summary collapse

Attributes inherited from View

#app, #widgets

Instance Method Summary collapse

Methods inherited from Base

#spacing

Methods inherited from View

#activate, #deactivate, #layout_widgets, #on_activate, #on_deactivate, #render, #spacing, #tick

Constructor Details

#initialize(app, prompt:, options:) ⇒ Choose

Returns a new instance of Choose.



186
187
188
189
190
191
# File 'lib/potty/mouth.rb', line 186

def initialize(app, prompt:, options:)
  @prompt = prompt
  @options = options
  @result = nil
  super(app)
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



184
185
186
# File 'lib/potty/mouth.rb', line 184

def result
  @result
end

Instance Method Details

#build_layoutObject



193
194
195
196
197
198
# File 'lib/potty/mouth.rb', line 193

def build_layout
  opts = @options.map { |o| o.is_a?(Hash) ? o : { value: o, label: o.to_s } }
  @radio = Potty::Widgets::RadioGroup.new(app, options: opts)
  @widgets = [Potty::Widgets::Label.new(app, text: @prompt, color: :info), @radio]
  @radio.focus
end

#handle_escapeObject



209
210
211
212
213
# File 'lib/potty/mouth.rb', line 209

def handle_escape
  @result = nil
  app.quit
  true
end

#handle_key(ch) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/potty/mouth.rb', line 200

def handle_key(ch)
  if Potty::Keys.enter?(ch)
    @result = @radio.cursor_value
    app.quit
    return true
  end
  super # arrows move the radio cursor
end