* add todo * refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944) * extract DirectoryLinkNode and FileLinkNode, move Node methods to children * temporarily move DirectoryNode methods into BaseNode for easier reviewing * move mostly unchanged DirectoryNode methods back to BaseNode * tidy * git.git_status_file takes an array * update git status of links * luacheck hack * safer git_status_dir * refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957) move expand_or_collapse to DirectoryNode * refactor(#2886): multi instance: node group functions refactoring (#2959) * move last_group_node to DirectoryNode * move add BaseNode:as and more doc * revert parameter name changes * revert parameter name changes * add Class * move group methods into DN * tidy group methods * tidy group methods * tidy group methods * tidy group methods * parent is DirectoryNode * tidy expand all * BaseNode -> Node * move watcher to DirectoryNode * last_group_node is DirectoryNode only * simplify create-file * simplify parent * simplify collapse-all * simplify live-filter * style * move lib.get_cursor_position to Explorer * move lib.get_node_at_cursor to Explorer * move lib.get_nodes to Explorer * move place_cursor_on_node to Explorer * resolve resource leak in purge_all_state * move many autocommands into Explorer * post merge tidy
120 lines
3.2 KiB
Lua
120 lines
3.2 KiB
Lua
local M = {
|
|
config = nil,
|
|
path = nil,
|
|
}
|
|
|
|
--- Write to log file
|
|
---@param typ string as per log.types config
|
|
---@param fmt string for string.format
|
|
---@param ... any arguments for string.format
|
|
function M.raw(typ, fmt, ...)
|
|
if not M.enabled(typ) then
|
|
return
|
|
end
|
|
|
|
local line = string.format(fmt, ...)
|
|
local file = io.open(M.path, "a")
|
|
if file then
|
|
io.output(file)
|
|
io.write(line)
|
|
io.close(file)
|
|
end
|
|
end
|
|
|
|
--- Write to a new file
|
|
---@param typ string as per log.types config
|
|
---@param path string absolute path
|
|
---@param fmt string for string.format
|
|
---@param ... any arguments for string.format
|
|
function M.file(typ, path, fmt, ...)
|
|
if not M.enabled(typ) then
|
|
return
|
|
end
|
|
|
|
local line = string.format(fmt, ...)
|
|
local file = io.open(path, "w")
|
|
if file then
|
|
io.output(file)
|
|
io.write(line)
|
|
io.close(file)
|
|
end
|
|
end
|
|
|
|
---@class Profile
|
|
---@field start number nanos
|
|
---@field tag string
|
|
|
|
--- Write profile start to log file
|
|
--- START is prefixed
|
|
---@param fmt string for string.format
|
|
---@param ... any arguments for string.format
|
|
---@return Profile to pass to profile_end
|
|
function M.profile_start(fmt, ...)
|
|
local profile = {}
|
|
if M.enabled("profile") then
|
|
profile.start = vim.loop.hrtime()
|
|
profile.tag = string.format((fmt or "???"), ...)
|
|
M.line("profile", "START %s", profile.tag)
|
|
end
|
|
return profile
|
|
end
|
|
|
|
--- Write profile end to log file
|
|
--- END is prefixed and duration in seconds is suffixed
|
|
---@param profile Profile returned from profile_start
|
|
function M.profile_end(profile)
|
|
if M.enabled("profile") and type(profile) == "table" then
|
|
local millis = profile.start and math.modf((vim.loop.hrtime() - profile.start) / 1000000) or -1
|
|
M.line("profile", "END %s %dms", profile.tag or "", millis)
|
|
end
|
|
end
|
|
|
|
--- Write to log file
|
|
--- time and typ are prefixed and a trailing newline is added
|
|
---@param typ string as per log.types config
|
|
---@param fmt string for string.format
|
|
---@param ... any arguments for string.format
|
|
function M.line(typ, fmt, ...)
|
|
if M.enabled(typ) then
|
|
M.raw(typ, string.format("[%s] [%s] %s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???")), ...)
|
|
end
|
|
end
|
|
|
|
local inspect_opts = {}
|
|
|
|
---@param opts table
|
|
function M.set_inspect_opts(opts)
|
|
inspect_opts = opts
|
|
end
|
|
|
|
--- Write to log file the inspection of a node
|
|
---@param typ string as per log.types config
|
|
---@param node Node node to be inspected
|
|
---@param fmt string for string.format
|
|
---@param ... any arguments for string.format
|
|
function M.node(typ, node, fmt, ...)
|
|
if M.enabled(typ) then
|
|
M.raw(typ, string.format("[%s] [%s] %s\n%s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???"), vim.inspect(node, inspect_opts)), ...)
|
|
end
|
|
end
|
|
|
|
--- Logging is enabled for typ or all
|
|
---@param typ string as per log.types config
|
|
---@return boolean
|
|
function M.enabled(typ)
|
|
return M.path ~= nil and (M.config.types[typ] or M.config.types.all)
|
|
end
|
|
|
|
function M.setup(opts)
|
|
M.config = opts.log
|
|
if M.config and M.config.enable and M.config.types then
|
|
M.path = string.format("%s/nvim-tree.log", vim.fn.stdpath("log"), os.date("%H:%M:%S"), vim.env.USER)
|
|
if M.config.truncate then
|
|
os.remove(M.path)
|
|
end
|
|
require("nvim-tree.notify").debug("nvim-tree.lua logging to " .. M.path)
|
|
end
|
|
end
|
|
|
|
return M
|