Module: RVC::Completion

Defined in:
lib/rvc/completion.rb

Constant Summary collapse

Cache =
TTLCache.new 10
Completor =
lambda do |word|
  return unless word
  line = Readline.line_buffer if Readline.respond_to? :line_buffer
  append_char, candidates = RVC::Completion.complete word, line
  Readline.completion_append_character = append_char
  candidates
end

Class Method Summary collapse

Class Method Details

.child_candidates(word) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rvc/completion.rb', line 90

def self.child_candidates word
  els, absolute, trailing_slash = Path.parse word
  last = trailing_slash ? '' : (els.pop || '')
  els.map! { |x| x.gsub '\\', '' }
  base_loc = absolute ? Location.new($shell.fs.root) : $shell.fs.loc
  found_loc = $shell.fs.traverse(base_loc, els).first or return []
  cur = found_loc.obj
  els.unshift '' if absolute
  children = Cache[cur, :children] rescue []
  children.
    select { |k,v| k.gsub(' ', '\\ ') =~ /^#{Regexp.escape(last)}/ }.
    map { |k,v| (els+[k])*'/' }.
    map { |x| x.gsub ' ', '\\ ' }
end

.cmd_candidates(word) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/rvc/completion.rb', line 80

def self.cmd_candidates word
  ret = []
  prefix_regex = /^#{Regexp.escape(word)}/
  MODULES.each do |name,m|
    m.commands.each { |s| ret << "#{name}.#{s}" }
  end
  ret.concat ALIASES.keys
  ret.sort.select { |e| e.match(prefix_regex) }
end

.complete(word, line) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rvc/completion.rb', line 58

def self.complete word, line
  candidates = if line
    if line[' ']
      child_candidates(word)
    else
      cmd_candidates(word)
    end
  else
    child_candidates(word) + cmd_candidates(word)
  end

  candidates += mark_candidates(word)

  if candidates.size == 1 and cmd_candidates(word).member?(candidates[0])
    append_char = ' '
  else
    append_char = '/'
  end

  return append_char, candidates
end

.installObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/rvc/completion.rb', line 47

def self.install
  if Readline.respond_to? :char_is_quoted=
    Readline.completer_word_break_characters = " \t\n\"'"
    Readline.completer_quote_characters = "\"\\"
    is_quoted = lambda { |str,i| i > 0 && str[i-1] == '\\' && !is_quoted[str,i-1] }
    Readline.char_is_quoted = is_quoted
  end

  Readline.completion_proc = Completor
end

.mark_candidates(word) ⇒ Object



105
106
107
108
109
# File 'lib/rvc/completion.rb', line 105

def self.mark_candidates word
  return [] unless word.empty? || word[0..0] == '~'
  prefix_regex = /^#{Regexp.escape(word[1..-1] || '')}/
  $shell.fs.marks.keys.sort.grep(prefix_regex).map { |x| "~#{x}" }
end