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
47 48 49 |
# File 'lib/shell_shock/context.rb', line 47 def abort! @abort = true end |
#abort? ⇒ Boolean
51 52 53 |
# File 'lib/shell_shock/context.rb', line 51 def abort? @abort end |
#add_command(command, *aliases) ⇒ Object
55 56 57 58 |
# File 'lib/shell_shock/context.rb', line 55 def add_command command, *aliases @commands ||= {} aliases.each { |a| @commands[a] = command } end |
#head_tail(string) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/shell_shock/context.rb', line 13 def head_tail(string) if string m = /[^ ]+/.match(string.strip) return m[0], m.post_match.strip if m end ["", ""] end |
#push ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/shell_shock/context.rb', line 60 def push @prompt ||= " > " add_command HelpCommand.new(@commands), "help" add_command ExitCommand.new(self), "exit" 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 puts end end |
#refresh ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/shell_shock/context.rb', line 21 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.map { |c| "#{c} " } log { "returning #{completions.inspect} completions" } completions end end |