From f6f49dd0595b997848262ff3e51c7c1b852cc11a Mon Sep 17 00:00:00 2001 From: Matt Leong Date: Tue, 23 Nov 2021 08:29:27 -0800 Subject: [PATCH] feat(utils): clean up logger --- lua/cosmic/core/session/mappings.lua | 9 ++++-- lua/cosmic/core/theme/ui.lua | 5 +-- lua/cosmic/lsp/mappings.lua | 6 +++- lua/cosmic/{utils.lua => utils/init.lua} | 20 +++--------- lua/cosmic/utils/logger.lua | 39 ++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 20 deletions(-) rename lua/cosmic/{utils.lua => utils/init.lua} (87%) create mode 100644 lua/cosmic/utils/logger.lua diff --git a/lua/cosmic/core/session/mappings.lua b/lua/cosmic/core/session/mappings.lua index 5b3f18c..e22ab9f 100644 --- a/lua/cosmic/core/session/mappings.lua +++ b/lua/cosmic/core/session/mappings.lua @@ -1,5 +1,10 @@ local map = require('cosmic.utils').map -- session -map('n', 'sl', ':silent RestoreSession') -map('n', 'ss', ':SaveSession') +map('n', 'sl', 'silent RestoreSession') +map('n', 'ss', 'SaveSession') +map( + 'n', + 'si', + 'lua require("cosmic.utils.logger"):log(require("auto-session-library").current_session_name())' +) diff --git a/lua/cosmic/core/theme/ui.lua b/lua/cosmic/core/theme/ui.lua index b54881d..0c01f74 100644 --- a/lua/cosmic/core/theme/ui.lua +++ b/lua/cosmic/core/theme/ui.lua @@ -8,6 +8,7 @@ function M.rename() local icons = require('cosmic.core.theme.icons') local utils = require('cosmic.utils') local config = require('cosmic.config') + local Logger = require('cosmic.utils.logger') local highlight = utils.highlight local prompt_str = ' ' .. icons.folder.arrow_closed .. ' ' local map_opts = { noremap = true, silent = true } @@ -62,7 +63,7 @@ function M.rename() if config.lsp.rename_notification then if err then - vim.notify(("Error running LSP query '%s': %s"):format(method, err), vim.log.levels.ERROR) + Logger:error(("Error running LSP query '%s': %s"):format(method, err)) return end @@ -75,7 +76,7 @@ function M.rename() table.insert(msg, ('%d changes -> %s'):format(#c, utils.get_relative_path(f))) end local currName = vim.fn.expand('') - vim.notify(msg, vim.log.levels.INFO, { title = ('Rename: %s -> %s'):format(currName, new_word) }) + Logger:log(msg, { title = ('Rename: %s -> %s'):format(currName, new_word) }) end end diff --git a/lua/cosmic/lsp/mappings.lua b/lua/cosmic/lsp/mappings.lua index f0825d4..e64c5f0 100644 --- a/lua/cosmic/lsp/mappings.lua +++ b/lua/cosmic/lsp/mappings.lua @@ -25,7 +25,11 @@ map('n', '', 'lua require("lsp_signature").signature()') -- lsp workspace map('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()') map('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()') -map('n', 'wl', 'lua vim.notify(vim.inspect(vim.lsp.buf.list_workspace_folders()))') +map( + 'n', + 'wl', + 'lua require("cosmic.utils.logger"):log(vim.inspect(vim.lsp.buf.list_workspace_folders()))' +) -- typescript helpers map('n', 'gr', ':TSLspRenameFile') diff --git a/lua/cosmic/utils.lua b/lua/cosmic/utils/init.lua similarity index 87% rename from lua/cosmic/utils.lua rename to lua/cosmic/utils/init.lua index 6bc4bf6..fa59e20 100644 --- a/lua/cosmic/utils.lua +++ b/lua/cosmic/utils/init.lua @@ -1,3 +1,4 @@ +local Logger = require('cosmic.utils.logger') local M = {} function M.map(mode, lhs, rhs, opts) @@ -77,9 +78,7 @@ function M.post_reload(msg) unload('cosmic.core.theme.highlights', true) unload('cosmic.core.statusline', true) msg = msg or 'User config reloaded!' - vim.notify(msg, vim.log.levels.INFO, { - title = 'CosmicNvim', - }) + Logger:log(msg) end function M.reload_user_config_sync() @@ -120,25 +119,16 @@ function M.update() args = { 'pull', '--ff-only' }, cwd = path, on_start = function() - vim.notify('Updating...', vim.log.levels.INFO, { - title = 'CosmicNvim', - }) + Logger:log('Updating...') end, on_exit = function() if vim.tbl_isempty(errors) then - vim.notify( - 'Updated! Running CosmicReloadSync...', - vim.log.levels.INFO, - { title = 'CosmicNvim', timeout = 30000 } - ) + Logger:log('Updated! Running CosmicReloadSync...', { timeout = 30000 }) M.reload_user_config_sync() else table.insert(errors, 1, 'Something went wrong! Please pull changes manually.') table.insert(errors, 2, '') - vim.notify(errors, vim.log.levels.ERROR, { - title = 'CosmicNvim Update Failed!', - timeout = 30000, - }) + Logger:error('Update failed!', { timeout = 30000 }) end end, on_stderr = function(_, err) diff --git a/lua/cosmic/utils/logger.lua b/lua/cosmic/utils/logger.lua new file mode 100644 index 0000000..9b7682a --- /dev/null +++ b/lua/cosmic/utils/logger.lua @@ -0,0 +1,39 @@ +local Logger = {} +Logger.__index = Logger + +local title = 'CosmicNvim' + +function Logger:log(msg, opts) + opts = opts or {} + vim.notify( + msg, + vim.log.levels.INFO, + vim.tbl_deep_extend('force', { + title = title, + }, opts) + ) +end + +function Logger:warn(msg, opts) + opts = opts or {} + vim.notify( + msg, + vim.log.levels.WARN, + vim.tbl_deep_extend('force', { + title = title, + }, opts) + ) +end + +function Logger:error(msg, opts) + opts = opts or {} + vim.notify( + msg, + vim.log.levels.ERROR, + vim.tbl_deep_extend('force', { + title = title, + }, opts) + ) +end + +return Logger