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.



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

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.



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

def input
  @input
end

Class Method Details

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



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

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



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

def crash
  instance_eval(&@exit_proc)
end

#createObject



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

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



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

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

#emptyline?Boolean

Returns:

  • (Boolean)


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

def emptyline?
  getline == ""
end

#get_lineObject



373
374
375
376
377
378
379
380
# File 'lib/fxirb.rb', line 373

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

#getlineObject



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

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

#history(dir) ⇒ Object



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

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)


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

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

#newLineEnteredObject



338
339
340
# File 'lib/fxirb.rb', line 338

def newLineEntered
  processCommandLine(extractText(@anchor, getLength-@anchor))
end

#on_exit(&block) ⇒ Object



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

def on_exit(&block)
  @exit_proc = block
end

#onKeyPress(sender, sel, event) ⇒ Object



206
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
# File 'lib/fxirb.rb', line 206

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



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

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



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

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

#onLeftBtnRelease(sender, sel, event) ⇒ Object



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

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

#onMiddleBtnPress(sender, sel, event) ⇒ Object



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

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

#processCommandLine(cmd) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/fxirb.rb', line 342

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



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

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

#sendCommand(cmd) ⇒ Object



357
358
359
360
361
362
363
# File 'lib/fxirb.rb', line 357

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

#write(obj) ⇒ Object



365
366
367
368
369
370
371
# File 'lib/fxirb.rb', line 365

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