Sha256: 4e1cecef2cf2e7ef304af9fecd17656dadd799df138ab074aa7c714c9844bc6d

Contents?: true

Size: 1.18 KB

Versions: 3

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
Overbryd-mite.cmd-0.1.3 lib/mite_cmd/autocomplete.rb
Overbryd-mite.cmd-0.1.4 lib/mite_cmd/autocomplete.rb
Overbryd-mite.cmd-0.1.5 lib/mite_cmd/autocomplete.rb