-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_completion
executable file
·48 lines (40 loc) · 1.35 KB
/
dev_completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
class DevCompletion
# Cache the full typed command (eg "dev Web").
def initialize(command)
@command = command
end
# Prepend the current path to each directory name;
# filter only paths matching the current typed path;
# then tidy up the (escaped) output.
def matches
@matches ||= directories.map do |directory|
target_directory ? "#{target_directory}/#{directory}" : directory
end.select do |directory|
directory.downcase[0, typed.length] == typed.downcase
end.map do |path|
# This should be done last.
path.gsub("\\", '')
end
end
# Cache the typed path (without the leading "dev<space>" part).
def typed
@typed ||= @command[/\s(.+?)$/, 1] || ''
end
# Cache the target directory.
# Eg "dev Web/my_p<tab>" will set the target directory to "Web".
def target_directory
@target_directory ||= if typed =~ /\//
split = typed.split('/')
joined = split[0...-1].join('/')
joined.empty? ? split[0] : joined
end
end
# Retrieve a (formatted) list of all directories within the target directory.
def directories
@directories ||= `cd ~/Dev/#{target_directory} ; ls -d */ | tr '' ' '`.split("\n")
end
end
# Instantiate a new instance, passing in the full typed command, and printing the matches.
puts DevCompletion.new(ENV["COMP_LINE"]).matches
exit 0