fxri 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ # Copyright (c) 2005 Martin Ankerl
2
+ class Search_Engine
3
+ def initialize(gui, data)
4
+ @gui = gui
5
+ @data = data
6
+ @search_thread = nil
7
+ end
8
+
9
+ # Executed whenever a search criteria changes to update the packet list.
10
+ def on_search
11
+ # restart current search
12
+ @end_time = Time.now + $cfg.search_delay
13
+ @restart_search = true
14
+ @gui.search_label.enabled = false
15
+ return if @search_thread && @search_thread.status
16
+
17
+ @search_thread = Thread.new(@search_thread) do
18
+ begin
19
+ @gui.search_label.enabled = false
20
+ # wait untill deadline
21
+ while (t = (@end_time - Time.now)) > 0
22
+ sleep(t)
23
+ end
24
+
25
+ @data.gui_mutex.synchronize do
26
+ # the thread has to use the gui mutex inside
27
+ @restart_search = false
28
+
29
+ match_data = get_match_data
30
+
31
+ # remove all items
32
+ @gui.packet_list.dirty_clear
33
+
34
+ # add all items that match the search criteria
35
+ status_text_deadline = Time.now + $cfg.status_line_update_interval
36
+ @data.items.each do |item|
37
+ #item.parent = @gui.packet_list if match?(item, match_data)
38
+ if match?(item, match_data)
39
+ item.show
40
+ now = Time.now
41
+ if now > status_text_deadline
42
+ update_search_status_text
43
+ status_text_deadline = now + $cfg.status_line_update_interval
44
+ end
45
+ end
46
+ break if @restart_search
47
+ end
48
+ update_search_status_text
49
+
50
+ if (@gui.packet_list.numItems > 0)
51
+ @gui.packet_list.setCurrentItem(0)
52
+ @gui.packet_list.selectItem(0)
53
+ @gui.main.show_info(@gui.packet_list.getItem(0).packet_item.data)
54
+ end
55
+ @gui.search_label.enabled = true
56
+
57
+ end # synchronize
58
+ end while @restart_search# || match_data != @gui.search_field.text.downcase.split
59
+ end #thread.new
60
+ end
61
+
62
+ def get_match_data
63
+ str_to_match_data(@gui.search_field.text)
64
+ end
65
+
66
+ # Converts a string into a match_data representation.
67
+ def str_to_match_data(str, index=0)
68
+ words = [ ]
69
+ exclude = [ ]
70
+ is_exclude = false
71
+
72
+ while str[index]
73
+ case str[index]
74
+ when ?", ?'
75
+ word, index = get_word(str, index+1, str[index])
76
+ unless word.empty?
77
+ if is_exclude
78
+ exclude.push word
79
+ is_exclude = false
80
+ else
81
+ words.push word
82
+ end
83
+ end
84
+
85
+ when 32 # space
86
+ is_exclude = false
87
+
88
+ =begin
89
+ when ?>
90
+ min, index = get_word(str, index+1)
91
+ min = @gui.logic.size_to_nr(min)
92
+
93
+ when ?<
94
+ max, index = get_word(str, index+1)
95
+ max = @gui.logic.size_to_nr(max)
96
+ =end
97
+ when ?-
98
+ is_exclude = true
99
+
100
+ else
101
+ word, index = get_word(str, index)
102
+ if is_exclude
103
+ exclude.push word
104
+ is_exclude = false
105
+ else
106
+ words.push word
107
+ end
108
+ end
109
+
110
+ index += 1
111
+ end
112
+
113
+ # check if word has upcase letters
114
+ words.collect! do |w|
115
+ [w, /[A-Z]/.match(w)!=nil]
116
+ end
117
+ exclude.collect! do |w|
118
+ [w, /[A-Z]/.match(w)!=nil]
119
+ end
120
+ [words, exclude]
121
+ end
122
+
123
+ def get_word(str, index, delim=32) # 32==space
124
+ word = ""
125
+ c = str[index]
126
+ while (c && c != delim)
127
+ word += c.chr
128
+ index += 1
129
+ c = str[index]
130
+ end
131
+ [word, index]
132
+ end
133
+
134
+ # Update the text for the number of displayed packs.
135
+ def update_search_status_text
136
+ @gui.search_label.text = sprintf($cfg.text.search, @gui.packet_list.numItems, @data.items.size)
137
+ end
138
+
139
+ # Find out if item is matched by current search criteria.
140
+ def match?(item, match_data)
141
+ words, exclude, min, max = match_data
142
+
143
+ # check text that has to be there
144
+ searchable, sortable_downcase, sortable_normal = item.sortable(0)
145
+ words.each do |match_str, is_sensitive|
146
+ if is_sensitive
147
+ return false unless sortable_normal.include?(match_str)
148
+ else
149
+ return false unless sortable_downcase.include?(match_str)
150
+ end
151
+ end
152
+
153
+ # check text not allowed to be there
154
+ exclude.each do |match_str, is_sensitive|
155
+ if is_sensitive
156
+ return false if sortable_normal.include?(match_str)
157
+ else
158
+ return false if sortable_downcase.include?(match_str)
159
+ end
160
+ end
161
+
162
+ # each check ok
163
+ true
164
+ end
165
+ end
data/lib/fxirb.rb ADDED
@@ -0,0 +1,339 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ # TODO
4
+ # - handle user input redirection
5
+ # - readline
6
+ # - multiline commands
7
+
8
+ # Credits:
9
+ # - Initial linux version: Gilles Filippini
10
+ # - Initial windows port : Marco Fraillis
11
+ # - Currently maintained and developed by
12
+ # Martin DeMello <martindemello@gmail.com>
13
+
14
+ require "fox12"
15
+ require "irb"
16
+ require "singleton"
17
+ require "English"
18
+
19
+ include Fox
20
+
21
+ STDOUT.sync = true
22
+
23
+ class FXIRBInputMethod < IRB::StdioInputMethod
24
+
25
+ def initialize
26
+ super
27
+ @history = 1
28
+ @begin = nil
29
+ @end = nil
30
+ end
31
+
32
+ def gets
33
+ print @prompt
34
+
35
+ if @prompt =~ /(\d+)[>*]/
36
+ print " "*$1.to_i
37
+ end
38
+
39
+ str = FXIrb.instance.gets
40
+ if /^exit/ =~ str
41
+ exit
42
+ end
43
+
44
+ @line_no += 1
45
+ @history = @line_no + 1
46
+ @line[@line_no] = str
47
+ end
48
+
49
+ def compact
50
+ print [@compact, @continued].inspect
51
+ if false # @compact
52
+ @compact = false
53
+ a = @line_no - @continued
54
+ @lines[a] = @lines[a..2].join
55
+ end
56
+ end
57
+
58
+ def prevCmd
59
+ if @line_no > 0
60
+ @history -= 1 unless @history <= 1
61
+ return line(@history)
62
+ end
63
+ return ""
64
+ end
65
+
66
+ def nextCmd
67
+ if (@line_no > 0) && (@history < @line_no)
68
+ @history += 1
69
+ return line(@history)
70
+ end
71
+ return ""
72
+ end
73
+
74
+ end
75
+
76
+ module IRB
77
+ def IRB.start_in_fxirb(im)
78
+ if RUBY_VERSION < "1.7.3"
79
+ IRB.initialize(nil)
80
+ IRB.parse_opts
81
+ IRB.load_modules
82
+ else
83
+ IRB.setup(nil)
84
+ end
85
+ IRB.restart(im)
86
+ end
87
+
88
+ def IRB.restart(im)
89
+ irb = Irb.new(nil, im)
90
+
91
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
92
+ @CONF[:MAIN_CONTEXT] = irb.context
93
+ trap("SIGINT") do
94
+ irb.signal_handle
95
+ end
96
+
97
+ catch(:IRB_EXIT) do
98
+ irb.eval_input
99
+ end
100
+ print "\n"
101
+ end
102
+ end
103
+
104
+ class FXIrb < FXText
105
+ include Singleton
106
+ include Responder
107
+
108
+ attr_reader :input
109
+
110
+ def FXIrb.init(p, tgt, sel, opts)
111
+ unless @__instance__
112
+ Thread.critical = true
113
+ begin
114
+ @__instance__ ||= new(p, tgt, sel, opts)
115
+ ensure
116
+ Thread.critical = false
117
+ end
118
+ end
119
+ return @__instance__
120
+ end
121
+
122
+ def initialize(p, tgt, sel, opts)
123
+ @parent = p
124
+ FXMAPFUNC(SEL_KEYRELEASE, 0, "onKeyRelease")
125
+ FXMAPFUNC(SEL_KEYPRESS, 0, "onKeyPress")
126
+ FXMAPFUNC(SEL_LEFTBUTTONPRESS,0,"onLeftBtnPress")
127
+ FXMAPFUNC(SEL_MIDDLEBUTTONPRESS,0,"onMiddleBtnPress")
128
+ FXMAPFUNC(SEL_LEFTBUTTONRELEASE,0,"onLeftBtnRelease")
129
+
130
+ super
131
+ setFont(FXFont.new(FXApp.instance, "lucida console", 9))
132
+ end
133
+
134
+ def create
135
+ super
136
+ restart
137
+ end
138
+
139
+ def restart
140
+ setFocus
141
+ setText("")
142
+ # IRB initialization
143
+ @inputAdded = false
144
+ @input = IO.pipe
145
+ $DEFAULT_OUTPUT = self
146
+
147
+ @im = FXIRBInputMethod.new
148
+ if @irb
149
+ @irb.kill
150
+ @irb = Thread.new {
151
+ IRB.restart(@im)
152
+ }
153
+ else
154
+ @irb = Thread.new {
155
+ IRB.start_in_fxirb(@im)
156
+ }
157
+ end
158
+ end
159
+
160
+
161
+ def onKeyRelease(sender, sel, event)
162
+ case event.code
163
+ when Fox::KEY_Return, Fox::KEY_KP_Enter
164
+ newLineEntered
165
+ end
166
+ return 1
167
+ end
168
+
169
+ def onKeyPress(sender,sel,event)
170
+ case event.code
171
+ when Fox::KEY_Return, Fox::KEY_KP_Enter
172
+ setCursorPos(getLength)
173
+ super
174
+ when Fox::KEY_Up,Fox::KEY_KP_Up
175
+ str = @im.prevCmd.chop
176
+ if str != ""
177
+ removeText(@anchor, getLength-@anchor)
178
+ write(str)
179
+ end
180
+ when Fox::KEY_Down,Fox::KEY_KP_Down
181
+ str = @im.nextCmd.chop
182
+ if str != ""
183
+ removeText(@anchor, getLength-@anchor)
184
+ write(str)
185
+ end
186
+ when Fox::KEY_Left,Fox::KEY_KP_Left
187
+ if getCursorPos > @anchor
188
+ super
189
+ end
190
+ when Fox::KEY_Delete,Fox::KEY_KP_Delete,Fox::KEY_BackSpace
191
+ if getCursorPos > @anchor
192
+ super
193
+ end
194
+ when Fox::KEY_Home, Fox::KEY_KP_Home
195
+ setCursorPos(@anchor)
196
+ when Fox::KEY_End, Fox::KEY_KP_End
197
+ setCursorPos(getLength)
198
+ when Fox::KEY_Page_Up, Fox::KEY_KP_Page_Up,
199
+ Fox::KEY_Page_Down, Fox::KEY_KP_Page_Down
200
+ when Fox::KEY_bracketright
201
+ #auto-dedent if the } or ] is on a line by itself
202
+ if (emptyline? or (getline == "en")) and indented?
203
+ str = getline
204
+ @anchor -= 2
205
+ rmline
206
+ appendText(str)
207
+ setCursorPos(getLength)
208
+ end
209
+ super
210
+ when Fox::KEY_u
211
+ if (event.state & CONTROLMASK) != 0
212
+ str = extractText(getCursorPos, getLength - getCursorPos)
213
+ rmline
214
+ appendText(str)
215
+ setCursorPos(@anchor)
216
+ end
217
+ super
218
+ when Fox::KEY_k
219
+ if (event.state & CONTROLMASK) != 0
220
+ str = extractText(@anchor, getCursorPos-@anchor)
221
+ rmline
222
+ appendText(str)
223
+ setCursorPos(getLength)
224
+ end
225
+ super
226
+ when Fox::KEY_d
227
+ if (event.state & CONTROLMASK) != 0
228
+ restart
229
+ =begin
230
+ #Ctrl - D
231
+ rmline
232
+ appendText(exit)
233
+ sendCommand("exit")
234
+ newLineEntered
235
+ =end
236
+ else
237
+ # test for 'end' so we can dedent
238
+ if (getline == "en") and indented?
239
+ str = getline
240
+ @anchor -= 2
241
+ rmline
242
+ appendText(str)
243
+ setCursorPos(getLength)
244
+ end
245
+ end
246
+ super
247
+ else
248
+ super
249
+ end
250
+ end
251
+
252
+ def getline
253
+ extractText(@anchor, getLength-@anchor)
254
+ end
255
+
256
+ def rmline
257
+ str = getline
258
+ removeText(@anchor, getLength-@anchor)
259
+ str
260
+ end
261
+
262
+ def emptyline?
263
+ getline == ""
264
+ end
265
+
266
+ def indented?
267
+ extractText(@anchor-2, 2) == " "
268
+ end
269
+
270
+ def onLeftBtnPress(sender,sel,event)
271
+ @store_anchor = @anchor
272
+ setFocus
273
+ super
274
+ end
275
+
276
+ def onLeftBtnRelease(sender,sel,event)
277
+ super
278
+ @anchor = @store_anchor
279
+ setCursorPos(@anchor)
280
+ setCursorPos(getLength)
281
+ end
282
+
283
+ def onMiddleBtnPress(sender,sel,event)
284
+ pos=getPosAt(event.win_x,event.win_y)
285
+ if pos >= @anchor
286
+ super
287
+ end
288
+ end
289
+
290
+ def newLineEntered
291
+ processCommandLine(extractText(@anchor, getLength-@anchor))
292
+ end
293
+
294
+ def processCommandLine(cmd)
295
+ #write("[#{cmd}]")
296
+ @input[1].puts cmd
297
+ @inputAdded = true
298
+ @irb.run
299
+ end
300
+
301
+ def sendCommand(cmd)
302
+ setCursorPos(getLength)
303
+ makePositionVisible(getLength) unless isPosVisible(getLength)
304
+ cmd += "\n"
305
+ appendText(cmd)
306
+ processCommandLine(cmd)
307
+ end
308
+
309
+ def write(obj)
310
+ str = obj.to_s
311
+ appendText(str)
312
+ setCursorPos(getLength)
313
+ makePositionVisible(getLength) unless isPosVisible(getLength)
314
+ return str.length
315
+ end
316
+
317
+ def gets
318
+ @anchor = getLength
319
+ if !@inputAdded
320
+ Thread.stop
321
+ end
322
+ @inputAdded = false
323
+ return @input[0].gets
324
+ end
325
+ end
326
+
327
+ # Stand alone run
328
+ if __FILE__ == $0
329
+ application = FXApp.new("FXIrb", "ruby")
330
+ application.threadsEnabled = true
331
+ Thread.abort_on_exception = true
332
+ application.init(ARGV)
333
+ window = FXMainWindow.new(application, "FXIrb", nil, nil, DECOR_ALL, 0, 0, 580, 500)
334
+ FXIrb.init(window, nil, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y|TEXT_WORDWRAP|TEXT_SHOWACTIVE)
335
+ application.create
336
+ window.show(PLACEMENT_SCREEN)
337
+ application.run
338
+ end
339
+