Class: OllamaAgent::TuiSlashReader

Inherits:
TTY::Reader
  • Object
show all
Defined in:
lib/ollama_agent/tui_slash_reader.rb

Overview

Extends TTY::Reader so Tab completes lines that start with / against a fixed candidate list.

Line editing and history match tty-reader read_line; only the Tab code path differs. Implementation synced from tty-reader 0.9.0 (lib/tty/reader.rb). rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Style/StringConcatenation, Metrics/BlockNesting

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(completion_candidates:) ⇒ TuiSlashReader

Returns a new instance of TuiSlashReader.



32
33
34
35
36
# File 'lib/ollama_agent/tui_slash_reader.rb', line 32

def initialize(completion_candidates:, **)
  super(**)
  @completion_candidates = Array(completion_candidates).uniq.sort
  @menu_lines_printed = 0
end

Instance Attribute Details

#command_paletteObject

Returns the value of attribute command_palette.



30
31
32
# File 'lib/ollama_agent/tui_slash_reader.rb', line 30

def command_palette
  @command_palette
end

#completion_candidatesObject

Returns the value of attribute completion_candidates.



30
31
32
# File 'lib/ollama_agent/tui_slash_reader.rb', line 30

def completion_candidates
  @completion_candidates
end

Instance Method Details

#read_line(prompt = "", value: "", echo: true, raw: true, nonblock: false) ⇒ Object



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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ollama_agent/tui_slash_reader.rb', line 38

def read_line(prompt = "", value: "", echo: true, raw: true, nonblock: false)
  line = TTY::Reader::Line.new(value, prompt: prompt)
  screen_width = TTY::Screen.width
  buffer = ""

  output.print(line)

  while (codes = get_codes(echo: echo, raw: raw, nonblock: nonblock)) &&
        (code = codes[0])
    char = codes.pack("U*")

    if EXIT_KEYS.include?(console.keys[char])
      trigger_key_event(char, line: line.to_s)
      break
    end

    erase_completion_menu if raw && echo && completion_menu_visible?
    clear_display(line, screen_width) if raw && echo

    if console.keys[char] == :backspace || code == BACKSPACE
      unless line.start?
        line.left
        line.delete
      end
    elsif console.keys[char] == :delete || code == DELETE
      line.delete
    elsif console.keys[char].to_s =~ /ctrl_/
      # skip
    elsif console.keys[char] == :escape
      close_completion_menu if command_palette_active_for?(line.text)
    elsif console.keys[char] == :up
      if completion_menu_visible?
        apply_selected_suggestion!(line, @command_palette.menu.previous)
        buffer = line.text
      elsif history_previous?
        line.replace(mutable_copy(history_previous))
      end
    elsif console.keys[char] == :down
      if command_palette_active_for?(line.text)
        show_completion_menu(line.text) unless completion_menu_visible?
        apply_selected_suggestion!(line, @command_palette.menu.next)
        buffer = line.text
      elsif track_history?
        line.replace(mutable_copy(history_next? ? history_next : buffer))
      end
    elsif console.keys[char] == :left
      line.left
    elsif console.keys[char] == :right
      line.right
    elsif console.keys[char] == :home
      line.move_to_start
    elsif console.keys[char] == :end
      line.move_to_end
    elsif console.keys[char] == :tab
      buffer = apply_slash_tab!(line, char)
    else
      if raw && code == CARRIAGE_RETURN
        char = "\n"
        line.move_to_end
      end
      close_completion_menu if completion_menu_visible? && code != CARRIAGE_RETURN && code != NEWLINE
      line.insert(char)
      buffer = line.text
    end

    if (console.keys[char] == :backspace || code == BACKSPACE) && echo
      if raw
        output.print("\e[1X") unless line.start?
      else
        output.print(" " + (line.start? ? "" : "\b"))
      end
    end

    trigger_key_event(char, line: line.to_s)

    if raw && echo
      output.print(line.to_s)
      ghost = current_ghost_for(line)
      if ghost && line.end?
        output.print("\e[2m#{ghost.suffix}\e[0m")
        output.print(cursor.backward(ghost.suffix.length))
      end
      if char == "\n"
        line.move_to_start
      elsif !line.end?
        output.print(cursor.backward(line.text_size - line.cursor))
      end
      draw_menu_items if completion_menu_visible?
    end

    next unless [CARRIAGE_RETURN, NEWLINE].include?(code)

    buffer = ""
    output.puts unless echo
    break
  end

  add_to_history(line.text.rstrip) if track_history? && echo

  line.text
end