Class: RubyTUI::Widgets::InputState

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytui/widgets/input_field.rb

Overview

InputState holds the mutable state for the InputField widget.

cursor is a character index into text. scroll_offset is the index of the first visible character and is updated by RubyTUI::Widgets::InputField#render.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: "", cursor: 0) ⇒ InputState

Returns a new instance of InputState.

Parameters:

  • text (String) (defaults to: "") —

    initial text; a mutable copy is stored (default: "")

  • cursor (Integer) (defaults to: 0) —

    initial cursor character index, not clamped to the text length (default: 0)



17
18
19
20
21
# File 'lib/rubytui/widgets/input_field.rb', line 17

def initialize(text: "", cursor: 0)
  @text = String.new(text)  # ensure mutable copy
  @cursor = cursor
  @scroll_offset = 0
end

Instance Attribute Details

#cursor ⇒ Object

Returns the value of attribute cursor.



12
13
14
# File 'lib/rubytui/widgets/input_field.rb', line 12

def cursor
  @cursor
end

#scroll_offset ⇒ Object

Returns the value of attribute scroll_offset.



12
13
14
# File 'lib/rubytui/widgets/input_field.rb', line 12

def scroll_offset
  @scroll_offset
end

#text ⇒ Object

Returns the value of attribute text.



12
13
14
# File 'lib/rubytui/widgets/input_field.rb', line 12

def text
  @text
end

Instance Method Details

#backspace ⇒ void

This method returns an undefined value.

Delete character before cursor (backspace)

Does nothing when the cursor is at the start.



37
38
39
40
41
42
# File 'lib/rubytui/widgets/input_field.rb', line 37

def backspace
  return if @cursor == 0

  @text.slice!(@cursor - 1)
  @cursor -= 1
end

#delete ⇒ void

This method returns an undefined value.

Delete character at cursor (delete key)

Does nothing when the cursor is at the end.



49
50
51
52
53
# File 'lib/rubytui/widgets/input_field.rb', line 49

def delete
  return if @cursor >= @text.length

  @text.slice!(@cursor)
end

#delete_word_back ⇒ void

This method returns an undefined value.

Delete the word before the cursor

Deletes any spaces directly before the cursor and the run of non-space characters before them. Only the space character counts as a separator. Does nothing when the cursor is at the start.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rubytui/widgets/input_field.rb', line 109

def delete_word_back
  return if @cursor == 0

  # Find start of previous word
  pos = @cursor - 1
  pos -= 1 while pos > 0 && @text[pos] == " "
  pos -= 1 while pos > 0 && @text[pos] != " "
  pos += 1 if pos > 0

  @text.slice!(pos...@cursor)
  @cursor = pos
end

#end_pos ⇒ void

This method returns an undefined value.

Move cursor to end



79
80
81
# File 'lib/rubytui/widgets/input_field.rb', line 79

def end_pos
  @cursor = @text.length
end

#handle_key(event) ⇒ Boolean

Handle a key event, returns true if the event was consumed

Character keys without Ctrl insert event.char. Backspace, Delete, Left, Right, Home and End edit or move. Ctrl-A / Ctrl-E move to start / end, Ctrl-K / Ctrl-U delete to end / start, Ctrl-W deletes the previous word. Other Ctrl combinations, other keys and non-KeyEvent events are not consumed.

Parameters:

  • event (Event) —

    the input event

Returns:

  • (Boolean) —

    true if the event was consumed



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rubytui/widgets/input_field.rb', line 131

def handle_key(event)
  return false unless event.is_a?(KeyEvent)

  case event.code
  when Input::Key::CHAR
    if event.ctrl?
      case event.char
      when "a" then home; true
      when "e" then end_pos; true
      when "k" then kill_to_end; true
      when "u" then kill_to_start; true
      when "w" then delete_word_back; true
      else false
      end
    else
      insert(event.char) if event.char
      true
    end
  when Input::Key::BACKSPACE then backspace; true
  when Input::Key::DELETE    then delete; true
  when Input::Key::LEFT      then move_left; true
  when Input::Key::RIGHT     then move_right; true
  when Input::Key::HOME      then home; true
  when Input::Key::END_KEY   then end_pos; true
  else
    false
  end
end

#home ⇒ void

This method returns an undefined value.

Move cursor to start



72
73
74
# File 'lib/rubytui/widgets/input_field.rb', line 72

def home
  @cursor = 0
end

#insert(char) ⇒ void

This method returns an undefined value.

Insert a character at the cursor position

Parameters:

  • char (String) —

    character (or string) to insert; the cursor advances by its length



27
28
29
30
# File 'lib/rubytui/widgets/input_field.rb', line 27

def insert(char)
  @text.insert(@cursor, char)
  @cursor += char.length
end

#kill_to_end ⇒ void

This method returns an undefined value.

Delete from cursor to end of line

The cursor does not move.



88
89
90
# File 'lib/rubytui/widgets/input_field.rb', line 88

def kill_to_end
  @text.slice!(@cursor..)
end

#kill_to_start ⇒ void

This method returns an undefined value.

Delete from start to cursor

The cursor moves to the start.



97
98
99
100
# File 'lib/rubytui/widgets/input_field.rb', line 97

def kill_to_start
  @text.slice!(0...@cursor)
  @cursor = 0
end

#move_left ⇒ void

This method returns an undefined value.

Move cursor left



58
59
60
# File 'lib/rubytui/widgets/input_field.rb', line 58

def move_left
  @cursor = [@cursor - 1, 0].max
end

#move_right ⇒ void

This method returns an undefined value.

Move cursor right



65
66
67
# File 'lib/rubytui/widgets/input_field.rb', line 65

def move_right
  @cursor = [@cursor + 1, @text.length].min
end