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

TODO convert to globbing



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

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

.cmd_candidates(word) ⇒ Object



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

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



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

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



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

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



107
108
109
110
111
# File 'lib/rvc/completion.rb', line 107

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