Module: ShellShock::Context
- Includes:
- Logger
- Defined in:
- lib/shell_shock/context.rb
Instance Method Summary collapse
- #abort! ⇒ Object
- #abort? ⇒ Boolean
- #add_command(command, *aliases) ⇒ Object
- #head_tail(string) ⇒ Object
- #push ⇒ Object
- #refresh ⇒ Object
Methods included from Logger
Instance Method Details
#abort! ⇒ Object
45 46 47 |
# File 'lib/shell_shock/context.rb', line 45 def abort! @abort = true end |
#abort? ⇒ Boolean
49 50 51 |
# File 'lib/shell_shock/context.rb', line 49 def abort? @abort end |
#add_command(command, *aliases) ⇒ Object
53 54 55 56 |
# File 'lib/shell_shock/context.rb', line 53 def add_command command, *aliases @commands ||= {} aliases.each {|a| @commands[a] = command} end |
#head_tail(string) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/shell_shock/context.rb', line 11 def head_tail string if string m = /[^ ]+/.match(string.strip) return m[0], m.post_match.strip if m end return '', '' end |
#push ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/shell_shock/context.rb', line 58 def push @prompt ||= ' > ' add_command HelpCommand.new(@commands), '?', 'help' add_command ExitCommand.new(self), 'exit', 'quit' begin until abort? refresh line = Readline.readline(@prompt, true) if line first, rest = head_tail(line) log { "looking for command \"#{first}\" with parameter \"#{rest}\"" } if @commands[first] @commands[first].execute rest else puts "unknown command \"#{first}\"" end else abort! end puts end rescue Interrupt => e puts end end |
#refresh ⇒ Object
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 |
# File 'lib/shell_shock/context.rb', line 19 def refresh refresh_commands if respond_to?(:refresh_commands) Readline.completer_word_break_characters = '' Readline.completion_proc = lambda do |string| log { "trying completion for \"#{string}\"" } first, rest = head_tail(string) log { "split \"#{first}\" from \"#{rest}\"" } if first command = @commands[first] if command log { "matched #{first} command" } if command.respond_to?(:completion) completions = command.completion(rest).map {|c| "#{first} #{c}" } else log { "#{first} has no completion proc" } completions = [] end end end completions ||= @commands.keys.grep( /^#{Regexp.escape(first)}/ ).sort log { "returning #{completions.inspect} completions" } completions end end |