Sha256: 78fc31e0d78e1d77550e9961c2480279e0fb073ce8bbe0e54572c6369cd1825e

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'shellwords'

module MightyMite
  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}/} : []
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
Overbryd-mighty-mite-0.1.0 lib/mighty_mite/autocomplete.rb
Overbryd-mighty-mite-0.1.1 lib/mighty_mite/autocomplete.rb
Overbryd-mighty-mite-0.1.2 lib/mighty_mite/autocomplete.rb
Overbryd-mighty-mite-0.1.3 lib/mighty_mite/autocomplete.rb