diff --git a/lua/user/check/exists.lua b/lua/user/check/exists.lua index a6bdd2ce..4a4d12db 100644 --- a/lua/user/check/exists.lua +++ b/lua/user/check/exists.lua @@ -1,9 +1,5 @@ ----@diagnostic disable:unused-local ----@diagnostic disable:unused-function ----@diagnostic disable:need-check-nil ----@diagnostic disable:missing-fields - require('user.types.user.check') + local Value = require('user.check.value') local is_nil = Value.is_nil @@ -14,26 +10,62 @@ local is_num = Value.is_num local is_fun = Value.is_fun local empty = Value.empty ----@type User.Check.Existance -M = { - module = function(mod, return_mod) - return_mod = is_bool(return_mod) and return_mod or false - - ---@type boolean - local res - ---@type unknown - local m - res, m = pcall(require, mod) - - if return_mod then - return not is_nil(m) and m or nil - else - return res +---@type fun(mod: string, return_mod: boolean?): boolean|unknown|nil +local function module(mod, return_mod) + return_mod = is_bool(return_mod) and return_mod or false + + ---@type boolean + local res + ---@type unknown + local m + res, m = pcall(require, mod) + + if return_mod then + return not is_nil(m) and m or nil + else + return res + end +end + +---@type fun(mod: string|string[], need_all: boolean?): boolean|table +local function modules(mod, need_all) + local exists = module + + if not (is_str(mod) or is_tbl(mod)) or empty(mod) then + error('`(user.check.exists.modules)`: Input is neither a string nor a table.') + end + + need_all = is_bool(need_all) and need_all or false + + ---@type boolean|table + local res = false + + if is_str(mod) then + res = exists(mod) + else + res = {} + + for _, v in next, mod do + local r = exists(v) + + if need_all then + res[v] = r + else + res = r + + -- Break when a module is not found. + if not r then + break + end + end end - end, -} + end + + return res +end -function M.vim_has(expr) +---@type fun(expr: string|string[]): boolean +local function vim_has(expr) if is_str(expr) then return vim.fn.has(expr) == 1 end @@ -42,7 +74,7 @@ function M.vim_has(expr) local res = false for _, v in next, expr do - if not M.vim_has(v) then + if not vim_has(v) then return false end end @@ -53,7 +85,8 @@ function M.vim_has(expr) return false end -function M.vim_exists(expr) +---@type fun(expr: string|string[]): boolean +local function vim_exists(expr) local exists = vim.fn.exists if is_str(expr) then @@ -63,7 +96,7 @@ function M.vim_exists(expr) if is_tbl(expr) and not empty(expr) then local res = false for _, v in next, expr do - res = M.vim_exists(v) + res = vim_exists(v) if not res then break @@ -76,10 +109,7 @@ function M.vim_exists(expr) return false end -function M.vim_isdir(path) - return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false -end - +---@type fun(vars: string|string[], fallback: fun()?): boolean local function env_vars(vars, fallback) local environment = vim.fn.environ() @@ -110,11 +140,8 @@ local function env_vars(vars, fallback) return res end -M.env_vars = env_vars - -function M.executable(exe, fallback) - local executable = vim.fn.executable - +---@type fun(exe: string|string[], fallback: fun()?): boolean +local function executable(exe, fallback) if not (is_str(exe) or is_tbl(exe)) then error('(user.check.exists.executable): Argument type is neither string nor table') end @@ -124,10 +151,10 @@ function M.executable(exe, fallback) local res = false if is_str(exe) then - res = executable(exe) == 1 + res = vim.fn.executable(exe) == 1 elseif is_tbl(exe) then for _, v in next, exe do - res = M.executable(v) + res = executable(v) if not res then break @@ -142,40 +169,20 @@ function M.executable(exe, fallback) return res end -function M.modules(mod, need_all) - local exists = M.module - - if not (is_str(mod) or is_tbl(mod)) or empty(mod) then - error('`(user.check.exists.modules)`: Input is neither a string nor a table.') - end - - need_all = is_bool(need_all) and need_all or false - - ---@type boolean|table - local res = false - - if is_str(mod) then - res = exists(mod) - else - res = {} - - for _, v in next, mod do - local r = exists(v) - - if need_all then - res[v] = r - else - res = r - - -- Break when a module is not found. - if not r then - break - end - end - end - end - - return res +---@type fun(path: string): boolean +local function vim_isdir(path) + return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false end +---@type User.Check.Existance +local M = { + module = module, + vim_has = vim_has, + vim_exists = vim_exists, + env_vars = env_vars, + executable = executable, + modules = modules, + vim_isdir = vim_isdir, +} + return M diff --git a/lua/user/check/init.lua b/lua/user/check/init.lua index f288edfd..437a7a61 100644 --- a/lua/user/check/init.lua +++ b/lua/user/check/init.lua @@ -10,27 +10,27 @@ local Exists = require('user.check.exists') --- Checking Utilities ---@type User.Check local M = { - value = Value, - exists = Exists, + value = require('user.check.value'), + exists = require('user.check.exists'), + + --- Check whether Nvim is running in a Linux Console rather than a `pty`. + --- --- + --- ## Description + --- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example. + --- --- + --- ## Return + --- A boolean that confirms whether the environment is a Linux Console. + --- --- + in_console = function() + local env = vim.fn.environ() + ---@type string + local TERM = env['TERM'] + + --- TODO: This is not a good enough check. Must find a better solution. + return vim.tbl_contains({ 'screen', 'linux' }, TERM) + end, } ---- Check whether Nvim is running in a Linux Console rather than a `pty`. ---- --- ---- ## Description ---- This function can be useful for (un)loading certain elements that conflict with the Linux console, for example. ---- --- ---- ## Return ---- A boolean that confirms whether the environment is a Linux Console. ---- --- -function M.in_console() - local env = vim.fn.environ() - ---@type string - local TERM = env['TERM'] - - --- TODO: This is not a good enough check. Must find a better solution. - return vim.tbl_contains({ 'screen', 'linux' }, TERM) -end - function M.new() local self = setmetatable({}, { __index = M }) diff --git a/lua/user/check/value.lua b/lua/user/check/value.lua index 13fafc1a..0ff03dee 100644 --- a/lua/user/check/value.lua +++ b/lua/user/check/value.lua @@ -5,6 +5,41 @@ require('user.types.user.check') +--- Checks whether a value is `nil`, i.e. non existant or explicitly set as nil. +--- ## Parameters +--- +--- * `var`: Any data type to be checked if it's nil. +--- **Keep in mind that if `multiple` is set to `true`, this _MUST_ be a _non-empty_ table**. +--- Otherwise it will be flagged as non-existant and the function will return `true`. +--- +--- * `multiple`: Tell the function you're checking for multiple values. (Default: `false`). +--- If set to `true`, every element of the table will be checked. +--- If **any** element doesn't exist or is `nil`, the function automatically returns false. +--- +--- ## Return +--- A boolean value indicating whether the data is `nil` or doesn't exist. +--- --- +local function is_nil(var, multiple) + multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false + + if not multiple then + return var == nil + end + + --- Treat `var` as a table from here on + if type(var) ~= 'table' or vim.tbl_isempty(var) then + return false + end + + for _, v in next, var do + if v ~= nil then + return false + end + end + + return true +end + ---@type User.Check.Value local M = { -- NOTE: We define `is_nil` first as it's used by the other checkers. @@ -23,33 +58,12 @@ local M = { --- ## Return --- A boolean value indicating whether the data is `nil` or doesn't exist. --- --- - is_nil = function(var, multiple) - multiple = (multiple ~= nil and type(multiple) == 'boolean') and multiple or false - - if not multiple then - return var == nil - end - - --- Treat `var` as a table from here on - if type(var) ~= 'table' or vim.tbl_isempty(var) then - return false - end - - for _, v in next, var do - if v ~= nil then - return false - end - end - - return true - end, + is_nil = is_nil, } ---@type fun(t: Types): ValueFunc local function type_fun(t) return function(var, multiple) - local is_nil = M.is_nil - multiple = (not is_nil(multiple) and type(multiple) == 'boolean') and multiple or false if not multiple then @@ -164,7 +178,6 @@ M.is_tbl = type_fun('table') --- A boolean value indicating whether the data is an integer or not. --- --- function M.is_int(var, multiple) - local is_nil = M.is_nil local is_tbl = M.is_tbl local is_bool = M.is_bool local is_num = M.is_num @@ -222,8 +235,7 @@ function M.empty(v) return true end -function M.fields(fields, T) - local is_nil = M.is_nil +local function fields(field, T) local is_tbl = M.is_tbl local is_str = M.is_str local is_num = M.is_num @@ -233,22 +245,23 @@ function M.fields(fields, T) error('(user.check.value.fields): Cannot look up a field in the following type: ' .. type(T)) end - if not (is_str(fields) or is_num(fields) or is_tbl(fields)) or empty(fields) then + if not (is_str(field) or is_num(field) or is_tbl(field)) or empty(field) then error('(user.check.value.fields): Field type `' .. type(T) .. '` not parseable') end - if not is_tbl(fields) then - return not is_nil(T[fields]) + if not is_tbl(field) then + return not is_nil(T[field]) end - for _, v in next, fields do - if not M.fields(v, T) then + for _, v in next, field do + if not fields(v, T) then return false end end return true end +M.fields = fields function M.tbl_values(values, T, return_keys) local is_tbl = M.is_tbl diff --git a/lua/user/highlight.lua b/lua/user/highlight.lua index b1cd448d..2c0a81c5 100644 --- a/lua/user/highlight.lua +++ b/lua/user/highlight.lua @@ -3,69 +3,71 @@ require('user.types.user.highlight') -local Check = require('user.check') +local Value = require('user.check.value') -local is_nil = Check.value.is_nil -local is_str = Check.value.is_str -local is_tbl = Check.value.is_tbl -local is_int = Check.value.is_int -local empty = Check.value.empty +local is_nil = Value.is_nil +local is_str = Value.is_str +local is_tbl = Value.is_tbl +local is_int = Value.is_int +local empty = Value.empty + +local function hl(name, opts, bufnr) + if not (is_str(name) and is_tbl(opts)) or empty(name) then + error('(user.highlight.hl): A highlight value is not permitted!') + end + + bufnr = is_int(bufnr) and bufnr or 0 + + vim.api.nvim_set_hl(bufnr, name, opts) +end ---@type User.Hl +---@diagnostic disable-next-line:missing-fields local M = { - hl = function(name, opts, bufnr) - if not (is_str(name) and is_tbl(opts)) or empty(name) then - error('(user.highlight.hl): A highlight value is not permitted!') + hl = hl, + + hl_from_arr = function(arr) + if not is_tbl(arr) or empty(arr) then + error('(user.highlight.hl_from_arr): Unable to parse argument.') end - bufnr = is_int(bufnr) and bufnr or 0 + for _, T in next, arr do + if not (is_str(T.name) and is_tbl(T.opts)) or empty(T.name) then + error('(user.highlight.hl_from_arr): A highlight value is not permitted!') + end - vim.api.nvim_set_hl(bufnr, name, opts) + hl(T.name, T.opts) + end end, -} -function M.hl_from_arr(arr) - if not is_tbl(arr) or empty(arr) then - error('(user.highlight.hl_from_arr): Unable to parse argument.') - end + --[[ Set hl groups based on a dict input. - for _, T in next, arr do - if not (is_str(T.name) and is_tbl(T.opts)) or empty(T.name) then - error('(user.highlight.hl_from_arr): A highlight value is not permitted!') + Example of a valid table: + ```lua + local T = { + ['HlGroup'] = { fg = '...', ... }, + ['HlGroupAlt'] = { ... }, + } + ``` + Which translates into VimScript as: + ```vim + hi HlGroup ctermfg=... ... + hi HlGroupAlt ... + ``` + See more at `:h nvim_set_hl` ]] + hl_from_dict = function(dict) + if not is_tbl(dict) or empty(dict) then + error('(user.highlight.hl_from_dict): Unable to parse argument.') end - M.hl(T.name, T.opts) - end -end - ---- Set hl groups based on a dict input. ---- --- ---- Example of a valid table: ---- ```lua ---- local T = { ---- ['HlGroup'] = { fg = '...', ... }, ---- ['HlGroupAlt'] = { ... }, ---- } ---- ``` ---- Which equals to: ---- ```vim ---- hi HlGroup ctermfg=... ... ---- hi HlGroupAlt ... ---- ``` ---- ---- See more at `:h nvim_set_hl` -function M.hl_from_dict(dict) - if not is_tbl(dict) or empty(dict) then - error('(user.highlight.hl_from_dict): Unable to parse argument.') - end - - for k, v in next, dict do - if (is_str(k) and is_tbl(v)) and not empty(k) then - M.hl(k, v) - else - error('(user.highlight.hl_from_dict): A highlight value is not permitted!') + for k, v in next, dict do + if (is_str(k) and is_tbl(v)) and not empty(k) then + hl(k, v) + else + error('(user.highlight.hl_from_dict): A highlight value is not permitted!') + end end - end -end + end, +} return M diff --git a/lua/user/maps/init.lua b/lua/user/maps/init.lua index af7a59d0..529d337b 100644 --- a/lua/user/maps/init.lua +++ b/lua/user/maps/init.lua @@ -4,34 +4,29 @@ require('user.types.user.maps') -local Check = require('user.check') -local Util = require('user.util') - -local is_nil = Check.value.is_nil -local is_tbl = Check.value.is_tbl -local is_fun = Check.value.is_fun -local is_str = Check.value.is_str -local is_num = Check.value.is_num -local is_int = Check.value.is_int -local is_bool = Check.value.is_bool -local empty = Check.value.empty -local field = Check.value.fields -local strip_fields = Util.strip_fields - -local kmap = vim.keymap.set -local map = vim.api.nvim_set_keymap -local bufmap = vim.api.nvim_buf_set_keymap - ----@type Modes +local Value = require('user.check.value') + +local is_nil = Value.is_nil +local is_tbl = Value.is_tbl +local is_str = Value.is_str +local is_int = Value.is_int +local is_bool = Value.is_bool +local empty = Value.empty +local field = Value.fields +local strip_fields = require('user.util').strip_fields + local MODES = { 'n', 'i', 'v', 't', 'o', 'x' } ---@type User.Maps local M = { + modes = MODES, + kmap = require('user.maps.kmap'), map = require('user.maps.map'), buf_map = require('user.maps.buf_map'), + wk = require('user.maps.wk'), - modes = MODES, + nop = function(T, opts, mode, prefix) if not (is_str(T) or is_tbl(T)) then error('(user.maps.nop): Argument is neither a string nor a table') @@ -57,91 +52,89 @@ local M = { prefix = is_str(prefix) and prefix or '' - local Kmap = require('user.maps.kmap') - if is_str(T) then - kmap(mode, prefix .. T, '', opts) + vim.keymap.set(mode, prefix .. T, '', opts) else for _, v in next, T do - kmap(mode, prefix .. v, '', opts) + vim.keymap.set(mode, prefix .. v, '', opts) end end end, -} -function M.map_dict(T, map_func, dict_has_modes, mode, bufnr) - if not (is_tbl(T) and not empty(T)) then - error("(user.maps.map_dict): Keys either aren't table or table is empty") - end - if not is_str(map_func) or empty(map_func) then - error('(user.maps.map_dict): `map_func` is not a string') - end + map_dict = function(T, map_func, dict_has_modes, mode, bufnr) + if not (is_tbl(T) and not empty(T)) then + error("(user.maps.map_dict): Keys either aren't table or table is empty") + end + if not is_str(map_func) or empty(map_func) then + error('(user.maps.map_dict): `map_func` is not a string') + end - if not (is_str(mode) and vim.tbl_contains(M.modes, mode)) then - mode = 'n' - end + mode = (is_str(mode) and vim.tbl_contains(MODES, mode)) and mode or 'n' + dict_has_modes = is_bool(dict_has_modes) and dict_has_modes or false + bufnr = is_int(bufnr) and bufnr or nil - dict_has_modes = is_bool(dict_has_modes) and dict_has_modes or false + local Kmap = require('user.maps.kmap') + local Map = require('user.maps.map') + local WK = require('user.maps.wk') - bufnr = is_int(bufnr) and bufnr or nil + local map_choices = { + kmap = Kmap, + map = Map, + ['wk.register'] = WK.register, + } - local map_choices = { - ['kmap'] = M.kmap, - ['map'] = M.map, - ['wk.register'] = M.wk.register, - } + if not field(map_func, map_choices) or (map_func == 'wk.register' and not WK.available()) then + map_func = 'kmap' + end - if not field(map_func, map_choices) or (map_func == 'wk.register' and not M.wk.available()) then - map_func = 'kmap' - end + local func - if dict_has_modes then - for mode_choice, t in next, T do - if map_func == 'kmap' or map_func == 'map' then - for lhs, v in next, t do - v[2] = is_tbl(v[2]) and v[2] or {} + if dict_has_modes then + for mode_choice, t in next, T do + if map_func == 'kmap' or map_func == 'map' then + ---@type ApiMapFunction|KeyMapFunction + func = map_choices[map_func][mode_choice] - map_choices[map_func][mode_choice](lhs, v[1], v[2]) - end - else - ---@type RegOpts - local wk_opts = { mode = mode_choice } + for lhs, v in next, t do + v[2] = is_tbl(v[2]) and v[2] or {} + + func(lhs, v[1], v[2]) + end + else + func = map_choices['wk.register'] + + ---@type RegOpts + local wk_opts = { mode = mode_choice } + + if not is_nil(bufnr) then + wk_opts.buffer = bufnr + end - if not is_nil(bufnr) then - wk_opts.buffer = bufnr + func(WK.convert_dict(t), wk_opts) end + end + elseif map_func == 'kmap' or map_func == 'map' then + ---@type ApiMapFunction|KeyMapFunction + func = map_choices[map_func][mode] + + for lhs, v in next, T do + v[2] = is_tbl(v[2]) and v[2] or {} - map_choices[map_func](M.wk.convert_dict(t), wk_opts) + func(lhs, v[1], v[2]) end - end - elseif map_func == 'kmap' or map_func == 'map' then - for lhs, v in next, T do - v[2] = is_tbl(v[2]) and v[2] or {} + else + func = map_choices['wk.register'] - map_choices[map_func][mode](lhs, v[1], v[2]) - end - else - ---@type RegOpts - local wk_opts = { mode = mode } + ---@type RegOpts + local wk_opts = { mode = mode } - if not is_nil(bufnr) then - wk_opts.buffer = bufnr - end + if not is_nil(bufnr) then + wk_opts.buffer = bufnr + end - map_choices[map_func](M.wk.convert_dict(T), wk_opts) - end -end - -for _, key in next, { M.map, M.buf_map } do - function key.desc(msg, silent, noremap, nowait, expr) - return { - desc = (is_str(msg) and not empty(msg)) and msg or 'Unnamed Key', - silent = is_bool(silent) and silent or true, - noremap = is_bool(noremap) and noremap or true, - nowait = is_bool(nowait) and nowait or true, - expr = is_bool(expr) and expr or false, - } - end -end + func(WK.convert_dict(T), wk_opts) + end + end, +} return M diff --git a/lua/user/opts.lua b/lua/user/opts.lua index d3f2ee2e..13cd6270 100644 --- a/lua/user/opts.lua +++ b/lua/user/opts.lua @@ -3,16 +3,16 @@ require('user.types.user.opts') -local Check = require('user.check') -local Util = require('user.util') +local Value = require('user.check.value') +local Exists = require('user.check.exists') -local exists = Check.exists.vim_exists -local is_nil = Check.value.is_nil -local executable = Check.exists.executable -local vim_has = Check.exists.vim_has -local vim_exists = Check.exists.vim_exists -local in_console = Check.in_console -local notify = Util.notify.notify +local exists = Exists.vim_exists +local is_nil = Value.is_nil +local executable = Exists.executable +local vim_has = Exists.vim_has +local vim_exists = Exists.vim_exists +local in_console = require('user.check').in_console +local notify = require('user.util.notify').notify ---@type OptsTbl local opt_tbl = { diff --git a/lua/user/update.lua b/lua/user/update.lua index 467ad2ee..2e9d23bd 100644 --- a/lua/user/update.lua +++ b/lua/user/update.lua @@ -2,29 +2,27 @@ ---@diagnostic disable:unused-local require('user.types.user.update') -local Util = require('user.util') -local Notify = require('user.util.notify') -local notify = Notify.notify +local notify = require('user.util.notify').notify ---@type User.Update ---@diagnostic disable-next-line:missing-fields -local M = {} - -function M.update(...) - local args = { ... } - - local old_cwd = vim.fn.getcwd() - - local cmd = { - 'git', - 'pull', - '--rebase', - '--recurse-submodules', - } - - vim.system(cmd, { cwd = vim.fn.stdpath('config') }) -end +local M = { + update = function(...) + local args = { ... } + + local old_cwd = vim.fn.getcwd() + + local cmd = { + 'git', + 'pull', + '--rebase', + '--recurse-submodules', + } + + vim.system(cmd, { cwd = vim.fn.stdpath('config') }) + end, +} function M.new() local self = setmetatable({}, { __index = M, __call = M.update }) diff --git a/lua/user/util/init.lua b/lua/user/util/init.lua index f54b1002..677ed2dc 100644 --- a/lua/user/util/init.lua +++ b/lua/user/util/init.lua @@ -3,11 +3,7 @@ require('user.types.user.util') ----@type User.Util ----@diagnostic disable-next-line:missing-fields -local M = {} - -function M.xor(x, y) +local function xor(x, y) if not require('user.check.value').is_bool({ x, y }, true) then error('(user.util.xor): An argument is not of boolean type') end @@ -15,6 +11,12 @@ function M.xor(x, y) return (x and not y) or (not x and y) end +---@type User.Util +---@diagnostic disable-next-line:missing-fields +local M = { + xor = xor, +} + function M.strip_fields(T, fields) local Value = require('user.check.value') @@ -76,12 +78,9 @@ function M.strip_values(T, values, max_instances) error('(user.util.strip_values): No values given') end - local xor = M.xor - max_instances = is_int(max_instances) and max_instances or 0 local res = {} - local count = 0 for k, v in next, T do @@ -103,7 +102,7 @@ function M.strip_values(T, values, max_instances) return res end -function M.ft_set(s, bufnr) +local function ft_set(s, bufnr) local Value = require('user.check.value') local is_int = Value.is_int @@ -117,6 +116,7 @@ function M.ft_set(s, bufnr) end end end +M.ft_set = ft_set function M.ft_get(bufnr) bufnr = require('user.check.value').is_int(bufnr) and bufnr or 0 @@ -135,7 +135,7 @@ function M.assoc() local is_str = Value.is_str local empty = Value.empty - local ft = M.ft_set + local ft = ft_set local au = vim.api.nvim_create_autocmd @@ -209,8 +209,8 @@ function M.assoc() pattern = 'lua', callback = function() local map_dict = require('user.maps').map_dict - local WK = require('user.maps').wk - local desc = require('user.maps').kmap.desc + local WK = require('user.maps.wk') + local desc = require('user.maps.kmap').desc if require('user.check.exists').executable('stylua') then map_dict({ @@ -232,30 +232,32 @@ function M.assoc() table.insert(aus[1].opts_tbl, { pattern = '*.org', callback = ft('org'), group = group }) end + local notify = require('user.util.notify').notify + for _, v in next, aus do if not (is_str(v.events) or is_tbl(v.events)) or empty(v.events) then - M.notify.notify('(user.assoc): Event type `' .. type(v.events) .. '` is neither string nor table', 'error') + notify('(user.assoc): Event type `' .. type(v.events) .. '` is neither string nor table', 'error') goto continue end if not is_tbl(v.opts_tbl) or empty(v.opts_tbl) then - M.notify.notify('(user.assoc): Event options are not in a table or it is empty', 'error') + notify('(user.assoc): Event options are not in a table or it is empty', 'error') goto continue end for _, o in next, v.opts_tbl do if not is_tbl(o) or empty(o) then - M.notify.notify('(user.assoc): Event option is not a table or an empty one', 'error') + notify('(user.assoc): Event option is not a table or an empty one', 'error') goto continue end if not is_nil(o.pattern) and (not is_str(o.pattern) or empty(o.pattern)) then - M.notify.notify('(user.assoc): Pattern is not a string or is an empty one', 'error') + notify('(user.assoc): Pattern is not a string or is an empty one', 'error') goto continue end if not is_fun(o.callback) then - M.notify.notify('(user.assoc): Callback is not a function', 'error') + notify('(user.assoc): Callback is not a function', 'error') goto continue end diff --git a/lua/user/util/notify.lua b/lua/user/util/notify.lua index 13a4305f..097db2e4 100644 --- a/lua/user/util/notify.lua +++ b/lua/user/util/notify.lua @@ -14,74 +14,74 @@ local function exists(mod) end ---@type User.Util.Notify -local M = {} - -function M.notify(msg, lvl, opts) - if type(msg) ~= 'string' then - error('(user.util.notify.notify): Empty message', vim.log.levels.ERROR) - end - - opts = (type(opts) == 'table') and opts or {} - - local vim_lvl = vim.log.levels - - local DEFAULT_LVLS = { - 'trace', - 'debug', - 'info', - 'warn', - 'error', - 'off', - } - - ---@type notify.Options - local DEFAULT_OPTS = { - animate = true, - hide_from_history = false, - title = 'Message', - timeout = 1500, - } - - if exists('notify') then - local notify = require('notify') - - if type(lvl) == 'number' and (lvl >= 0 and lvl <= 5) then - lvl = DEFAULT_LVLS[math.floor(lvl) + 1] - elseif type(lvl) == 'number' then - lvl = DEFAULT_LVLS[3] +local M = { + notify = function(msg, lvl, opts) + if type(msg) ~= 'string' then + error('(user.util.notify.notify): Empty message', vim.log.levels.ERROR) end - if opts ~= nil and type(opts) == 'table' and not vim.tbl_isempty(opts) then - for key, v in next, DEFAULT_OPTS do - opts[key] = (opts[key] ~= nil and type(v) == type(opts[key])) and opts[key] or v + opts = (type(opts) == 'table') and opts or {} + + local vim_lvl = vim.log.levels + + local DEFAULT_LVLS = { + 'trace', + 'debug', + 'info', + 'warn', + 'error', + 'off', + } + + ---@type notify.Options + local DEFAULT_OPTS = { + animate = true, + hide_from_history = false, + title = 'Message', + timeout = 1500, + } + + if exists('notify') then + local notify = require('notify') + + if type(lvl) == 'number' and (lvl >= 0 and lvl <= 5) then + lvl = DEFAULT_LVLS[math.floor(lvl) + 1] + elseif type(lvl) == 'number' then + lvl = DEFAULT_LVLS[3] end - else - for key, v in next, DEFAULT_OPTS do - opts[key] = v - end - end - notify(msg, lvl, opts) - else - if type(lvl) == 'string' then - for k, v in next, DEFAULT_LVLS do - if lvl == v then - lvl = k - 1 - break + if opts ~= nil and type(opts) == 'table' and not vim.tbl_isempty(opts) then + for key, v in next, DEFAULT_OPTS do + opts[key] = (opts[key] ~= nil and type(v) == type(opts[key])) and opts[key] or v + end + else + for key, v in next, DEFAULT_OPTS do + opts[key] = v end end + notify(msg, lvl, opts) + else if type(lvl) == 'string' then - lvl = vim_lvl.INFO + for k, v in next, DEFAULT_LVLS do + if lvl == v then + lvl = k - 1 + break + end + end + + if type(lvl) == 'string' then + lvl = vim_lvl.INFO + end end - end - if opts ~= nil and type(opts) == 'table' and not vim.tbl_isempty(opts) then - vim.notify(msg, lvl, opts) - else - vim.notify(msg, lvl) + if opts ~= nil and type(opts) == 'table' and not vim.tbl_isempty(opts) then + vim.notify(msg, lvl, opts) + else + vim.notify(msg, lvl) + end end - end -end + end, +} return M