mirror of
https://github.com/jiriks74/presence.nvim
synced 2024-12-27 02:22:36 +01:00
Move autocmd to autoload and validate setup opts
This commit is contained in:
parent
a656ba5361
commit
f3151560a7
3 changed files with 37 additions and 15 deletions
9
autoload/presence.vim
Normal file
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)
|
||||
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
|
||||
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)
|
||||
end
|
||||
|
||||
-- Initialize logger with provided options
|
||||
self.log = Log {
|
||||
level = options.log_level or vim.g.presence_log_level,
|
||||
}
|
||||
|
||||
self.log:debug("Setting up plugin...")
|
||||
-- Set autocommands
|
||||
vim.fn["presence#SetAutoCmds"]()
|
||||
|
||||
-- Internal state
|
||||
self.is_connected = false
|
||||
|
@ -46,6 +54,19 @@ function Presence:setup(options)
|
|||
return self
|
||||
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)
|
||||
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
|
||||
if !exists("g:presence_has_setup")
|
||||
lua << EOF
|
||||
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
|
||||
endif
|
||||
|
|
Loading…
Reference in a new issue