Class: Potty::Mouth::Prompt::Ask

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:, default: '') ⇒ Ask

Returns a new instance of Ask.



115
116
117
118
119
120
# File 'lib/potty/mouth.rb', line 115

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

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



113
114
115
# File 'lib/potty/mouth.rb', line 113

def result
  @result
end

Instance Method Details

#build_layoutObject



122
123
124
125
126
# File 'lib/potty/mouth.rb', line 122

def build_layout
  @field = Potty::Widgets::TextInput.new(app, text: @default)
  @widgets = [Potty::Widgets::Label.new(app, text: @prompt, color: :info), @field]
  @field.focus
end

#handle_escapeObject



139
140
141
142
143
# File 'lib/potty/mouth.rb', line 139

def handle_escape
  @result = nil
  app.quit
  true
end

#handle_key(ch) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/potty/mouth.rb', line 128

def handle_key(ch)
  # Intercept Enter before super (which would otherwise advance focus)
  # — a single-field prompt submits on Enter.
  if Potty::Keys.enter?(ch)
    @result = @field.text
    app.quit
    return true
  end
  super
end