Class: Tui::InputField
- Inherits:
-
Object
- Object
- Tui::InputField
- Defined in:
- lib/tui.rb
Instance Attribute Summary collapse
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#placeholder ⇒ Object
readonly
Returns the value of attribute placeholder.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(placeholder:, text:, cursor: nil) ⇒ InputField
constructor
A new instance of InputField.
- #to_s ⇒ Object
Constructor Details
#initialize(placeholder:, text:, cursor: nil) ⇒ InputField
Returns a new instance of InputField.
864 865 866 867 868 |
# File 'lib/tui.rb', line 864 def initialize(placeholder:, text:, cursor: nil) @placeholder = placeholder @text = text.to_s.dup @cursor = cursor.nil? ? @text.length : [[cursor, 0].max, @text.length].min end |
Instance Attribute Details
#cursor ⇒ Object
Returns the value of attribute cursor.
861 862 863 |
# File 'lib/tui.rb', line 861 def cursor @cursor end |
#placeholder ⇒ Object (readonly)
Returns the value of attribute placeholder.
862 863 864 |
# File 'lib/tui.rb', line 862 def placeholder @placeholder end |
#text ⇒ Object
Returns the value of attribute text.
861 862 863 |
# File 'lib/tui.rb', line 861 def text @text end |
Instance Method Details
#to_s ⇒ Object
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
# File 'lib/tui.rb', line 870 def to_s return render_placeholder if text.empty? before = text[0...cursor] cursor_char = text[cursor] || ' ' after = cursor < text.length ? text[(cursor + 1)..] : "" buf = String.new buf << before buf << Palette::INPUT_CURSOR_ON if Tui.colors_enabled? buf << cursor_char buf << Palette::INPUT_CURSOR_OFF if Tui.colors_enabled? buf << after buf end |