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
98 99 100 101 |
# File 'lib/legion/tty/components/input_bar.rb', line 98 def clear_buffer @buffer = +'' @cursor_pos = 0 end |
#clear_thinking ⇒ Object
125 126 127 |
# File 'lib/legion/tty/components/input_bar.rb', line 125 def clear_thinking @thinking = false end |
#complete(partial) ⇒ Object
115 116 117 118 119 |
# File 'lib/legion/tty/components/input_bar.rb', line 115 def complete(partial) return [] if partial.nil? || partial.empty? @completions.select { |c| c.start_with?(partial) }.sort end |
#current_line ⇒ Object
94 95 96 |
# File 'lib/legion/tty/components/input_bar.rb', line 94 def current_line @buffer.dup end |
#cursor_column ⇒ Object
90 91 92 |
# File 'lib/legion/tty/components/input_bar.rb', line 90 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 68 69 70 71 72 73 74 75 76 77 |
# 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 return :pass if @buffer.empty? @cursor_pos = 0 :handled when :ctrl_a @cursor_pos = 0 :handled when :end return :pass if @buffer.empty? @cursor_pos = @buffer.length :handled when :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
103 104 105 |
# File 'lib/legion/tty/components/input_bar.rb', line 103 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
108 109 110 111 112 113 |
# File 'lib/legion/tty/components/input_bar.rb', line 108 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
80 81 82 83 84 85 86 87 88 |
# File 'lib/legion/tty/components/input_bar.rb', line 80 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
121 122 123 |
# File 'lib/legion/tty/components/input_bar.rb', line 121 def show_thinking @thinking = true end |
#thinking? ⇒ Boolean
129 130 131 |
# File 'lib/legion/tty/components/input_bar.rb', line 129 def thinking? @thinking end |