Module: Complete

Defined in:
lib/droxi/complete.rb

Overview

Module containing tab-completion logic and methods.

Class Method Summary collapse

Class Method Details

.collapse(path) ⇒ Object



76
77
78
79
80
# File 'lib/droxi/complete.rb', line 76

def self.collapse(path)
  nil while path.sub!(/[^\/]+\/\.\.\//, '/')
  nil while path.sub!('./', '')
  path
end

.local(string) ⇒ Object

Returns an Array of potential local tab-completions for a String.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/droxi/complete.rb', line 15

def self.local(string)
  dir = local_search_path(string)
  name = string.end_with?('/') ? '' : File.basename(string)

  Dir.entries(dir).select do |entry|
    entry.start_with?(name) && !/^\.{1,2}$/.match(entry)
  end.map do |entry|
    entry << (File.directory?(dir + '/' + entry) ? '/' : ' ')
    string + entry[name.length, entry.length]
  end
end

.local_dir(string) ⇒ Object

Returns an Array of potential local tab-completions for a String, including only directories.



29
30
31
# File 'lib/droxi/complete.rb', line 29

def self.local_dir(string)
  local(string).select { |result| result.end_with?('/') }
end

.local_search_path(string) ⇒ Object

Return the directory in which to search for potential local tab-completions for a String. Defaults to working directory in case of bogus input.



6
7
8
9
10
11
12
# File 'lib/droxi/complete.rb', line 6

def self.local_search_path(string)
  begin
    File.expand_path(strip_filename(string))
  rescue
    Dir.pwd
  end
end

.remote(string, state) ⇒ Object

Returns an Array of potential remote tab-completions for a String.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/droxi/complete.rb', line 46

def self.remote(string, state)
  dir = remote_search_path(string, state)
  name = string.end_with?('/') ? '' : File.basename(string)

  state.contents(dir).map do |entry|
    File.basename(entry) 
  end.select do |entry|
    entry.start_with?(name) && !/^\.{1,2}$/.match(entry)
  end.map do |entry|
    entry << (state.directory?(dir + '/' + entry) ? '/' : ' ')
    string + entry[name.length, entry.length]
  end
end

.remote_dir(string, state) ⇒ Object

Returns an Array of potential remote tab-completions for a String, including only directories.



62
63
64
# File 'lib/droxi/complete.rb', line 62

def self.remote_dir(string, state)
  remote(string, state).select { |result| result.end_with?('/') }
end

.remote_search_path(string, state) ⇒ Object

Return the directory in which to search for potential remote tab-completions for a String.



35
36
37
38
39
40
41
42
43
# File 'lib/droxi/complete.rb', line 35

def self.remote_search_path(string, state)
  path = case
  when string.empty? then state.pwd + '/'
  when string.start_with?('/') then string
  else state.pwd + '/' + string
  end

  strip_filename(collapse(path))
end

.strip_filename(path) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/droxi/complete.rb', line 68

def self.strip_filename(path)
  if path != '/'
    path.end_with?('/') ? path.sub(/\/$/, '') : File.dirname(path)
  else
    path
  end
end