-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbg.lua
100 lines (92 loc) · 3.09 KB
/
dbg.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
--{{{ environment
local type = type
local tostring = tostring
local table = table
local math = math
local pairs = pairs
local ipairs = ipairs
local io = io
local setmetatable = setmetatable
local getmetatable = getmetatable
local string = string
local naughty = require("naughty")
local screen = screen
module("dbg")
quiet = false
colors = {
header = "#FF004D",
count = "#333333",
index = "#553333",
name = "#8F8870",
}
--- }}}
--{{{ dbg_get
function dbg_get(var, depth, indent)
local a = ""
local text = ""
local name = ""
local vtype = type(var)
local vstring = tostring(var)
if vtype == "table" or vtype == "userdata" then
if vtype == "userdata" then var = getmetatable(var) end
-- element count and longest key
local count = 0
local longest_key = 3
for k,v in pairs(var) do
count = count + 1
longest_key = math.max(#tostring(k), longest_key)
end
text = text .. vstring .. " <span color='"..colors.count.."'>#" .. count .. "</span>"
-- descend a table
if depth > 0 then
-- sort keys FIXME: messes up sorting number
local sorted = {}
for k, v in pairs(var) do table.insert(sorted, { k, v }) end
table.sort(sorted, function(a, b) return tostring(a[1]) < tostring(b[1]) end)
-- go through elements
for _, p in ipairs(sorted) do
local key = p[1]; local value = p[2]
-- don't descend _M
local d; if key ~= "_M" then d = depth - 1 else d = 0 end
-- get content and add to output
local content = dbg_get(value, d, indent + longest_key + 1)
text = text .. '\n' .. string.rep(" ", indent) ..
string.format("<span color='"..colors.index.."'>%-"..longest_key.."s</span> %s",
tostring(key), content)
end
end
elseif vtype == "tag" or vtype == "client" then
text = text .. vstring .. " [<span color='"..colors.name.."'>" .. var.name:sub(1,10) .. "</span>]"
elseif vtype == "widget" and var.text then
text = text .. vstring .. " [<span color='"..colors.name.."'>" .. var.text:sub(1,10) .. "</span>]"
elseif vtype == "string" then
text = text .. '<i>' .. vstring:sub(1,100) .. '</i>'
else
text = text .. vstring
end
return text
end
-- }}}
--{{{ dbg
function dbg(vars)
if quiet then return end
local num = table.maxn(vars)
local text = "<span color='"..colors.header.."'>dbg</span> <span color='"..colors.count.."'>#"..num.."</span>"
local depth = vars.d or 1
for i = 1, num do
local desc = dbg_get(vars[i], depth, 3)
text = text .. string.format("\n<span color='"..colors.index.."'>%2d</span> %s", i, desc)
end
if vars.err then
text = text:gsub("<.->", "").."\n===\n"
text = text:gsub(">", ">")
text = text:gsub("<", "<")
text = text:gsub("&", "&")
io.stderr:write(text)
else
naughty.notify{ text = text, timeout = 0, hover_timeout = 0.05, screen = screen.count() }
end
end
--}}}
setmetatable(_M, { __call = function (_, ...) return dbg(...) end })
-- vim: foldmethod=marker:filetype=lua:expandtab:shiftwidth=2:tabstop=2:softtabstop=2:encoding=utf-8:textwidth=80