Last active
August 29, 2015 14:17
-
-
Save vladimir-kotikov/5e7ccb6aa663f20e075f to your computer and use it in GitHub Desktop.
Clink prompt filetring profiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clink.filter_prompt(prompt) | |
local function add_ansi_codes(p) | |
local c = tonumber(clink.get_setting_int("prompt_colour")) | |
if c < 0 then | |
return p | |
end | |
c = c % 16 | |
--[[ | |
<4 >=4 %2 | |
0 0 0 Black 4 1 -3 Blue 0 | |
1 4 3 Red 5 5 0 Magenta 1 | |
2 2 0 Green 6 3 -3 Cyan 0 | |
3 6 3 Yellow 7 7 0 Gray 1 | |
--]] | |
-- Convert from cmd.exe colour indices to ANSI ones. | |
local colour_id = c % 8 | |
if (colour_id % 2) == 1 then | |
if colour_id < 4 then | |
c = c + 3 | |
end | |
elseif colour_id >= 4 then | |
c = c - 3 | |
end | |
-- Clamp | |
if c > 15 then | |
c = 15 | |
end | |
-- Build ANSI code | |
local code = "\x1b[0;" | |
if c > 7 then | |
c = c - 8 | |
code = code.."1;" | |
end | |
code = code..(c + 30).."m" | |
return code..p.."\x1b[0m" | |
end | |
local calls, total, this = {}, {}, {} | |
local function profile_hook(event) | |
local i = debug.getinfo(2, "Sln") | |
if i.what ~= 'Lua' then return end | |
local func = (i.name or "")..":"..(i.source..' : '..i.linedefined) | |
if event == 'call' then | |
this[func] = os.clock() | |
else | |
if this[func] then | |
local time = os.clock() - this[func] | |
total[func] = (total[func] or 0) + time | |
calls[func] = (calls[func] or 0) + 1 | |
end | |
end | |
end | |
local function unset_profile_hook() | |
for f,time in pairs(total) do | |
print(("Function %s took %.3f seconds after %d calls"):format(f, time, calls[f])) | |
end | |
debug.sethook() | |
end | |
clink.prompt.value = prompt | |
debug.sethook(profile_hook, "cr") | |
for _, filter in ipairs(clink.prompt.filters) do | |
if filter.f() == true then | |
unset_profile_hook() | |
return add_ansi_codes(clink.prompt.value) | |
end | |
end | |
unset_profile_hook() | |
return add_ansi_codes(clink.prompt.value) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment