Class: FXIrb

Inherits:
FXText
  • Object
show all
Includes:
Responder, Singleton
Defined in:
lib/fxirb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, tgt, sel, opts) ⇒ FXIrb

Returns a new instance of FXIrb.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fxirb.rb', line 159

def initialize(p, tgt, sel, opts)
  FXMAPFUNC(SEL_KEYRELEASE, 0, "onKeyRelease")
  FXMAPFUNC(SEL_KEYPRESS, 0, "onKeyPress")
  FXMAPFUNC(SEL_LEFTBUTTONPRESS,0,"onLeftBtnPress")
  FXMAPFUNC(SEL_MIDDLEBUTTONPRESS,0,"onMiddleBtnPress")
  FXMAPFUNC(SEL_LEFTBUTTONRELEASE,0,"onLeftBtnRelease")

  super
  setFont(FXFont.new(FXApp.instance, "lucida console", 9))
  @anchor = 0
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



145
146
147
# File 'lib/fxirb.rb', line 145

def input
  @input
end

Class Method Details

.init(p, tgt, sel, opts) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fxirb.rb', line 147

def FXIrb.init(p, tgt, sel, opts)
  unless @__instance__
    Thread.critical = true
    begin
      @__instance__ ||= new(p, tgt, sel, opts)
    ensure
      Thread.critical = false
    end
  end
  return @__instance__
end

Instance Method Details

#crashObject



195
196
197
# File 'lib/fxirb.rb', line 195

def crash
  instance_eval(&@exit_proc)
end

#createObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fxirb.rb', line 171

def create
  super
  setFocus
  # IRB initialization
  @inputAdded = 0
  @input = IO.pipe
  $DEFAULT_OUTPUT = self

  @im = FXIRBInputMethod.new
  @irb = Thread.new {
    IRB.start_in_fxirb(@im)
    self.crash
  }


  @multiline = false

  @exit_proc = lambda {exit}
end

#dedentObject



285
286
287
288
289
290
291
# File 'lib/fxirb.rb', line 285

def dedent
  str = getline
  @anchor -= 2
  rmline
  appendText(str)
  setCursorPos(getLength)
end

#emptyline?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/fxirb.rb', line 311

def emptyline?
  getline == ""
end

#get_lineObject



379
380
381
382
383
384
385
386
# File 'lib/fxirb.rb', line 379

def get_line
  @anchor = getLength
  if @inputAdded == 0
    Thread.stop
  end
  @inputAdded -= 1
  return @input[0].gets
end

#getlineObject



301
302
303
# File 'lib/fxirb.rb', line 301

def getline
  extractText(@anchor, getLength-@anchor)
end

#history(dir) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/fxirb.rb', line 293

def history(dir)
  str = (dir == :prev) ? @im.prevCmd.chomp : @im.nextCmd.chomp
  if str != ""
    removeText(@anchor, getLength-@anchor)
    write(str)
  end
end

#indented?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/fxirb.rb', line 315

def indented?
  extractText(@anchor-2, 2) == "  "
end

#newLineEnteredObject



339
340
341
342
343
344
345
346
# File 'lib/fxirb.rb', line 339

def newLineEntered
  if getLength-@anchor > 0
    processCommandLine(extractText(@anchor, getLength-@anchor))
  else
    # Probably wrong, but might work
    processCommandLine(extractText(@anchor, getLength))
  end
end

#on_exit(&block) ⇒ Object



191
192
193
# File 'lib/fxirb.rb', line 191

def on_exit(&block)
  @exit_proc = block
end

#onKeyPress(sender, sel, event) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/fxirb.rb', line 207

def onKeyPress(sender,sel,event)
  case event.code
  when Fox::KEY_Return, Fox::KEY_KP_Enter
    setCursorPos(getLength)
    super
  when Fox::KEY_Up,Fox::KEY_KP_Up
    str = extractText(@anchor, getCursorPos-@anchor)
    if str =~ /\n/
      @multiline = true
      super
      setCursorPos(@anchor+1) if getCursorPos < @anchor
    else
      history(:prev) unless @multiline
    end
  when Fox::KEY_Down,Fox::KEY_KP_Down
    str = extractText(getCursorPos, getLength - getCursorPos)
    if str =~ /\n/
      @multiline = true
      super
    else
      history(:next) unless @multiline
    end
  when Fox::KEY_Left,Fox::KEY_KP_Left
    if getCursorPos > @anchor
      super
    end
  when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace
    if getCursorPos > @anchor
      super
    end
  when Fox::KEY_Home, Fox::KEY_KP_Home
    setCursorPos(@anchor)
  when Fox::KEY_End, Fox::KEY_KP_End
    setCursorPos(getLength)
  when Fox::KEY_Page_Up, Fox::KEY_KP_Page_Up
    history(:prev)
  when Fox::KEY_Page_Down, Fox::KEY_KP_Page_Down
    history(:next)
  when Fox::KEY_bracketright
    #auto-dedent if the } or ] is on a line by itself
    if (emptyline? or (getline == "en")) and indented?
      dedent
    end
    super
  when Fox::KEY_u
    if (event.state & CONTROLMASK) != 0
      str = extractText(getCursorPos, getLength - getCursorPos)
      rmline
      appendText(str)
      setCursorPos(@anchor)
    end
    super
  when Fox::KEY_k
    if (event.state & CONTROLMASK) != 0
      str = extractText(@anchor, getCursorPos-@anchor)
      rmline
      appendText(str)
      setCursorPos(getLength)
    end
    super
  when Fox::KEY_d
    if (event.state & CONTROLMASK) != 0
      #Ctrl - D
      rmline
      appendText("exit")
      newLineEntered
    else
      # test for 'end' so we can dedent
      if (getline == "en") and indented?
        dedent
      end
    end
    super
  else
    super
  end
end

#onKeyRelease(sender, sel, event) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/fxirb.rb', line 199

def onKeyRelease(sender, sel, event)
  case event.code
  when Fox::KEY_Return, Fox::KEY_KP_Enter
    newLineEntered
  end
  return 1
end

#onLeftBtnPress(sender, sel, event) ⇒ Object



319
320
321
322
323
# File 'lib/fxirb.rb', line 319

def onLeftBtnPress(sender,sel,event)
  @store_anchor = @anchor
  setFocus
  super
end

#onLeftBtnRelease(sender, sel, event) ⇒ Object



325
326
327
328
329
330
# File 'lib/fxirb.rb', line 325

def onLeftBtnRelease(sender,sel,event)
  super
  @anchor = @store_anchor
  setCursorPos(@anchor)
  setCursorPos(getLength)
end

#onMiddleBtnPress(sender, sel, event) ⇒ Object



332
333
334
335
336
337
# File 'lib/fxirb.rb', line 332

def onMiddleBtnPress(sender,sel,event)
  pos=getPosAt(event.win_x,event.win_y)
  if pos >= @anchor
    super
  end
end

#processCommandLine(cmd) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/fxirb.rb', line 348

def processCommandLine(cmd)
  @multiline = false
  lines = cmd.split(/\n/)
  lines.each {|i|
    @input[1].puts i
    @inputAdded += 1
  }
  @im.print_prompt = false
  while (@inputAdded > 0) do
    @irb.run
  end
  @im.merge_last(lines.length)
  @im.print_prompt = true
end

#rmlineObject



305
306
307
308
309
# File 'lib/fxirb.rb', line 305

def rmline
  str = getline
  removeText(@anchor, getLength-@anchor)
  str
end

#sendCommand(cmd) ⇒ Object



363
364
365
366
367
368
369
# File 'lib/fxirb.rb', line 363

def sendCommand(cmd)
  setCursorPos(getLength)
  makePositionVisible(getLength) unless isPosVisible(getLength)
  cmd += "\n"
  appendText(cmd)
  processCommandLine(cmd)
end

#write(obj) ⇒ Object



371
372
373
374
375
376
377
# File 'lib/fxirb.rb', line 371

def write(obj)
  str = obj.to_s
  appendText(str)
  setCursorPos(getLength)
  makePositionVisible(getLength) unless isPosVisible(getLength)
  return str.length
end