Class: Luck::TextBox
Direct Known Subclasses
Instance Attribute Summary collapse
-
#handler ⇒ Object
Returns the value of attribute handler.
-
#index ⇒ Object
Returns the value of attribute index.
-
#label ⇒ Object
Returns the value of attribute label.
-
#mask ⇒ Object
Returns the value of attribute mask.
-
#multiline ⇒ Object
Returns the value of attribute multiline.
Attributes inherited from Label
Attributes inherited from Control
#display, #pane, #x1, #x2, #y1, #y2
Instance Method Summary collapse
- #handle_char(char) ⇒ Object
-
#initialize(*args) ⇒ TextBox
constructor
A new instance of TextBox.
- #on_submit(&blck) ⇒ Object
- #redraw ⇒ Object
- #text ⇒ Object
- #value ⇒ Object
- #value=(val) ⇒ Object
Methods inherited from Label
Methods inherited from Control
Constructor Details
#initialize(*args) ⇒ TextBox
Returns a new instance of TextBox.
5 6 7 8 9 10 11 |
# File 'lib/luck/textbox.rb', line 5 def initialize *args @label = '' @text = 'Input box' @index = 0 super end |
Instance Attribute Details
#handler ⇒ Object
Returns the value of attribute handler.
3 4 5 |
# File 'lib/luck/textbox.rb', line 3 def handler @handler end |
#index ⇒ Object
Returns the value of attribute index.
3 4 5 |
# File 'lib/luck/textbox.rb', line 3 def index @index end |
#label ⇒ Object
Returns the value of attribute label.
3 4 5 |
# File 'lib/luck/textbox.rb', line 3 def label @label end |
#mask ⇒ Object
Returns the value of attribute mask.
3 4 5 |
# File 'lib/luck/textbox.rb', line 3 def mask @mask end |
#multiline ⇒ Object
Returns the value of attribute multiline.
3 4 5 |
# File 'lib/luck/textbox.rb', line 3 def multiline @multiline end |
Instance Method Details
#handle_char(char) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/luck/textbox.rb', line 49 def handle_char char if char == "\n" && !@multiline handler.call self, @text if handler self.value = '' elsif char == :backspace if @index > 0 @index -= 1 @text.slice! @index, 1 end elsif char == :delete if @index < @text.size @text.slice! @index, 1 end elsif char == :left @index -= 1 if @index > 0 elsif char == :right @index += 1 if @index < @text.size elsif char == :home @index = 0 elsif char == :end @index = @text.size elsif char.is_a? String if text.size < width @text.insert @index, char @index += 1 end end redraw end |
#on_submit(&blck) ⇒ Object
27 28 29 |
# File 'lib/luck/textbox.rb', line 27 def on_submit &blck @handler = blck end |
#redraw ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/luck/textbox.rb', line 13 def redraw super case @alignment when :center text.center width when :right text.rjust width else @display.driver.set_cursor y1, x1 + text.size - @text.size + @index if @display.active_control == self end @display.driver.cursor = true if @display.active_control == self end |
#text ⇒ Object
39 40 41 42 43 |
# File 'lib/luck/textbox.rb', line 39 def text text = @mask ? (@mask * value.size) : value text = "#{label}: #{text}" unless label.empty? text end |
#value ⇒ Object
35 36 37 |
# File 'lib/luck/textbox.rb', line 35 def value @text end |
#value=(val) ⇒ Object
31 32 33 34 |
# File 'lib/luck/textbox.rb', line 31 def value= val @text = val @index = @text.size if @index > @text.size end |