Skip to content

Commit

Permalink
refactor: separate logging and notifying
Browse files Browse the repository at this point in the history
  • Loading branch information
jghauser committed Jul 4, 2024
1 parent 22aac8a commit 627b31a
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 30 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,6 @@ enable_icons = true,
-- the database.
required_keys = { "papis_id", "ref" },
},

-- Configuration of logging.
log = {

-- What levels to log. Debug mode is more conveniently
-- enabled in `enable_modules`.
level = "info",
},
```

In order to use the cmp source, you need to add it to the sources loaded by cmp.
Expand Down
2 changes: 1 addition & 1 deletion lua/papis/at-cursor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ local function if_ref_valid_run_fun(fun, self, type)
fun(papis_id, type)
end
else
log.info(string.format("No entry in database corresponds to '%s'", ref))
vim.notify(string.format("No entry in database corresponds to '%s'", ref), vim.log.levels.WARN)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lua/papis/completion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ local log = require("papis.log")

local has_cmp, cmp = pcall(require, "cmp")
if not has_cmp then
log.warn("The plugin nvim-cmp wasn't found but the respective papis.nvim module is configured to be loaded.")
vim.notify("The plugin nvim-cmp wasn't found but the respective papis.nvim module is configured to be loaded.",
vim.log.levels.ERROR)
return nil
end

Expand Down
3 changes: 0 additions & 3 deletions lua/papis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ local default_config = {
tag_format = nil,
required_keys = { "papis_id", "ref" },
},
log = {
level = "info", -- off turns it off
},
}

local M = vim.deepcopy(default_config)
Expand Down
2 changes: 1 addition & 1 deletion lua/papis/fs-watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
-- disable watcher
unwatch_cb()
-- note, print still occurs even though we unwatched *future* events
log.warn(string.format("An error occured: %s", error))
vim.notify(string.format("An error occured: %s", error), vim.log.levels.ERROR)
end)
end

Expand Down
9 changes: 5 additions & 4 deletions lua/papis/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ local function are_dependencies_available()
local dependencies = { "papis", config.yq_bin }
for _, dependency in ipairs(dependencies) do
if vim.fn.executable(dependency) == 0 then
log.error(
string.format("The executable '%s' could not be found. Please install it to use papis.nvim", dependency)
vim.notify(
string.format("The executable '%s' could not be found. Please install it to use papis.nvim", dependency),
vim.log.levels.ERROR
)
return false
end
Expand Down Expand Up @@ -62,7 +63,7 @@ function M.start()
-- set up db
local db = require("papis.sqlite-wrapper")
if not db then
log.warn("Requiring `sqlite-wrapper.lua` failed. Aborting...")
vim.notify("Requiring `sqlite-wrapper.lua` failed. Aborting...", vim.log.levels.ERROR)
return nil
end
db:init()
Expand All @@ -75,7 +76,7 @@ function M.start()
-- require what's necessary within `M.start()` instead of globally to allow lazy-loading
local data = require("papis.data")
if not data then
log.warn("Requiring `data.lua` failed. Aborting...")
vim.notify("Requiring `data.lua` failed. Aborting...", vim.log.levels.ERROR)
return nil
end

Expand Down
2 changes: 1 addition & 1 deletion lua/papis/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
local default_config = {
plugin = "papis.nvim",

use_console = true,
use_console = false,

highlights = true,

Expand Down
8 changes: 5 additions & 3 deletions lua/papis/papis-storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ local function is_valid_entry(entry, path)
if entry[key] then
is_valid = true
else
log.info(string.format("The entry at '%s' is missing the key '%s' and will not be added.", path, key))
vim.notify(string.format("The entry at '%s' is missing the key '%s' and will not be added.", path, key),
vim.log.levels.WARN)
break
end
end
else
log.info(string.format("The entry at '%s' is faulty and will not be added.", path))
vim.notify(string.format("The entry at '%s' is faulty and will not be added.", path), vim.log.levels.WARN)
end
return is_valid
end
Expand Down Expand Up @@ -199,7 +200,8 @@ function M.get_data_full(metadata)
if type(entry[key]) == "table" then
data[key] = entry[key]
else
log.warn("Wanted to add `" .. key .. "` of `" .. entry.ref .. "` but the value is not of type `table`")
vim.notify("Wanted to add `" .. key .. "` of `" .. entry.ref .. "` but the value is not of type `table`",
vim.log.levels.WARN)
data[key] = {}
end
end
Expand Down
3 changes: 2 additions & 1 deletion lua/papis/search/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
local log = require("papis.log")
local has_telescope, telescope = pcall(require, "telescope")
if not has_telescope then
log.error("The plugin telescope.nvim wasn't found but the search module is enabled and requires it.")
vim.notify("The plugin telescope.nvim wasn't found but the search module is enabled and requires it.",
vim.log.levels.ERROR)
return nil
end
local entry_display = require("telescope.pickers.entry_display")
Expand Down
5 changes: 2 additions & 3 deletions lua/papis/sqlite-wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
-- Wrapper around sqlite.lua setting up the main database and associated methods.
--

local log = require("papis.log")

local has_sqlite, sqlite = pcall(require, "sqlite")
if not has_sqlite then
log.error("The dependency 'sqlite.nvim' is missing. Ensure that it is installed to run papis.nvim")
vim.notify("The dependency 'sqlite.nvim' is missing. Ensure that it is installed to run papis.nvim",
vim.log.levels.ERROR)
return nil
end
local sqlite_utils = require "sqlite.utils"
Expand Down
8 changes: 4 additions & 4 deletions lua/papis/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ function M:do_open_attached_files(papis_id)
lookup_tbl[filename] = entry.files[k]
end
if vim.tbl_isempty(filenames) then
log.info("This item has no attached files.")
vim.notify("This item has no attached files.", vim.log.levels.WARN)
elseif #filenames == 1 then
log.info("Opening file '" .. filenames[1] .. "' ")
vim.notify("Opening file '" .. filenames[1] .. "' ", vim.log.levels.INFO)
local path = lookup_tbl[filenames[1]]
self:do_open_file_external(path)
else
vim.ui.select(filenames, {
prompt = "Select attachment to open:",
}, function(choice)
if choice then
log.info("Opening file '" .. choice .. "' ")
vim.notify("Opening file '" .. choice .. "' ", vim.log.levels.INFO)
local path = lookup_tbl[choice]
self:do_open_file_external(path)
end
Expand All @@ -133,7 +133,7 @@ end
function M:do_open_text_file(papis_id, type)
local db = require("papis.sqlite-wrapper")
if not db then
log.warn("Sqlite-wrapper has not been initialised properly. Aborting...")
vim.notify("Sqlite-wrapper has not been initialised properly. Aborting...", vim.log.levels.ERROR)
return nil
end
log.debug("Opening a text file")
Expand Down

0 comments on commit 627b31a

Please sign in to comment.