Class: Command::TextInterpreter

Inherits:
BaseInterpreter show all
Defined in:
lib/command-set/interpreter/text.rb

Direct Known Subclasses

PlaybackInterpreter, RecordingInterpreter

Instance Attribute Summary collapse

Attributes inherited from BaseInterpreter

#command_set, #logger, #out_io, #sub_modes, #subject

Instance Method Summary collapse

Methods inherited from BaseInterpreter

#behavior, #fill_subject, #get_formatter, #get_subject, #next_command, #pop_mode, #prep_subject, #process_input, #push_mode, #subject_requirements

Constructor Details

#initializeTextInterpreter

Returns a new instance of TextInterpreter.



8
9
10
11
12
13
14
# File 'lib/command-set/interpreter/text.rb', line 8

def initialize
  super
  @complete_line = false
  @behavior.merge!(
    :prompt => [/(?:: )?$/, "> "]
  )
end

Instance Attribute Details

#complete_lineObject

Returns the value of attribute complete_line.



16
17
18
# File 'lib/command-set/interpreter/text.rb', line 16

def complete_line
  @complete_line
end

Instance Method Details

#complete(word, line) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/command-set/interpreter/text.rb', line 83

def complete(word, line)
  list = current_command_set.completion_list(line, word, build_subject)

  if list.length == 0
    raise CommandException, "Unrecognized term: #{word}"
  end

  return word if list[-1].empty?

  if list.length == 1
    return list[0]
  else
    raise CommandException, "Ambiguous term: #{word}"
  end
end

#cook_input(line) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/command-set/interpreter/text.rb', line 66

def cook_input(line)
  line = split_line(line)
  if(@complete_line)
    new_line = []
    line.each do |word|
      word = complete(word, new_line.dup)
      new_line << word
    end
    line = new_line
  end
  line.pop if line.last.empty?
  return self.current_command_set.process_terms(line, self.subject)
end

#get_formattersObject



149
150
151
# File 'lib/command-set/interpreter/text.rb', line 149

def get_formatters
  return [Results::StrategyFormatter.new(::Command::raw_stdout)]
end

#get_promptObject



153
154
155
156
157
158
159
160
161
# File 'lib/command-set/interpreter/text.rb', line 153

def get_prompt
  prompt = ""

  prompt.sub!(*(@command_set.prompt))
  @sub_modes.each do |mode|
    prompt.sub!(*(mode[0].prompt))
  end
  prompt.sub!(*(@behavior[:prompt]))
end

#goObject



18
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
44
45
46
47
48
# File 'lib/command-set/interpreter/text.rb', line 18

def go
  if @command_set.nil? or @subject.nil?
    raise "command_set or subject unset!"
  end

  @stop = false

  begin
    old_proc = set_readline_completion do |prefix|
      readline_complete(Readline.line_buffer, prefix)
    end

    line = readline(get_prompt, true)
    if line.nil?
      puts
      break
    end
    next if line.empty?
    process_line(line)
  rescue Interrupt
    @out_io.puts "Interrupt: please use \"quit\""
  rescue CommandException => ce
    output_exception("Error", ce)
  rescue Exception => e
    self.pause_before_dying(e)
  ensure
    unless old_proc.nil?
      set_readline_completion(&old_proc)
    end
  end until @stop
end

#output_exception(label, ex) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/command-set/interpreter/text.rb', line 50

def output_exception(label, ex)
  @out_io.puts label + ": " + ex.message
  @out_io.puts ex.backtrace.join("\n") if @behavior[:debug_commands]
  logger.warn ex.message
  ex.backtrace.each do |line|
    logger.debug line
  end
end

#pause_before_dying(exception) ⇒ Object



59
60
61
62
63
64
# File 'lib/command-set/interpreter/text.rb', line 59

def pause_before_dying(exception)
  output_exception("Exception", exception)
  puts "Waiting for return"
  $stdin.gets
  stop
end

#prompt_user(message) ⇒ Object



167
168
169
# File 'lib/command-set/interpreter/text.rb', line 167

def prompt_user(message)
  readline(message)
end

#readline_complete(buffer, rl_prefix) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/command-set/interpreter/text.rb', line 99

def readline_complete(buffer, rl_prefix)
  begin
    parsed_input = split_line(buffer)
    prefix = parsed_input.pop

    #Lest this kill coverage: hide exists to fix readline's irritating
    #word splitting
    hide = prefix.sub(%r{#{rl_prefix}$}, "")
      
    if /"#{prefix}$/ =~ buffer
      hide = '"' + hide
    end

    completes = current_command_set.completion_list(parsed_input, 
                                                    prefix, build_subject)
    completes.map! do |complete|
      if split_line(complete).length > 1
        ('"' + complete + '"').sub(/^#{hide}/, "")
      else
        complete.sub(/^#{hide}/, "")
      end
    end

    return completes
  rescue Object => ex
    #It's really irritating for an app to crap out in completion
    return ["#{ex.class}: #{ex.message}", ""]
  end
end

#split_line(line) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/command-set/interpreter/text.rb', line 129

def split_line(line)
  line_array = [""]
  scanner = StringScanner.new(line)
  scanner.scan(/\s*/)

  until scanner.eos?
    next_break = scanner.scan(/['"]/) || '\s'
    line_array.last << scanner.scan(/[^#{next_break}\\]*/)
    stopped_by = scanner.scan(/[#{next_break}\\]/)
    if stopped_by == '\\'
      line_array.last << scanner.getch
    elsif not (stopped_by.nil? or stopped_by =~ /['"]/)
      scanner.scan(/\s*/)
      line_array << ""
    end
  end

  return line_array
end

#stopObject



163
164
165
# File 'lib/command-set/interpreter/text.rb', line 163

def stop
  @stop = true
end