Class: Legion::TTY::Components::InputBar

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/input_bar.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bufferObject (readonly)

Returns the value of attribute buffer.



9
10
11
# File 'lib/legion/tty/components/input_bar.rb', line 9

def buffer
  @buffer
end

#completionsObject (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_bufferObject



100
101
102
103
# File 'lib/legion/tty/components/input_bar.rb', line 100

def clear_buffer
  @buffer = +''
  @cursor_pos = 0
end

#clear_thinkingObject



127
128
129
# File 'lib/legion/tty/components/input_bar.rb', line 127

def clear_thinking
  @thinking = false
end

#complete(partial) ⇒ Object



117
118
119
120
121
# File 'lib/legion/tty/components/input_bar.rb', line 117

def complete(partial)
  return [] if partial.nil? || partial.empty?

  @completions.select { |c| c.start_with?(partial) }.sort
end

#current_lineObject



96
97
98
# File 'lib/legion/tty/components/input_bar.rb', line 96

def current_line
  @buffer.dup
end

#cursor_columnObject



92
93
94
# File 'lib/legion/tty/components/input_bar.rb', line 92

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
78
79
# File 'lib/legion/tty/components/input_bar.rb', line 34

def handle_key(key)
  return handle_paste(key[:paste]) if key.is_a?(Hash) && key.key?(:paste)

  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

#historyObject



105
106
107
# File 'lib/legion/tty/components/input_bar.rb', line 105

def history
  @history_entries.dup
end

#prompt_plain_lengthObject



29
30
31
# File 'lib/legion/tty/components/input_bar.rb', line 29

def prompt_plain_length
  @name.length + 3
end

#prompt_stringObject



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_lineObject

Backward-compatible blocking read for onboarding/tests



110
111
112
113
114
115
# File 'lib/legion/tty/components/input_bar.rb', line 110

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



82
83
84
85
86
87
88
89
90
# File 'lib/legion/tty/components/input_bar.rb', line 82

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_thinkingObject



123
124
125
# File 'lib/legion/tty/components/input_bar.rb', line 123

def show_thinking
  @thinking = true
end

#thinking?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/legion/tty/components/input_bar.rb', line 131

def thinking?
  @thinking
end