Class: Command::TextInterpreter

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

Instance Attribute Summary collapse

Attributes inherited from BaseInterpreter

#command_set, #logger, #out_io, #subject

Instance Method Summary collapse

Methods inherited from BaseInterpreter

#behavior, #get_subject, #next_command, #pop_mode, #prep_subject, #process_input, #push_mode, #subject_requirements, #subject_template

Constructor Details

#initializeTextInterpreter

Returns a new instance of TextInterpreter.



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

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

Instance Attribute Details

#complete_lineObject

Returns the value of attribute complete_line.



67
68
69
# File 'lib/command-set/text-interpreter.rb', line 67

def complete_line
  @complete_line
end

Instance Method Details

#assign_terms(cmd) ⇒ Object



127
128
129
130
131
132
# File 'lib/command-set/text-interpreter.rb', line 127

def assign_terms(cmd)
  terms = super
  unless terms.empty?
    puts "(Discarding extraneous: #{terms.join(" ")}"
  end
end

#complete(word, line) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/command-set/text-interpreter.rb', line 137

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

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

  return word if list[-1].empty?

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

#cook_input(line) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/command-set/text-interpreter.rb', line 114

def cook_input(line)
  line = split_line(line)
  if(@complete_line)
    new_line = []
    line.each do |word|
      word = complete(word, new_line)
      new_line << word
    end
    line = new_line
  end
  return CommandSetup.new(line)
end

#get_formatterObject



182
183
184
# File 'lib/command-set/text-interpreter.rb', line 182

def get_formatter
  return Results::StrategyFormatter.new(::Command::raw_stdout)
end

#get_promptObject



186
187
188
189
190
191
192
193
194
# File 'lib/command-set/text-interpreter.rb', line 186

def get_prompt
  prompt = ""

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

#goObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/command-set/text-interpreter.rb', line 69

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

  Readline.completion_proc = lambda do |prefix|
    parsed_input = Readline.line_buffer.split(" ")
    unless prefix.empty?
      parsed_input.pop
    end

    list = current_command_set.completion_list(parsed_input, 
                                               prefix, build_subject)
    list
  end

  @stop = false

  begin
    line = Readline.readline(get_prompt, true)
    next if line.empty?
    process_line(line)
  rescue Interrupt
    puts "Interrupt: please use \"quit\""
  rescue CommandException => ce
    @out_io.puts "Error: " + ce.message
    @out_io.puts ce.backtrace if @behavior[:debug_commands]
    logger.warn ce.message
    ce.backtrace.each do |line|
      logger.debug line
    end

  rescue Exception => e
    @out_io.puts "Exception: " + e.message
    @out_io.puts e.backtrace.join("\n") if @behavior[:debug_commands]
    logger.warn e.message
    e.backtrace.each do |line|
      logger.debug line
    end
    puts "Waiting for return"
    $stdin.gets
    @stop = true
  end until @stop
end

#prompt_user(message) ⇒ Object



200
201
202
# File 'lib/command-set/text-interpreter.rb', line 200

def prompt_user(message)
  Readline.readline(message)
end

#split_line(line) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/command-set/text-interpreter.rb', line 153

def split_line(line)
  line_array = []
  scanner = StringScanner.new(line)

  until scanner.eos?
    scanner.scan(/\s*/)
    quote = scanner.scan(/['"]/)
    unless quote.nil?
      line_array << ""
      until scanner.eos?
        line_array.last << scanner.scan(/[^#{quote}\\]*/)
        stopped_by = scanner.scan(/[#{quote}\\]/)
        break unless stopped_by == '\\'
        line_array.last << scanner.getch
      end
    else
      line_array << ""
      until scanner.eos?
        line_array.last << scanner.scan(/[^\s\\]*/)
        stopped_by = scanner.scan(/[\s\\]/)
        break unless stopped_by == '\\'
        line_array.last << scanner.getch
      end
    end
  end

  return line_array
end

#stopObject



196
197
198
# File 'lib/command-set/text-interpreter.rb', line 196

def stop
  @stop = true
end