Class: WxIRB::InputTextCtrl

Inherits:
Wx::TextCtrl
  • Object
show all
Includes:
Wx
Defined in:
lib/wxirb.rb

Overview

Keyboard commands in the input text area are what you’d expect in a multi-line textbox with some additional special keyboard modifiers. See InputTextCtrl#on_char

Constant Summary collapse

STYLE =
TE_PROCESS_TAB|TE_PROCESS_ENTER|WANTS_CHARS|TE_MULTILINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, output, mirb) ⇒ InputTextCtrl

Returns a new instance of InputTextCtrl.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/wxirb.rb', line 187

def initialize(parent, output, mirb)
  super(parent, :style => STYLE)
  @history = CmdHistory.new
  @output = output

  @stdout_save = $stdout
  $stdout = StringIO.new
  @mirb = mirb 
  evt_idle :on_idle
  evt_char  :on_char

  @font = Wx::Font.new(10, Wx::MODERN, Wx::NORMAL, Wx::NORMAL)
  set_default_style Wx::TextAttr.new(Wx::BLACK, Wx::WHITE, @font)

  paint do |dc| 
    b_height = dc.get_text_extent("@", @font)[1] * 4
    cache_best_size Wx::Size.new(self.size.width, b_height)
  end
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



183
184
185
# File 'lib/wxirb.rb', line 183

def history
  @history
end

Instance Method Details

#on_char(evt) ⇒ Object

Fires on text input events. Implements a few special keyboard handlers

  • META+ENTER : sends a newline inside the input window instead of actually running a command

  • META+UP-ARROW : scroll up in history

  • META+DOWN-ARROW : scroll down in history



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/wxirb.rb', line 218

def on_char(evt)
  k = evt.key_code
  mflag = evt.modifiers

  if [MOD_NONE, MOD_SHIFT].include?(mflag) and (0x20..0x7f).include?(k)
    evt.skip()
    return
  end

  case k
  when K_RETURN
    if evt.inputmod_down
      # multi-line command uses meta-down-arrow for newline
      self.write_text("\n")
    else
      @history << self.value
      run self.value
      self.clear
    end
    return
  when (evt.inputmod_down and K_UP)
    if hist=history.prev
      self.value = hist
      self.set_insertion_point_end
      return
    end
  when (evt.inputmod_down and K_DOWN)
    if hist=history.next
      self.value = hist
      self.set_insertion_point_end
      return
    else
      self.clear
    end
  end
  evt.skip()
end

#on_idle(evt) ⇒ Object

Fires on Wx::IdleEvent - saves persistent history if history has changed



208
209
210
# File 'lib/wxirb.rb', line 208

def on_idle(evt)
  history.save
end

#run(cmd) ⇒ Object

Runs a command through the mock irb class, handles display details.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/wxirb.rb', line 257

def run(cmd)
  (lines = cmd.split(/\r?\n/)).each_index do |idx|
    begin
      line = lines[idx] + "\n"
      @output.default_style = Wx::TextAttr.new(Wx::BLUE)
      @output << ">> #{line}"

      out, obj = @mirb.run(line)
      @output.default_style = Wx::TextAttr.new(Wx::BLACK)
      @output << out
      @output.default_style = Wx::TextAttr.new(Wx::Colour.new(100,100,100))
      @output << "=> #{obj.inspect}\n"
    rescue MimickIRB::Empty
    rescue MimickIRB::Continue
      if idx == lines.size-1
        @output.default_style = Wx::TextAttr.new(Wx::LIGHT_GREY)
        @output << "...\n"
      end
    rescue Exception => se
      @output.default_style = Wx::TextAttr.new(Wx::RED)
      @output << (se.inspect + "\n" + se.backtrace.join("\n") + "\n")
    end
  end
end