Class: Legion::TTY::Components::InputBar
- Inherits:
-
Object
- Object
- Legion::TTY::Components::InputBar
- Defined in:
- lib/legion/tty/components/input_bar.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#completions ⇒ Object
readonly
Returns the value of attribute completions.
Instance Method Summary collapse
- #clear_buffer ⇒ Object
- #clear_thinking ⇒ Object
- #complete(partial) ⇒ Object
- #current_line ⇒ Object
- #cursor_column ⇒ Object
-
#handle_key(key) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #history ⇒ Object
-
#initialize(name: 'User', reader: nil, completions: []) ⇒ InputBar
constructor
A new instance of InputBar.
- #prompt_plain_length ⇒ Object
- #prompt_string ⇒ Object
-
#read_line ⇒ Object
Backward-compatible blocking read for onboarding/tests.
-
#render_line(width:) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #show_thinking ⇒ Object
- #thinking? ⇒ Boolean
Constructor Details
#initialize(name: 'User', reader: nil, completions: []) ⇒ InputBar
Returns a new instance of InputBar.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/legion/tty/components/input_bar.rb', line 11 def initialize(name: 'User', reader: nil, completions: []) @name = name @completions = completions @buffer = +'' @cursor_pos = 0 @history_entries = [] @history_index = nil @saved_buffer = nil @tab_matches = [] @tab_index = 0 @legacy_reader = reader @thinking = false end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
9 10 11 |
# File 'lib/legion/tty/components/input_bar.rb', line 9 def buffer @buffer end |
#completions ⇒ Object (readonly)
Returns the value of attribute completions.
9 10 11 |
# File 'lib/legion/tty/components/input_bar.rb', line 9 def completions @completions end |
Instance Method Details
#clear_buffer ⇒ Object
88 89 90 91 |
# File 'lib/legion/tty/components/input_bar.rb', line 88 def clear_buffer @buffer = +'' @cursor_pos = 0 end |
#clear_thinking ⇒ Object
115 116 117 |
# File 'lib/legion/tty/components/input_bar.rb', line 115 def clear_thinking @thinking = false end |
#complete(partial) ⇒ Object
105 106 107 108 109 |
# File 'lib/legion/tty/components/input_bar.rb', line 105 def complete(partial) return [] if partial.nil? || partial.empty? @completions.select { |c| c.start_with?(partial) }.sort end |
#current_line ⇒ Object
84 85 86 |
# File 'lib/legion/tty/components/input_bar.rb', line 84 def current_line @buffer.dup end |
#cursor_column ⇒ Object
80 81 82 |
# File 'lib/legion/tty/components/input_bar.rb', line 80 def cursor_column prompt_plain_length + @cursor_pos end |
#handle_key(key) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
34 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/legion/tty/components/input_bar.rb', line 34 def handle_key(key) case key when :enter submit_line when :backspace handle_backspace when :tab handle_tab_key when :up history_prev when :down history_next when :right @cursor_pos = [@cursor_pos + 1, @buffer.length].min :handled when :left @cursor_pos = [@cursor_pos - 1, 0].max :handled when :home, :ctrl_a @cursor_pos = 0 :handled when :end, :ctrl_e @cursor_pos = @buffer.length :handled when :ctrl_u @buffer = +'' @cursor_pos = 0 :handled when String insert_char(key) else :pass end end |
#history ⇒ Object
93 94 95 |
# File 'lib/legion/tty/components/input_bar.rb', line 93 def history @history_entries.dup end |
#prompt_plain_length ⇒ Object
29 30 31 |
# File 'lib/legion/tty/components/input_bar.rb', line 29 def prompt_plain_length @name.length + 3 end |
#prompt_string ⇒ Object
25 26 27 |
# File 'lib/legion/tty/components/input_bar.rb', line 25 def prompt_string "#{Theme.c(:accent, @name)} #{Theme.c(:primary, '>')} " end |
#read_line ⇒ Object
Backward-compatible blocking read for onboarding/tests
98 99 100 101 102 103 |
# File 'lib/legion/tty/components/input_bar.rb', line 98 def read_line reader = @legacy_reader || build_legacy_reader reader&.read_line(prompt_string) rescue Interrupt nil end |
#render_line(width:) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
70 71 72 73 74 75 76 77 78 |
# File 'lib/legion/tty/components/input_bar.rb', line 70 def render_line(width:) avail = [width - prompt_plain_length, 1].max display = if @buffer.length > avail @buffer[(@buffer.length - avail)..] else @buffer end "#{prompt_string}#{display}" end |
#show_thinking ⇒ Object
111 112 113 |
# File 'lib/legion/tty/components/input_bar.rb', line 111 def show_thinking @thinking = true end |
#thinking? ⇒ Boolean
119 120 121 |
# File 'lib/legion/tty/components/input_bar.rb', line 119 def thinking? @thinking end |