Class: Downloads::Commands::Shell

Inherits:
Base
  • Object
show all
Defined in:
lib/downloads/commands/shell.rb

Instance Attribute Summary

Attributes inherited from Base

#configuration, #local, #options, #remote

Instance Method Summary collapse

Methods inherited from Base

command_name, #configure, inherited, #initialize, usage, #usage, #valid?

Constructor Details

This class inherits a constructor from Downloads::Commands::Base

Instance Method Details

#runObject



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
# File 'lib/downloads/commands/shell.rb', line 9

def run
  Readline.completer_word_break_characters = ''
  Readline.completion_append_character = ' '
  Readline.completion_proc = lambda do |line|
    words = Shellwords.shellwords("downloads #{line}")
    words << '' if line.split('')[-1] == ' '
    previous_token, current_token = words[-2..-1]
    completions = TabTab::Definition['downloads'].extract_completions(previous_token, current_token, {})
    completions.map { |completion| (words[1..-2] + [completion]).join(' ') }
  end

  puts 'Type ctrl-d or quit to exit.'

  loop do
    line = Readline::readline('> ') || 'quit'
    next if line.strip == ''
    Readline::HISTORY.push(line)
    command = Downloads::Commands.lookup(line.split(' '))
    if command.valid?
      command.run
    else
      puts command.usage
    end
  end
end