Method: Rex::Ui::Text::DispatcherShell::CommandDispatcher#tab_complete_directory

Defined in:
lib/rex/ui/text/dispatcher_shell.rb

#tab_complete_directory(str, words) ⇒ Object

Return a list of possible directory for tab completion.



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rex/ui/text/dispatcher_shell.rb', line 300

def tab_complete_directory(str, words)
  directory = str[-1] == File::SEPARATOR ? str : File.dirname(str)
  filename = str[-1] == File::SEPARATOR ? '' : File.basename(str)
  entries = Dir.entries(directory).select { |fp| fp.start_with?(filename) }
  dirs = entries - ['.', '..']
  dirs = dirs.map { |fp| File.join(directory, fp).gsub(/\A\.\//, '') }
  dirs = dirs.select { |x| File.directory?(x) }
  dirs = dirs.map { |x| x + File::SEPARATOR }
  if dirs.length == 1 && dirs[0] != str && dirs[0].end_with?(File::SEPARATOR)
    # If Readline receives a single value from this function, it will assume we're done with the tab
    # completing, and add an extra space at the end.
    # This is annoying if we're recursively tab-traversing our way through subdirectories -
    # we may want to continue traversing, but MSF will add a space, requiring us to back up to continue
    # tab-completing our way through successive subdirectories.
    ::Readline.completion_append_character = nil
  end

  if dirs.length == 0 && File.directory?(str)
    # we've hit the end of the road
    dirs = [str]
  end

  dirs
end