Skip to content

Use vim.notify as default console log #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lua/plenary/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ local default_config = {
plugin = "plenary",

-- Should print the output to neovim while running.
-- values: 'sync','async',false
use_console = "async",
-- values: 'sync','async','notify',false
use_console = "notify",

-- Should highlighting be used in console (using echohl).
highlights = true,
Expand Down Expand Up @@ -132,8 +132,20 @@ log.new = function(config, standalone)
local info = debug.getinfo(config.info_level or 2, "Sl")
local src_path = info.source:sub(2)
local src_line = info.currentline
-- Notify
if config.use_console == "notify" then
local level = vim.log.levels[level_config.name:upper()]
if level == nil then
level = vim.log.levels.ERROR
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ERROR by default? When can level be nil?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plenary's log config defines levels like:

  modes = {
    { name = "trace", hl = "Comment" },
    { name = "debug", hl = "Comment" },
    { name = "info", hl = "None" },
    { name = "warn", hl = "WarningMsg" },
    { name = "error", hl = "ErrorMsg" },
    { name = "fatal", hl = "ErrorMsg" },
  },

while vim.log.levels is defined as:

    vim.log.levels.DEBUG
    vim.log.levels.ERROR
    vim.log.levels.INFO
    vim.log.levels.TRACE
    vim.log.levels.WARN
    vim.log.levels.OFF

These almost match, but plenary defines a level "fatal" that vim.log.levels does not. I figured vim.log.levels.ERROR was the closest analogue to it, but maybe it's not sensible to just assume any unknown level should be treated as such. Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware of "fatal" in plenary. Makes sense, thanks for the explanation!

vim.notify(msg, level, {
title = config.plugin,
filename = src_path,
lnum = src_line,
col = 1,
})
-- Output to console
if config.use_console then
elseif config.use_console then
local log_to_console = function()
local console_string = config.fmt_msg(true, level_config.name, src_path, src_line, msg)

Expand Down