Comparison

util/helpers.lua @ 4705:447f5a94792d

util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Apr 2012 06:58:21 +0100
parent 4681:3299223bbed5
child 5415:9e7aa5b47bdf
comparison
equal deleted inserted replaced
4704:d6c4e58333cf 4705:447f5a94792d
31 31
32 function revert_log_events(events) 32 function revert_log_events(events)
33 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :)) 33 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
34 end 34 end
35 35
36 function show_events(events) 36 function show_events(events, specific_event)
37 local event_handlers = events._handlers; 37 local event_handlers = events._handlers;
38 local events_array = {}; 38 local events_array = {};
39 local event_handler_arrays = {}; 39 local event_handler_arrays = {};
40 for event in pairs(events._event_map) do 40 for event in pairs(events._event_map) do
41 local handlers = event_handlers[event]; 41 local handlers = event_handlers[event];
42 table.insert(events_array, event); 42 if handlers and (event == specific_event or not specific_event) then
43 local handler_strings = {}; 43 table.insert(events_array, event);
44 for i, handler in ipairs(handlers) do 44 local handler_strings = {};
45 local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler)); 45 for i, handler in ipairs(handlers) do
46 handler_strings[i] = " "..i..": "..tostring(handler)..(upvals and ("\n "..upvals) or ""); 46 local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler));
47 handler_strings[i] = " "..i..": "..tostring(handler)..(upvals and ("\n "..upvals) or "");
48 end
49 event_handler_arrays[event] = handler_strings;
47 end 50 end
48 event_handler_arrays[event] = handler_strings;
49 end 51 end
50 table.sort(events_array); 52 table.sort(events_array);
51 local i = 1; 53 local i = 1;
52 repeat 54 while i <= #events_array do
53 local handlers = event_handler_arrays[events_array[i]]; 55 local handlers = event_handler_arrays[events_array[i]];
54 for j=#handlers, 1, -1 do 56 for j=#handlers, 1, -1 do
55 table.insert(events_array, i+1, handlers[j]); 57 table.insert(events_array, i+1, handlers[j]);
56 end 58 end
57 if i > 1 then events_array[i] = "\n"..events_array[i]; end 59 if i > 1 then events_array[i] = "\n"..events_array[i]; end
58 i = i + #handlers + 1 60 i = i + #handlers + 1
59 until i == #events_array; 61 end
60 return table.concat(events_array, "\n"); 62 return table.concat(events_array, "\n");
61 end 63 end
62 64
63 function get_upvalue(f, get_name) 65 function get_upvalue(f, get_name)
64 local i, name, value = 0; 66 local i, name, value = 0;