Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
#! /usr/bin/env ruby
#
# This is version 0.1.1 of Gilles Filippini's FXIrb, an attempt to embed
# IRB into an FXRuby FXText widget. The master page for this is here:
#
# http://www.rubygarden.org/ruby?FXIrb
#
# TODO
# - handle user input redirection
# - ^D
# - readonly everywhere but for the command line
# - readline
require "irb"
require "singleton"
require "fcntl"
module IRB
def IRB.start_in_fxirb(redir)
IRB.initialize(nil)
IRB.parse_opts
IRB.load_modules
irb = Irb.new(nil, redir)
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context
trap("SIGINT") do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
print "\n"
end
end
class Redirect < IRB::StdioInputMethod
def initialize(dest)
super()
@dest = dest
end
def gets
close
@dest.write(prompt)
str = @dest.gets(@prompt)
if /^exit/ =~ str
exit
end
@line[@line_no += 1] = str
end
def close
if @thread
$defout = @old_out.dup
$stderr = @old_err.dup
$stdin = @old_in.dup
@output[1].close
@thread.join
end
end
def redir
@output = IO.pipe
@old_out = $defout.dup
@old_err = $stderr.dup
@old_in = $stdin.dup
$stdin = @dest.input[0]
$defout = $stderr = @output[1]
@output[1].fcntl(Fcntl::F_SETFL, File::NONBLOCK)
@thread = Thread.new {
while not @output[0].eof?
select([@output[0]])
@dest.write(@output[0].read)
end
@output[0].close
}
end
end
module Fox
class FXIrb < FXText
include Singleton
include Responder
attr_reader :input
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
def initialize(p, tgt, sel, opts)
FXMAPFUNC(SEL_KEYRELEASE, 0, "onKeyRelease")
super
setFont(FXFont.new(FXApp.instance, "-misc-fixed-medium-r-semicondensed-*-*-120-*-*-c-*-iso8859-1"))
end
def create
super
setFocus
# IRB initialization
@redir = Redirect.new(self)
@input = IO.pipe
@irb = Thread.new {
IRB.start_in_fxirb(@redir)
}
end
def onKeyRelease(sender, sel, event)
if [Fox::KEY_Return, Fox::KEY_KP_Enter].include?(event.code)
newLineEntered
end
return 1
end
def newLineEntered
if @running
start = prevLine(getLength)
@input[1].puts(extractText(start, getLength - start))
else
processCommandLine(extractText(@anchor, getLength-@anchor))
end
end
def processCommandLine(cmd)
@redir.redir
@running = true
@input[1].puts cmd
end
def sendCommand(cmd)
setCursorPos(getLength)
makePositionVisible(getLength) unless isPosVisible(getLength)
cmd += "\n"
appendText(cmd)
processCommandLine(cmd)
end
def write(obj)
str = obj.to_s
appendText(str)
setCursorPos(getLength)
makePositionVisible(getLength) unless isPosVisible(getLength)
return str.length
end
def gets(prompt)
@running = false
@anchor = getLength
return @input[0].gets
end
end
end
# Stand alone run
if __FILE__ == $0
include Fox
application = FXApp.new("FXIrb", "ruby")
Thread.abort_on_exception = true
application.init(ARGV)
window = FXMainWindow.new(application, "FXIrb", nil, nil, DECOR_ALL, 0, 0, 580, 600)
FXIrb.init(window, nil, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y)
application.create
window.show(PLACEMENT_SCREEN)
application.run
end