Sha256: 32ded9c49e46d9b15b35e97c8497acb7768b4753546156e269a3ecfdca573255

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# derived from http://github.com/ryanb/dotfiles/tree/master/bash/completion_scripts/project_completion
#This class handles completions given a path key and the text already typed.
class Lightning
  class Completion
    def self.complete(text_to_complete, bolt_key)
      new(text_to_complete, bolt_key).matches
    end
      
    def initialize(text_typed, bolt_key)
      @text_typed = text_typed
      @bolt_key = bolt_key
    end
  
    def matches
      if Lightning.config[:complete_regex]
        begin 
          possible_completions.grep(/#{blob_to_regex(typed)}/)
        rescue RegexpError
          ['Error: Invalid regular expression']
        end
      else
        possible_completions.select do |e|
          e[0, typed.length] == typed
        end
      end
    end
    
    #just converts * to .*  to make a glob-like regex
    def blob_to_regex(string)
      string.gsub(/^\*|([^\.])\*/) {|e| $1 ? $1 + ".*" : ".*" }
    end
  
    def typed
      # @text_typed[/\s(.+?)$/, 1] || ''
      text = @text_typed[/^(\S+)\s+(#{Lightning::TEST_FLAG})?\s*(.+?)$/, 3] || ''
      text.strip
    end
  
    def possible_completions
      Lightning.bolts[@bolt_key].completions
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cldwalker-lightning-0.2.0 lib/lightning/completion.rb