fix(cmd): only call supported lua functions

This commit is contained in:
Jan De Dobbeleer 2021-11-13 21:24:52 +01:00
parent e0c31470b7
commit 2a44dca898
No known key found for this signature in database
GPG key ID: F6CC273CE5BA9AEE

View file

@ -3,10 +3,21 @@
local endedit_time
local last_duration
local function os_clock_millis()
-- Clink v1.2.30 has a fix for Lua's os.clock() implementation failing after
-- the program has been running more than 24 days. In older versions, call
-- OMP to get the time in milliseconds.
if (clink.version_encoded or 0) >= 10020030 then
return math.floor(os.clock() * 1000)
else
return io.popen("::OMP:: --millis"):read("*n")
end
end
local function duration_onbeginedit()
last_duration = 0
if endedit_time then
local beginedit_time = io.popen("::OMP:: --millis"):read("*n")
local beginedit_time = os_clock_millis()
local elapsed = beginedit_time - endedit_time
if elapsed >= 0 then
last_duration = elapsed
@ -15,13 +26,27 @@ local function duration_onbeginedit()
end
local function duration_onendedit()
endedit_time = io.popen("::OMP:: --millis"):read("*n")
endedit_time = os_clock_millis()
end
-- Prompt functions
local function execution_time_option()
if last_duration ~= nil then
return "--execution-time "..last_duration
end
return ""
end
local function error_level_option()
if os.geterrorlevel ~= nil and settings.get("cmd.get_errorlevel") then
return "--error "..os.geterrorlevel()
end
return ""
end
local function get_posh_prompt(rprompt)
local prompt_exe = string.format("::OMP:: --config=\"::CONFIG::\" --execution-time %s --error %s --rprompt=%s", last_duration, os.geterrorlevel(), rprompt)
local prompt_exe = string.format('::OMP:: --config="::CONFIG::" %s %s --rprompt=%s', execution_time_option(), error_level_option(), rprompt)
prompt = io.popen(prompt_exe):read("*a")
return prompt
end
@ -45,6 +70,7 @@ local function builtin_modules_onendedit()
duration_onendedit()
end
clink.onbeginedit(builtin_modules_onbeginedit)
clink.onendedit(builtin_modules_onendedit)
if clink.onbeginedit ~= nil and clink.onendedit ~= nil then
clink.onbeginedit(builtin_modules_onbeginedit)
clink.onendedit(builtin_modules_onendedit)
end