Class: Tui::InputField

Inherits:
Object
  • Object
show all
Defined in:
lib/tui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(placeholder:, text:, cursor: nil) ⇒ InputField

Returns a new instance of InputField.



805
806
807
808
809
# File 'lib/tui.rb', line 805

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

#cursorObject

Returns the value of attribute cursor.



802
803
804
# File 'lib/tui.rb', line 802

def cursor
  @cursor
end

#placeholderObject (readonly)

Returns the value of attribute placeholder.



803
804
805
# File 'lib/tui.rb', line 803

def placeholder
  @placeholder
end

#textObject

Returns the value of attribute text.



802
803
804
# File 'lib/tui.rb', line 802

def text
  @text
end

Instance Method Details

#to_sObject



811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/tui.rb', line 811

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