1
0
mirror of https://github.com/jiriks74/presence.nvim synced 2024-11-23 20:37:50 +01:00

Move autocmd to autoload and validate setup opts

This commit is contained in:
Andrew Kwon 2021-01-03 11:29:19 -08:00
parent a656ba5361
commit f3151560a7
3 changed files with 37 additions and 15 deletions

9
autoload/presence.vim Normal file

@ -0,0 +1,9 @@
" Define autocommands to handle auto-update events
function presence#SetAutoCmds()
augroup presence_events
autocmd!
if exists("g:presence_auto_update") && g:presence_auto_update
autocmd BufRead * lua package.loaded.presence:update()
endif
augroup END
endfunction

@ -6,6 +6,18 @@ local DiscordRPC = require("presence.discord")
function Presence:setup(options) function Presence:setup(options)
options = options or {} options = options or {}
self.options = options
-- Initialize logger with provided options
self.log = Log {
level = options.log_level or vim.g.presence_log_level,
}
self.log:debug("Setting up plugin...")
-- Warn on any duplicate user-defined options
self:check_dup_options("auto_update")
self:check_dup_options("log_level")
-- Ensure auto-update config is reflected in its global var setting -- Ensure auto-update config is reflected in its global var setting
if options.auto_update ~= nil or not vim.g.presence_auto_update then if options.auto_update ~= nil or not vim.g.presence_auto_update then
@ -15,12 +27,8 @@ function Presence:setup(options)
vim.api.nvim_set_var("presence_auto_update", should_auto_update) vim.api.nvim_set_var("presence_auto_update", should_auto_update)
end end
-- Initialize logger with provided options -- Set autocommands
self.log = Log { vim.fn["presence#SetAutoCmds"]()
level = options.log_level or vim.g.presence_log_level,
}
self.log:debug("Setting up plugin...")
-- Internal state -- Internal state
self.is_connected = false self.is_connected = false
@ -46,6 +54,19 @@ function Presence:setup(options)
return self return self
end end
-- Check and warn for duplicate user-defined options
function Presence:check_dup_options(option)
local g_variable = "presence_"..option
if self.options[option] ~= nil and vim.g[g_variable] ~= nil then
local warning_fmt = "Duplicate options set: `g:%s` and setup option `%s`"
local warning_msg = string.format(warning_fmt, g_variable, option)
self.log:warn(warning_msg)
end
end
function Presence:connect(on_done) function Presence:connect(on_done)
self.log:debug("Connecting to Discord...") self.log:debug("Connecting to Discord...")

@ -1,15 +1,7 @@
" Define autocommands to handle auto-update events
augroup presence_events
autocmd!
if g:presence_auto_update
autocmd BufRead * lua package.loaded.presence:update()
endif
augroup END
" Fallback to setting up the plugin automatically " Fallback to setting up the plugin automatically
if !exists("g:presence_has_setup") if !exists("g:presence_has_setup")
lua << EOF lua << EOF
local Presence = require("presence"):setup() local Presence = require("presence"):setup()
Presence.log:debug("Custom setup not detected, plugin set up using defaults") Presence.log:debug("Custom setup not detected, using defaults")
EOF EOF
endif endif