Skip to content

Commit

Permalink
• Extract path completion into a new method.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhl committed Jan 22, 2009
1 parent 6a77b00 commit a7db6c8
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/rush/shell.rb
Expand Up @@ -115,6 +115,23 @@ def path_parts(input) # :nodoc:
end
end

def complete_path(possible_var, accessor, quote, partial_path, pre) # :nodoc:
original_var, fixed_path = possible_var, ''
if /^(.+\/)([^\/]*)$/ === partial_path
fixed_path, partial_path = $~.captures
possible_var += "['#{fixed_path}']"
end
full_path = eval("#{possible_var}.full_path", @pure_binding) rescue nil
box = eval("#{possible_var}.box", @pure_binding) rescue nil
if full_path and box
Rush::Dir.new(full_path, box).entries.select do |e|
e.name.match(/^#{Regexp.escape(partial_path)}/)
end.map do |e|
(pre || '') + original_var + accessor + quote + fixed_path + e.name + (e.dir? ? "/" : "")
end
end
end

# Try to do tab completion on dir square brackets and slash accessors.
#
# Example:
Expand All @@ -126,25 +143,10 @@ def path_parts(input) # :nodoc:
# It does work remotely, though, which is pretty sweet.
def completion_proc
proc do |input|
possible_var, accessor, quote, partial_path, pre = path_parts(input)
if possible_var
original_var, fixed_path = possible_var, ''
if /^(.+\/)([^\/]*)$/ === partial_path
fixed_path, partial_path = $~.captures
possible_var += "['#{fixed_path}']"
end
full_path = eval("#{possible_var}.full_path", @pure_binding) rescue nil
box = eval("#{possible_var}.box", @pure_binding) rescue nil
if full_path and box
dir = Rush::Dir.new(full_path, box)
return dir.entries.select do |e|
e.name.match(/^#{Regexp.escape(partial_path)}/)
end.map do |e|
(pre || '') + original_var + accessor + quote + fixed_path + e.name + (e.dir? ? "/" : "")
end
end
receiver, accessor, *rest = path_parts(input)
if receiver
complete_path(receiver, accessor, *rest)
end
nil
end
end
end
Expand Down

0 comments on commit a7db6c8

Please sign in to comment.