Sha256: 6ff51dfd4e30436fa7505b30e79f0351f4d021ae9e1ad75c449a9881216a7821

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'shellwords'

module MiteCmd
  class Autocomplete
    include Shellwords
    
    attr_accessor :completion_table
    attr_reader :calling_script
    
    def initialize(calling_script)
      @calling_script = calling_script
    end
    
    def bash_line
      ENV['COMP_LINE'].to_s
    end
  
    def argument_string
      bash_line.sub(/^(.*)#{File.basename calling_script}\s*/, '').close_unmatched_quotes
    end
  
    def partial_argument_string
      bash_line[0..cursor_position+1].sub(/^(.*)#{File.basename calling_script}\s*/, '').close_unmatched_quotes
    end
  
    def current_word
      return nil if argument_string =~ /\s$/ && bash_line.length == cursor_position
      shellwords(partial_argument_string).last
    end
  
    def current_argument_index
      return args.size if argument_string =~ /\s$/ && bash_line.length == cursor_position
      args.index(current_word) || 0
    end
  
    def cursor_position
      ENV['COMP_POINT'].to_i
    end
  
    def args
      shellwords(argument_string)
    end
  
    def suggestions
      completion_table[current_argument_index] ? completion_table[current_argument_index].select {|s| s =~ /^#{current_word}/i} : []
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
Overbryd-mite.cmd-0.1.7 lib/mite_cmd/autocomplete.rb
Overbryd-mite.cmd-0.1.9 lib/mite_cmd/autocomplete.rb
mite.cmd-0.1.12 lib/mite_cmd/autocomplete.rb
mite.cmd-0.1.10 lib/mite_cmd/autocomplete.rb