-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
errhand.lua
126 lines (109 loc) · 2.78 KB
/
errhand.lua
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
local inspect = require("inspect")
local utf8validate = require("utf8validate")
local just = require("just")
local imgui = require("imgui")
local typecheck = require("typecheck")
local errhand = love.errhand
local message = ""
local trace = ""
local scrollY = 0
local function draw()
local w, h = love.graphics.getDimensions()
love.graphics.origin()
love.graphics.clear(0.1, 0.1, 0.1, 1)
love.graphics.setColor(1, 1, 1, 1)
local _h = 40
imgui.setSize(w, h, w / 2, _h)
local h0 = just.height
just.row(true)
if imgui.button("close", "close") then
love.event.quit()
end
if imgui.button("copy", "copy") then
love.system.setClipboardText(message .. "\n" .. trace)
end
if imgui.button("restart", "restart") then
love.event.quit("restart")
end
if imgui.button("restart+check", "restart and check integrity") then
love.filesystem.remove("userdata/files.lua")
love.event.quit("restart")
end
if imgui.button("vscode", "open in vscode") then
local line = message:match("^[^\n]+")
local last_file
line:gsub("([^:]+:[^:]+):", function(file)
last_file = file
end)
print("code -g " .. last_file)
os.execute("code -g " .. last_file)
love.event.quit()
end
just.offset(w - 70)
if imgui.button("error", "error") then
error(message .. "\n" .. trace)
end
just.row()
imgui.Container("error container", w, h - (just.height - h0), _h / 3, _h, scrollY)
just.indent(10)
just.text(message .. "\n" .. trace, w - _h)
scrollY = imgui.Container()
end
local handlers = {}
---@param key string
---@param ... any?
function handlers.keypressed(key, ...)
if key == "escape" then
love.event.quit()
elseif key == "c" and love.keyboard.isDown("lctrl", "rctrl") then
love.system.setClipboardText(message .. "\n" .. trace)
end
end
---@return number|string?
local function run()
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
return a or 1
end
local icb = just.callbacks[name]
local skip = icb and icb(a,b,c,d,e,f)
if not skip and handlers[name] then
handlers[name](a,b,c,d,e,f)
end
end
if love.graphics.isActive() then
draw()
just._end()
love.graphics.present()
end
if love.timer then
love.timer.sleep(0.001)
end
end
return function(msg)
if type(msg) ~= "string" then
msg = inspect(msg)
end
message = utf8validate(msg)
trace = debug.traceback()
message = typecheck.fix_traceback(message)
trace = typecheck.fix_traceback(trace)
print(message .. "\n" .. trace)
love.filesystem.write("userdata/lasterror.txt", message .. "\n" .. trace)
if love.graphics and love.graphics.isActive() then
love.graphics.reset()
love.window.setVSync(1)
just.reset()
else
love.window.setMode(800, 600)
end
return function()
local status, err = pcall(run)
if not status then
run = errhand(err)
else
return err
end
end
end