Class: LLM::Repl::Input Private

Inherits:
Object
  • Object
show all
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_A =

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.

1
CTRL_E =

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.

5
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]
EOF =

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.

[4]

Instance Method Summary collapse

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.

Parameters:



22
23
24
25
26
27
28
29
# File 'lib/llm/repl/input.rb', line 22

def initialize(agent, options = {})
  @agent = agent
  @provider = agent.llm.name
  @buffer = +""
  @cursor = 0
  @scroll = 0
  @height = options.fetch(:height, 3)
end

Instance Method Details

#cursorInteger

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:

  • (Integer)


77
78
79
# File 'lib/llm/repl/input.rb', line 77

def cursor
  prompt.length + @cursor
end

#cursor_pos(cols) ⇒ 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.

Parameters:

  • cols (Integer)

Returns:

  • (Array(Integer, Integer))


107
108
109
110
# File 'lib/llm/repl/input.rb', line 107

def cursor_pos(cols)
  sync_scroll(cols)
  [(cursor / cols) - @scroll, cursor % cols]
end

#heightInteger

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:

  • (Integer)


83
84
85
# File 'lib/llm/repl/input.rb', line 83

def height
  @height
end

#lines(cols) ⇒ 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, wrapped at the given column width. The viewport follows the cursor so the cursor line is always visible.

Parameters:

  • cols (Integer)

Returns:

  • (Array<String>)


94
95
96
97
98
99
100
# File 'lib/llm/repl/input.rb', line 94

def lines(cols)
  sync_scroll(cols)
  text = to_s
  chunks = text.chars.each_slice(cols).map(&:join)
  chunks = [""] if chunks.empty?
  chunks[@scroll, height] || []
end

#move_endvoid

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.



120
121
122
# File 'lib/llm/repl/input.rb', line 120

def move_end
  @cursor = [0, @buffer.size].max
end

#move_leftvoid

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.



126
127
128
# File 'lib/llm/repl/input.rb', line 126

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

#move_rightvoid

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.



132
133
134
# File 'lib/llm/repl/input.rb', line 132

def move_right
  @cursor = [@cursor + 1, @buffer.size].min
end

#move_startvoid

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.



114
115
116
# File 'lib/llm/repl/input.rb', line 114

def move_start
  @cursor = 0
end

#on_char(window, char) ⇒ 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.

Parameters:

Returns:

  • (Symbol, nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/llm/repl/input.rb', line 35

def on_char(window, char)
  if EOF.include?(char)
    :exit
  elsif BACKSPACE.include?(char)
    backspace
    :backspace
  elsif ENTER.include?(char)
    :submit
  elsif char == UP
    window.scroll_up
    :up
  elsif char == DOWN
    window.scroll_down
    :down
  elsif char == CTRL_A
    move_start
    :ctrl_a
  elsif char == CTRL_E
    move_end
    :ctrl_e
  elsif char == LEFT
    move_left
    :left
  elsif char == RIGHT
    move_right
    :right
  elsif String === char
    insert(char)
    :char
  else
    nil
  end
end

#takeString

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:

  • (String)


138
139
140
141
142
143
144
# File 'lib/llm/repl/input.rb', line 138

def take
  @buffer.dup.tap do
    @buffer.clear
    @cursor = 0
    @scroll = 0
  end
end

#to_sString

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:

  • (String)


71
72
73
# File 'lib/llm/repl/input.rb', line 71

def to_s
  "#{@provider}> #{@buffer}"
end