Class: RawLine::Completer

Inherits:
Object
  • Object
show all
Defined in:
lib/rawline/completer.rb

Instance Method Summary collapse

Constructor Details

#initialize(char:, line:, completion:, completion_found:, completion_not_found:, completion_selected:, done:, keys:) ⇒ Completer

Returns a new instance of Completer.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rawline/completer.rb', line 4

def initialize(char:, line:, completion:, completion_found:, completion_not_found:, completion_selected:, done:, keys:)
  @completion_char = char
  @line = line
  @completion_proc = completion
  @completion_found_proc = completion_found
  @completion_not_found_proc = completion_not_found
  @completion_selected_proc = completion_selected
  @done_proc = done
  @keys = keys

  @completion_matches = HistoryBuffer.new(0) do |h|
    h.duplicates = false
    h.cycle = true
  end
  @completion_matches.empty

  @first_time = true
  @word_start = @line.word[:start]
end

Instance Method Details

#read_bytes(bytes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rawline/completer.rb', line 24

def read_bytes(bytes)
  return unless bytes.any?

  # this is to prevent a series of bytes from coming in at one time
  # E.g. holding down the tab key or arrow keys
  bytes = bytes.uniq

  if @first_time
    matches = fetch_completions
    resize(matches)

    if matches.length == 1
      handle_one_match
    elsif matches.length > 1
      handle_more_than_one_match
    else
      handle_no_completions
    end

    @first_time = false
  elsif bytes.map(&:ord) == @keys[:left_arrow]
    select_previous
  elsif bytes.map(&:ord) == @keys[:right_arrow]
    select_next
  elsif bytes.map(&:ord) == @completion_char
    select_next
  else
    Treefell['editor'].puts "completer, done with leftover bytes: #{bytes.inspect}"
    @done_proc.call(bytes)
  end
end