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

Expose more configs with generalized option setter

This commit is contained in:
Andrew Kwon 2021-02-28 16:05:09 -08:00
parent bb149540fd
commit e92f8f73b7

@ -9,24 +9,27 @@ function Presence:setup(options)
options = options or {} options = options or {}
self.options = options self.options = options
-- Initialize logger with provided options -- Initialize logger
self.log = log:init({ self:set_option("log_level", "warn", false)
level = options.log_level or vim.g.presence_log_level, self.log = log:init({ level = options.log_level })
}) self:check_dup_options("log_level")
-- Use the default or user-defined client id if provided
if options.client_id then
self.log:info("Using user-defined Discord client id")
end
self:set_option("auto_update", 1)
self:set_option("main_image", "neovim")
self:set_option("editing_text", "Editing %s")
self:set_option("workspace_text", "Working on %s")
self:set_option("neovim_image_text", "The One True Text Editor")
self:set_option("client_id", "793271441293967371")
self.log:debug("Setting up plugin...") 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 vim.api.nvim_set_var("presence_auto_update", options.auto_update)
local should_auto_update = options.auto_update ~= nil
and (options.auto_update and 1 or 0)
or (vim.g.presence_auto_update or 1)
vim.api.nvim_set_var("presence_auto_update", should_auto_update)
end
-- Set autocommands -- Set autocommands
vim.fn["presence#SetAutoCmds"]() vim.fn["presence#SetAutoCmds"]()
@ -35,17 +38,10 @@ function Presence:setup(options)
self.is_connected = false self.is_connected = false
self.is_authorized = false self.is_authorized = false
-- Use the default or user-defined client id if provided
self.client_id = "793271441293967371"
if options.client_id then
self.log:debug("Using user-defined Discord client id")
self.client_id = options.client_id
end
self.discord = Discord:init({ self.discord = Discord:init({
client_id = self.client_id,
ipc_path = self.get_ipc_path(),
logger = self.log, logger = self.log,
client_id = options.client_id,
ipc_path = self.get_ipc_path(),
}) })
self.log:info("Completed plugin setup") self.log:info("Completed plugin setup")
@ -56,6 +52,39 @@ function Presence:setup(options)
return self return self
end end
-- Set option using either vim global or setup table
function Presence:set_option(option, default, validate)
validate = validate == nil and true or validate
local g_variable = string.format("presence_%s", option)
-- Coalesce boolean options to integer 0 or 1
if type(self.options[option]) == "boolean" then
self.options[option] = self.options[option] and 1 or 0
end
if validate then
-- Warn on any duplicate user-defined options
self:check_dup_options(option)
end
self.options[option] = self.options[option] or
vim.g[g_variable] or
default
end
-- Check and warn for duplicate user-defined options
function Presence:check_dup_options(option)
local g_variable = string.format("presence_%s", option)
if self.options[option] ~= nil and vim.g[g_variable] ~= nil then
local warning_fmt = "Duplicate options: `g:%s` and setup option `%s`"
local warning_msg = string.format(warning_fmt, g_variable, option)
self.log:warn(warning_msg)
end
end
-- Send a nil activity to unset the presence -- Send a nil activity to unset the presence
function Presence:cancel() function Presence:cancel()
self.log:debug("Canceling Discord presence...") self.log:debug("Canceling Discord presence...")
@ -107,18 +136,6 @@ function Presence:call_remote_nvim_instance(ipc_path, command)
end) end)
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...")
@ -302,21 +319,28 @@ function Presence:update_for_buffer(buffer)
local name = filename local name = filename
local asset_key = "file" local asset_key = "file"
local description = filename local description = filename
if files[extension] then local file_asset = extension and files[extension] or files[filename]
name, asset_key, description = unpack(files[extension]) if file_asset then
name, asset_key, description = unpack(file_asset)
end end
local file_text = description or name
local neovim_image_text = self.options.neovim_image_text
-- TODO: Update timestamp to be workspace-specific -- TODO: Update timestamp to be workspace-specific
local started_at = os.time() local started_at = os.time()
local use_file_as_main_image = self.options.main_image == "file"
local assets = {
large_image = use_file_as_main_image and asset_key or "neovim",
large_text = use_file_as_main_image and file_text or neovim_image_text,
small_image = use_file_as_main_image and "neovim" or asset_key,
small_text = use_file_as_main_image and neovim_image_text or file_text,
}
local activity = { local activity = {
state = string.format("Editing %s", filename), state = string.format(self.options.editing_text, filename),
assets = { assets = assets,
large_image = "neovim",
large_text = "The One True Text Editor",
small_image = asset_key,
small_text = description or name,
},
timestamps = { timestamps = {
start = started_at start = started_at
}, },
@ -326,7 +350,7 @@ function Presence:update_for_buffer(buffer)
local project_name = self:get_project_name(parent_dirpath) local project_name = self:get_project_name(parent_dirpath)
if project_name then if project_name then
self.log:debug(string.format("Detected project: %s", project_name)) self.log:debug(string.format("Detected project: %s", project_name))
activity.details = string.format("Working on %s", project_name) activity.details = string.format(self.options.workspace_text, project_name)
else else
self.log:debug("No project detected") self.log:debug("No project detected")
end end