Class: LLM::Repl::Input Private
- Inherits:
-
Object
- Object
- LLM::Repl::Input
- Defined in:
- lib/llm/repl/input.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The LLM::Repl::Input class manages the editable input line shown at the bottom of the REPL.
Constant Summary collapse
- CTRL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{ A: Curses::KEY_CTRL_A, E: Curses::KEY_CTRL_E, F: Curses::KEY_CTRL_F, K: Curses::KEY_CTRL_K, Y: Curses::KEY_CTRL_Y, D: Curses::KEY_CTRL_D }
- UP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::UP
- DOWN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::DOWN
- LEFT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::LEFT
- RIGHT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Curses::Key::RIGHT
- ENTER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[Curses::Key::ENTER, 10, 13]
- BACKSPACE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[Curses::Key::BACKSPACE, 127]
- PASTE_THRESHOLD =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Threshold in seconds. If characters arrive faster than this, we assume the user is pasting multi-line text. Human typing is ~150–300ms per key, so 50ms reliably distinguishes a paste from manual typing.
0.05
Instance Attribute Summary collapse
- #buffer ⇒ String readonly private
- #paste writeonly private
Instance Method Summary collapse
- #paste? ⇒ Boolean private
- #on_char(window, char, now) ⇒ Symbol? private
- #to_s ⇒ String private
- #cursor ⇒ Integer private
- #height ⇒ Integer private
-
#lines ⇒ Array<String>
private
Returns the visible lines of the input buffer, split by newlines.
-
#cursor_pos ⇒ Array(Integer, Integer)
private
Returns the cursor position as [line, column] within the visible viewport.
- #move_start private
- #move_end private
- #move_left private
- #move_right private
- #move_forward private
- #kill private
- #delete private
- #restore private
- #take ⇒ String private
- #initialize(agent, options = {}) ⇒ LLM::Repl::Input constructor private
Constructor Details
#initialize(agent, options = {}) ⇒ LLM::Repl::Input
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/llm/repl/input.rb', line 44 def initialize(agent, = {}) @agent = agent @provider = agent.llm.name @buffer = +"" @cursor = 0 @scroll = 0 @height = .fetch(:height, 3) @last_char_at = nil @paste = false end |
Instance Attribute Details
#buffer ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
34 35 36 |
# File 'lib/llm/repl/input.rb', line 34 def buffer @buffer end |
#paste=(value) (writeonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
39 40 41 |
# File 'lib/llm/repl/input.rb', line 39 def paste=(value) @paste = value end |
Instance Method Details
#paste? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
217 218 219 |
# File 'lib/llm/repl/input.rb', line 217 def paste? @paste end |
#on_char(window, char, now) ⇒ Symbol?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 103 104 105 106 107 108 109 |
# File 'lib/llm/repl/input.rb', line 59 def on_char(window, char, now) is_paste = lambda { @last_char_at and (now - @last_char_at) < PASTE_THRESHOLD } if CTRL[:D] == char delete :ctrl_d elsif CTRL[:A] == char move_start :ctrl_a elsif CTRL[:E] == char move_end :ctrl_e elsif CTRL[:F] == char move_forward :ctrl_f elsif CTRL[:Y] == char restore :ctrl_y elsif CTRL[:K] == char kill :ctrl_k elsif char == LEFT move_left :left elsif char == RIGHT move_right :right elsif BACKSPACE.include?(char) backspace :backspace elsif ENTER.include?(char) if @paste = is_paste.() insert("\n") :char else :submit end elsif char == UP window.scroll_up :up elsif char == DOWN window.scroll_down :down elsif String === char insert(char) :char else nil end ensure @last_char_at = now if char end |
#to_s ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
113 114 115 |
# File 'lib/llm/repl/input.rb', line 113 def to_s "#{@provider}> #{@buffer}" end |
#cursor ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
119 120 121 |
# File 'lib/llm/repl/input.rb', line 119 def cursor prompt.length + @cursor end |
#height ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
125 126 127 |
# File 'lib/llm/repl/input.rb', line 125 def height @height end |
#lines ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the visible lines of the input buffer, split by newlines. The viewport follows the cursor so the cursor line is always visible.
134 135 136 137 138 |
# File 'lib/llm/repl/input.rb', line 134 def lines scroll! chunks = to_s.split("\n", -1) chunks[@scroll, height] || [] end |
#cursor_pos ⇒ Array(Integer, Integer)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the cursor position as [line, column] within the visible viewport.
144 145 146 147 148 149 150 |
# File 'lib/llm/repl/input.rb', line 144 def cursor_pos scroll! before = to_s[0...cursor] line = before.count("\n") col = cursor - (before.rindex("\n") || -1) - 1 [line - @scroll, col] end |
#move_start
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
154 155 156 |
# File 'lib/llm/repl/input.rb', line 154 def move_start @cursor = 0 end |
#move_end
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
160 161 162 |
# File 'lib/llm/repl/input.rb', line 160 def move_end @cursor = [0, @buffer.size].max end |
#move_left
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
166 167 168 |
# File 'lib/llm/repl/input.rb', line 166 def move_left @cursor = [@cursor - 1, 0].max end |
#move_right
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
172 173 174 |
# File 'lib/llm/repl/input.rb', line 172 def move_right @cursor = [@cursor + 1, @buffer.size].min end |
#move_forward
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
178 179 180 |
# File 'lib/llm/repl/input.rb', line 178 def move_forward @cursor = [0, @cursor + 1].max end |
#kill
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
184 185 186 187 188 |
# File 'lib/llm/repl/input.rb', line 184 def kill @copy = @buffer.slice(@cursor, @buffer.size) @buffer[@cursor, @buffer.size] = "" @cursor = @buffer.size end |
#delete
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
192 193 194 195 |
# File 'lib/llm/repl/input.rb', line 192 def delete @buffer[@cursor] = "" @cursor = [0, @cursor].max end |
#restore
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
199 200 201 202 203 |
# File 'lib/llm/repl/input.rb', line 199 def restore return unless @copy @buffer.insert(@cursor, @copy) @cursor += @copy.size end |
#take ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
207 208 209 210 211 212 213 |
# File 'lib/llm/repl/input.rb', line 207 def take @buffer.dup.tap do @buffer.clear @cursor = 0 @scroll = 0 end end |