Files
text-transform.nvim/lua/text-transform/util/debug.lua
2024-05-05 23:02:08 +03:00

68 lines
1.4 KiB
Lua

local D = {}
local function is_debug()
return _G.TextTransform ~= nil
and _G.TextTransform.config ~= nil
and _G.TextTransform.config.debug
end
---prints only if debug is true.
---
---@param scope string: the scope from where this function is called.
---@param str string: the formatted string.
---@param ... any: the arguments of the formatted string.
---@private
function D.log(scope, str, ...)
if not is_debug() then
return
end
local info = debug.getinfo(2, "Sl")
local line = ""
if info then
line = "L" .. info.currentline
end
print(
string.format(
"%s [text-transform:%s in %s] > %s",
os.date("%H:%M:%S"),
scope,
line,
string.format(str, ...)
)
)
end
---prints the table if debug is true.
---
---@param table table: the table to print.
---@param indent number?: the default indent value, starts at 0.
---@private
function D.tprint(table, indent)
if not is_debug() then
return
end
if not indent then
indent = 0
end
for k, v in pairs(table) do
local formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
D.tprint(v, indent + 1)
elseif type(v) == "boolean" then
print(formatting .. tostring(v))
elseif type(v) == "function" then
print(formatting .. "FUNCTION")
else
print(formatting .. v)
end
end
end
return D