move tab completion helpers up to ShellDispatcher to avoid copy-paste and allow non-msf shells to complete filenames

git-svn-id: file:///home/svn/framework3/trunk@11562 4d416f70-5f16-0410-b530-b9f4589650da
unstable
James Lee 2011-01-12 22:09:32 +00:00
parent e27e824755
commit 7226a43bea
3 changed files with 48 additions and 68 deletions

View File

@ -78,43 +78,6 @@ module CommandDispatcher
dlog("Call stack:\n#{$@.join("\n")}", 'core', LEV_1)
end
#
# Provide command-specific tab completion
#
def tab_complete_helper(str, words)
items = []
# Is the user trying to tab complete one of our commands?
if (commands.include?(words[0]))
if (self.respond_to?('cmd_'+words[0]+'_tabs'))
res = self.send('cmd_'+words[0]+'_tabs', str, words)
return [] if res.nil?
items.concat(res)
else
# Avoid the default completion list for known commands
return []
end
end
return items
end
#
# Provide a generic tab completion for file names.
#
# If the only completion is a directory, this descends into that directory
# and continues completions with filenames contained within.
#
def tab_complete_filenames(str, words)
matches = ::Readline::FILENAME_COMPLETION_PROC.call(str)
if matches and matches.length == 1 and File.directory?(matches[0])
dir = matches[0]
dir += File::SEPARATOR if dir[-1,1] != File::SEPARATOR
matches = ::Readline::FILENAME_COMPLETION_PROC.call(dir)
end
matches
end
#
# The driver that this command dispatcher is associated with.
#

View File

@ -583,29 +583,6 @@ class Console::CommandDispatcher::Core
return true
end
#
# Provide command-specific tab completion
# Stolen directly from msf/ui/console/command_dispatcher/core.rb
# perhaps this should be moved into rex/ui/text/dispatcher_shell.rb ?
#
def tab_complete_helper(str, words)
items = []
# Is the user trying to tab complete one of our commands?
if (commands.include?(words[0]))
if (self.respond_to?('cmd_'+words[0]+'_tabs'))
res = self.send('cmd_'+words[0]+'_tabs', str, words)
return [] if res.nil?
items.concat(res)
else
# Avoid the default completion list for known commands
return []
end
end
return items
end
protected
attr_accessor :extensions # :nodoc:

View File

@ -148,20 +148,22 @@ module DispatcherShell
# If no command is set and it supports commands, add them all
if (tab_words.empty? and dispatcher.respond_to?('commands'))
items.concat(dispatcher.commands.to_a.map { |x| x[0] })
items.concat(dispatcher.commands.keys)
end
# If the dispatcher exports a tab completion function, use it
if(dispatcher.respond_to?('tab_complete_helper'))
res = dispatcher.tab_complete_helper(str, tab_words)
else
res = tab_complete_helper(dispatcher, str, tab_words)
end
if (res.nil?)
# A nil response indicates no optional arguments
return [''] if items.empty?
else
# Otherwise we add the completion items to the list
items.concat(res)
end
if (res.nil?)
# A nil response indicates no optional arguments
return [''] if items.empty?
else
# Otherwise we add the completion items to the list
items.concat(res)
end
}
@ -184,6 +186,44 @@ module DispatcherShell
}
end
#
# Provide command-specific tab completion
#
def tab_complete_helper(dispatcher, str, words)
items = []
tabs_meth = "cmd_#{words[0]}_tabs"
# Is the user trying to tab complete one of our commands?
if (dispatcher.commands.include?(words[0]) and dispatcher.respond_to?(tabs_meth))
res = dispatcher.send(tabs_meth, str, words)
return [] if res.nil?
items.concat(res)
regexp = /^#{Regexp.quote(str)}/
items.select! { |i| i =~ regexp }
else
# Avoid the default completion list for known commands
return []
end
return items
end
#
# Provide a generic tab completion for file names.
#
# If the only completion is a directory, this descends into that directory
# and continues completions with filenames contained within.
#
def tab_complete_filenames(str, words)
matches = ::Readline::FILENAME_COMPLETION_PROC.call(str)
if matches and matches.length == 1 and File.directory?(matches[0])
dir = matches[0]
dir += File::SEPARATOR if dir[-1,1] != File::SEPARATOR
matches = ::Readline::FILENAME_COMPLETION_PROC.call(dir)
end
matches
end
#
# Run a single command line.
#