Class: Redcar::FilterCommand::Autocompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_command/autocompletion.rb

Overview

Command auto-completion manager

Class Method Summary collapse

Class Method Details

.suggest(text) ⇒ Hash

Provides command suggestions for text snippets based on previously run commands

Parameters:

  • text (String)

    the start of a command

Returns:

  • (Hash)

    a hash containing a command beginning with the given text, an input type, an output type, and which characters of the text to select.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/filter_command/autocompletion.rb', line 11

def self.suggest text
  match, completion,selection = nil
  if text.length > 0
    FilterCommand::History.recent_commands.each do |map|
      if map['command'] == text
        match = map unless match
      elsif map['command'][0,text.length] == text
        completion = map unless completion
      end
    end
    if completion and not match
      if !@typed or @typed != text
        match  = completion
        selection = [text.length,match['command'].length]
      end
    end
    @typed = text
    if match
      {
        'command'     => match['command'],
        'input_type'  => match['input_type'],
        'output_type' => match['output_type'],
        'selection'   => selection
      }
    end
  else
    @typed_length = nil
  end
end