feat(notify): add notify.threshold (#1693)

* feat: configurable notification level

add `notify.threshold` to setup opts

* feat: configurable notification level: add threshold example doc

* feat: configurable notification level: log always comes last

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
kylo252
2022-11-01 00:24:40 +01:00
committed by GitHub
parent ada2c6441d
commit 6ca6f99e76
19 changed files with 120 additions and 66 deletions

View File

@@ -4,6 +4,7 @@ local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local notify = require "nvim-tree.notify"
local M = {}
@@ -81,14 +82,14 @@ local function do_single_paste(source, dest, action_type, action_fn)
dest_stats, errmsg, errcode = uv.fs_stat(dest)
if not dest_stats and errcode ~= "ENOENT" then
utils.notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end
local function on_process()
success, errmsg = action_fn(source, dest)
if not success then
utils.notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end
end
@@ -122,11 +123,11 @@ local function add_to_clipboard(node, clip)
for idx, _node in ipairs(clip) do
if _node.absolute_path == node.absolute_path then
table.remove(clip, idx)
return utils.notify.info(node.absolute_path .. " removed to clipboard.")
return notify.info(node.absolute_path .. " removed to clipboard.")
end
end
table.insert(clip, node)
utils.notify.info(node.absolute_path .. " added to clipboard.")
notify.info(node.absolute_path .. " added to clipboard.")
end
function M.clear_clipboard()
@@ -157,7 +158,7 @@ local function do_paste(node, action_type, action_fn)
local stats, errmsg, errcode = uv.fs_stat(destination)
if not stats and errcode ~= "ENOENT" then
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
utils.notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
return
end
local is_dir = stats and stats.type == "directory"
@@ -219,18 +220,18 @@ function M.print_clipboard()
end
end
return utils.notify.info(table.concat(content, "\n") .. "\n")
return notify.info(table.concat(content, "\n") .. "\n")
end
local function copy_to_clipboard(content)
if M.use_system_clipboard == true then
vim.fn.setreg("+", content)
vim.fn.setreg('"', content)
return utils.notify.info(string.format("Copied %s to system clipboard!", content))
return notify.info(string.format("Copied %s to system clipboard!", content))
else
vim.fn.setreg('"', content)
vim.fn.setreg("1", content)
return utils.notify.info(string.format("Copied %s to neovim clipboard!", content))
return notify.info(string.format("Copied %s to neovim clipboard!", content))
end
end

View File

@@ -5,13 +5,14 @@ local events = require "nvim-tree.events"
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local watch = require "nvim-tree.explorer.watch"
local notify = require "nvim-tree.notify"
local M = {}
local function create_and_notify(file)
local ok, fd = pcall(uv.fs_open, file, "w", 420)
if not ok then
utils.notify.error("Couldn't create file " .. file)
notify.error("Couldn't create file " .. file)
return
end
uv.fs_close(fd)
@@ -71,7 +72,7 @@ function M.fn(node)
end
if utils.file_exists(new_file_path) then
utils.notify.warn "Cannot create: file already exists"
notify.warn "Cannot create: file already exists"
return
end
@@ -96,14 +97,14 @@ function M.fn(node)
elseif not utils.file_exists(path_to_create) then
local success = uv.fs_mkdir(path_to_create, 493)
if not success then
utils.notify.error("Could not create folder " .. path_to_create)
notify.error("Could not create folder " .. path_to_create)
is_error = true
break
end
end
end
if not is_error then
utils.notify.info(new_file_path .. " was properly created")
notify.info(new_file_path .. " was properly created")
end
events._dispatch_folder_created(new_file_path)
if M.enable_reload then

View File

@@ -5,6 +5,7 @@ local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local view = require "nvim-tree.view"
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"
local M = {}
@@ -44,7 +45,7 @@ end
local function remove_dir(cwd)
local handle = luv.fs_scandir(cwd)
if type(handle) == "string" then
return utils.notify.error(handle)
return notify.error(handle)
end
while true do
@@ -83,18 +84,18 @@ function M.fn(node)
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return utils.notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. node.name)
end
events._dispatch_folder_removed(node.absolute_path)
else
local success = luv.fs_unlink(node.absolute_path)
if not success then
return utils.notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. node.name)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
utils.notify.info(node.absolute_path .. " was properly removed.")
notify.info(node.absolute_path .. " was properly removed.")
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end

View File

@@ -3,6 +3,7 @@ local uv = vim.loop
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"
local M = {}
@@ -12,15 +13,15 @@ end
function M.rename(node, to)
if utils.file_exists(to) then
utils.notify.warn(err_fmt(node.absolute_path, to, "file already exists"))
notify.warn(err_fmt(node.absolute_path, to, "file already exists"))
return
end
local success, err = uv.fs_rename(node.absolute_path, to)
if not success then
return utils.notify.warn(err_fmt(node.absolute_path, to, err))
return notify.warn(err_fmt(node.absolute_path, to, err))
end
utils.notify.info(node.absolute_path .. "" .. to)
notify.info(node.absolute_path .. "" .. to)
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end

View File

@@ -1,6 +1,7 @@
local a = vim.api
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"
local M = {
config = {
@@ -43,13 +44,13 @@ function M.fn(node)
M.config.trash.require_confirm = true
end
else
utils.notify.warn "Trash is currently a UNIX only feature!"
notify.warn "Trash is currently a UNIX only feature!"
return
end
local binary = M.config.trash.cmd:gsub(" .*$", "")
if vim.fn.executable(binary) == 0 then
utils.notify.warn(binary .. " is not executable.")
notify.warn(binary .. " is not executable.")
return
end
@@ -71,7 +72,7 @@ function M.fn(node)
if node.nodes ~= nil and not node.link_to then
trash_path(function(_, rc)
if rc ~= 0 then
utils.notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_folder_removed(node.absolute_path)
@@ -82,7 +83,7 @@ function M.fn(node)
else
trash_path(function(_, rc)
if rc ~= 0 then
utils.notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_file_removed(node.absolute_path)

View File

@@ -4,7 +4,7 @@ local a = vim.api
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local util = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"
-- BEGIN_DEFAULT_MAPPINGS
local DEFAULT_MAPPINGS = {
@@ -307,7 +307,7 @@ local function merge_mappings(user_mappings)
if not is_empty(map.action) then
M.custom_keypress_funcs[map.action] = map.action_cb
else
util.notify.warn "action can't be empty if action_cb provided"
notify.warn "action can't be empty if action_cb provided"
end
end
end

View File

@@ -1,7 +1,7 @@
local core = require "nvim-tree.core"
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
local notify = require "nvim-tree.notify"
local M = {}
@@ -58,7 +58,7 @@ end
function M.fn(base_node)
local node = base_node.nodes and base_node or core.get_explorer()
if gen_iterator()(node) then
utils.notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
end
renderer.draw()
end

View File

@@ -1,4 +1,4 @@
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"
local M = {}
@@ -30,7 +30,7 @@ local function dispatch(event_name, payload)
for _, handler in pairs(get_handlers(event_name)) do
local success, error = pcall(handler, payload)
if not success then
utils.notify.error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error))
notify.error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error))
end
end
end

View File

@@ -6,6 +6,7 @@ local common = require "nvim-tree.explorer.common"
local sorters = require "nvim-tree.explorer.sorters"
local filters = require "nvim-tree.explorer.filters"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"
local M = {}
@@ -52,7 +53,7 @@ end
local function get_dir_handle(cwd)
local handle = uv.fs_scandir(cwd)
if type(handle) == "string" then
utils.notify.error(handle)
notify.error(handle)
return
end
return handle

View File

@@ -6,6 +6,7 @@ local common = require "nvim-tree.explorer.common"
local filters = require "nvim-tree.explorer.filters"
local sorters = require "nvim-tree.explorer.sorters"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"
local M = {}
@@ -22,7 +23,7 @@ function M.reload(node, status)
local cwd = node.link_to or node.absolute_path
local handle = uv.fs_scandir(cwd)
if type(handle) == "string" then
utils.notify.error(handle)
notify.error(handle)
return
end

View File

@@ -1,4 +1,5 @@
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"
local M = {}
@@ -292,12 +293,12 @@ end
local function removed(opts)
if opts.auto_close then
utils.notify.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
notify.warn "auto close feature has been removed, see note in the README (tips & reminder section)"
opts.auto_close = nil
end
if opts.focus_empty_on_setup then
utils.notify.warn "focus_empty_on_setup has been removed and will be replaced by a new startup configuration. Please remove this option. See https://bit.ly/3yJch2T"
notify.warn "focus_empty_on_setup has been removed and will be replaced by a new startup configuration. Please remove this option. See https://bit.ly/3yJch2T"
end
opts.focus_empty_on_setup = nil
end
@@ -312,7 +313,7 @@ function M.migrate_legacy_options(opts)
end
end
if msg then
utils.notify.warn(msg)
notify.warn(msg)
end
-- silently move

View File

@@ -56,7 +56,7 @@ function M.setup(opts)
if M.config.truncate then
os.remove(M.path)
end
require("nvim-tree.utils").notify.debug("nvim-tree.lua logging to " .. M.path)
require("nvim-tree.notify").debug("nvim-tree.lua logging to " .. M.path)
end
end

View File

@@ -2,12 +2,13 @@ local Marks = require "nvim-tree.marks"
local Core = require "nvim-tree.core"
local utils = require "nvim-tree.utils"
local FsRename = require "nvim-tree.actions.fs.rename-file"
local notify = require "nvim-tree.notify"
local M = {}
function M.bulk_move()
if #Marks.get_marks() == 0 then
utils.notify.warn "no bookmark to perform bulk move on, aborting."
notify.warn "no bookmark to perform bulk move on, aborting."
return
end
@@ -17,7 +18,7 @@ function M.bulk_move()
return
end
if vim.fn.filewritable(location) ~= 2 then
utils.notify.warn(location .. " is not writable, cannot move.")
notify.warn(location .. " is not writable, cannot move.")
return
end

44
lua/nvim-tree/notify.lua Normal file
View File

@@ -0,0 +1,44 @@
local M = {}
local config = {
threshold = vim.log.levels.INFO,
}
local modes = {
{ name = "trace", level = vim.log.levels.TRACE },
{ name = "debug", level = vim.log.levels.DEBUG },
{ name = "info", level = vim.log.levels.INFO },
{ name = "warn", level = vim.log.levels.WARN },
{ name = "error", level = vim.log.levels.ERROR },
}
do
local has_notify, notify_plugin = pcall(require, "notify")
local dispatch = function(level, msg)
if level < config.threshold then
return
end
vim.schedule(function()
if has_notify and notify_plugin then
notify_plugin(msg, level, { title = "NvimTree" })
else
vim.notify("[NvimTree] " .. msg, level)
end
end)
end
for _, x in ipairs(modes) do
M[x.name] = function(msg, ...)
return dispatch(x.level, msg, ...)
end
end
end
function M.setup(opts)
opts = opts or {}
config.threshold = opts.notify.threshold or vim.log.levels.INFO
end
return M

View File

@@ -1,4 +1,4 @@
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"
local M = {
SIGN_GROUP = "NvimTreeGitSigns",
@@ -68,7 +68,7 @@ end
local function nil_() end
local function warn_status(git_status)
utils.notify.warn(
notify.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/nvim-tree/nvim-tree.lua/issues with this message.'

View File

@@ -1,5 +1,3 @@
local has_notify, notify = pcall(require, "notify")
local a = vim.api
local uv = vim.loop
@@ -16,24 +14,6 @@ function M.path_to_matching_str(path)
return path:gsub("(%-)", "(%%-)"):gsub("(%.)", "(%%.)"):gsub("(%_)", "(%%_)")
end
local function notify_level(level)
return function(msg)
vim.schedule(function()
if has_notify then
notify(msg, level, { title = "NvimTree" })
else
vim.notify("[NvimTree] " .. msg, level)
end
end)
end
end
M.notify = {}
M.notify.warn = notify_level(vim.log.levels.WARN)
M.notify.error = notify_level(vim.log.levels.ERROR)
M.notify.info = notify_level(vim.log.levels.INFO)
M.notify.debug = notify_level(vim.log.levels.DEBUG)
function M.str_find(haystack, needle)
return vim.fn.stridx(haystack, needle) ~= -1
end

View File

@@ -1,4 +1,5 @@
local uv = vim.loop
local notify = require "nvim-tree.notify"
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
@@ -47,7 +48,7 @@ function Event:start()
self._fs_event, _, name = uv.new_fs_event()
if not self._fs_event then
self._fs_event = nil
utils.notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name))
notify.warn(string.format("Could not initialize an fs_event watcher for path %s : %s", self._path, name))
return false
end
@@ -64,7 +65,7 @@ function Event:start()
rc, _, name = self._fs_event:start(self._path, FS_EVENT_FLAGS, event_cb)
if rc ~= 0 then
utils.notify.warn(string.format("Could not start the fs_event watcher for path %s : %s", self._path, name))
notify.warn(string.format("Could not start the fs_event watcher for path %s : %s", self._path, name))
return false
end
@@ -88,7 +89,7 @@ function Event:destroy()
if self._fs_event then
local rc, _, name = self._fs_event:stop()
if rc ~= 0 then
utils.notify.warn(string.format("Could not stop the fs_event watcher for path %s : %s", self._path, name))
notify.warn(string.format("Could not stop the fs_event watcher for path %s : %s", self._path, name))
end
self._fs_event = nil
end