Class: Potty::Widgets::InputItem
- Defined in:
- lib/potty/widgets/list_item.rb
Overview
Text input item - allows inline text editing
Instance Attribute Summary collapse
-
#input_value ⇒ Object
Returns the value of attribute input_value.
Attributes inherited from ListItem
Instance Method Summary collapse
- #display_text ⇒ Object
- #handle_key(ch) ⇒ Object
-
#initialize(label, default: "", &on_submit) ⇒ InputItem
constructor
A new instance of InputItem.
Methods inherited from ListItem
#activate, #disabled?, #render_custom
Constructor Details
#initialize(label, default: "", &on_submit) ⇒ InputItem
Returns a new instance of InputItem.
66 67 68 69 70 71 |
# File 'lib/potty/widgets/list_item.rb', line 66 def initialize(label, default: "", &on_submit) super(label) @input_value = default @on_submit = on_submit @cursor_pos = @input_value.length end |
Instance Attribute Details
#input_value ⇒ Object
Returns the value of attribute input_value.
64 65 66 |
# File 'lib/potty/widgets/list_item.rb', line 64 def input_value @input_value end |
Instance Method Details
#display_text ⇒ Object
73 74 75 76 |
# File 'lib/potty/widgets/list_item.rb', line 73 def display_text cursor = "_" "#{@text}: #{@input_value}#{cursor}" end |
#handle_key(ch) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/potty/widgets/list_item.rb', line 78 def handle_key(ch) case ch when *Keys::ENTERS @on_submit&.call(@input_value) true when *Keys::BACKSPACES if @cursor_pos > 0 @input_value[@cursor_pos - 1] = '' @cursor_pos -= 1 end true when Keys::LEFT @cursor_pos = [@cursor_pos - 1, 0].max true when Keys::RIGHT @cursor_pos = [@cursor_pos + 1, @input_value.length].min true when Keys::SPACE..(Keys::DEL_ASCII - 1) # Printable ASCII @input_value.insert(@cursor_pos, ch.chr) @cursor_pos += 1 true else false end end |