diff --git a/lua/lib/log.lua b/lua/lib/log.lua index 7528a9a..fef5169 100644 --- a/lua/lib/log.lua +++ b/lua/lib/log.lua @@ -1,7 +1,6 @@ local Log = {} Log.codes = {} - Log.levels = { { "debug", "Comment" }, { "info", "None" }, @@ -9,22 +8,11 @@ Log.levels = { { "error", "ErrorMsg" }, } -function Log.new(options) - options = options or {} - - local logger = vim.deepcopy(Log) - logger.options = options - logger.options.level = options.level - - return logger +function Log:init(options) + self.level = options.level + return self end -setmetatable(Log, { - __call = function(_, ...) - return Log.new(...) - end, -}) - -- Initialize logger with log functions for each level for i = 1, #Log.levels do local level, hl = unpack(Log.levels[i]) @@ -33,7 +21,7 @@ for i = 1, #Log.levels do Log[level] = function(self, message) -- Skip if log level is not set or the log is below the configured or default level - if not self.options.level or self.codes[level] < self.codes[self.options.level] then + if not self.level or self.codes[level] < self.codes[self.level] then return end diff --git a/lua/presence/discord.lua b/lua/presence/discord.lua index b159d49..97a7206 100644 --- a/lua/presence/discord.lua +++ b/lua/presence/discord.lua @@ -18,11 +18,11 @@ Discord.events = { local struct = require("deps.struct") -- Initialize a new Discord RPC client -function Discord:new(options) +function Discord:init(options) self.log = options.logger + self.ipc_path = options.ipc_path self.client_id = options.client_id - self.ipc_path = self.find_ipc_path() self.pipe = vim.loop.new_pipe(true) return self @@ -71,7 +71,7 @@ function Discord:call(opcode, payload, on_response) on_response(err_message) else - self.log:debug("Successfully wrote message to pipe") + self.log:debug("Wrote message to pipe") end end) end) @@ -149,34 +149,14 @@ function Discord:set_activity(activity, on_response) self:call(self.opcodes.frame, payload, on_response) end --- Find the the IPC path in temporary runtime directories -function Discord.find_ipc_path() - local env_vars = { - 'TEMP', - 'TMP', - 'TMPDIR', - 'XDG_RUNTIME_DIR', - } - - for i = 1, #env_vars do - local var = env_vars[i] - local path = vim.loop.os_getenv(var) - if path then - return path - end - end - - return nil -end - function Discord.generate_uuid() - local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' + local template ="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" - local uuid = template:gsub('[xy]', function(char) - local n = char == 'x' + local uuid = template:gsub("[xy]", function(char) + local n = char == "x" and math.random(0, 0xf) or math.random(8, 0xb) - return string.format('%x', n) + return string.format("%x", n) end) return uuid diff --git a/lua/presence/init.lua b/lua/presence/init.lua index 5a52d63..352d2a9 100644 --- a/lua/presence/init.lua +++ b/lua/presence/init.lua @@ -1,18 +1,18 @@ local Presence = {} -local Log = require("lib.log") +local log = require("lib.log") local files = require("presence.files") local msgpack = require("deps.msgpack") -local DiscordRPC = require("presence.discord") +local Discord = require("presence.discord") function Presence:setup(options) options = options or {} self.options = options -- Initialize logger with provided options - self.log = Log { + self.log = log:init({ level = options.log_level or vim.g.presence_log_level, - } + }) self.log:debug("Setting up plugin...") @@ -42,8 +42,9 @@ function Presence:setup(options) self.client_id = options.client_id end - self.discord = DiscordRPC:new({ + self.discord = Discord:init({ client_id = self.client_id, + ipc_path = self.get_ipc_path(), logger = self.log, }) @@ -56,8 +57,8 @@ function Presence:setup(options) end -- Send a nil activity to unset the presence -function Presence:cancel_presence() - self.log:debug("Nullifying Discord presence...") +function Presence:cancel() + self.log:debug("Canceling Discord presence...") if not self.discord:is_connected() then return @@ -74,14 +75,14 @@ function Presence:cancel_presence() end -- Send command to cancel the presence for all other remote Neovim instances -function Presence:cancel_all_remote_presences() +function Presence:cancel_all_remote_instances() self:get_nvim_socket_addrs(function(sockets) for i = 1, #sockets do local nvim_socket = sockets[i] -- Skip if the nvim socket is the current instance if nvim_socket ~= vim.v.servername then - local command = "lua package.loaded.presence:cancel_presence()" + local command = "lua package.loaded.presence:cancel()" self:call_remote_nvim_instance(nvim_socket, command) end end @@ -161,6 +162,26 @@ function Presence:authorize(on_done) end) end +-- Find the the IPC path in temp runtime directories +function Presence.get_ipc_path() + local env_vars = { + "TEMP", + "TMP", + "TMPDIR", + "XDG_RUNTIME_DIR", + } + + for i = 1, #env_vars do + local var = env_vars[i] + local path = vim.loop.os_getenv(var) + if path then + return path + end + end + + return nil +end + -- Gets the file path of the current vim buffer function Presence.get_current_buffer(on_buffer) vim.schedule(function() @@ -270,7 +291,7 @@ function Presence:update_for_buffer(buffer) self.log:debug(string.format("Setting activity for %s...", buffer)) -- Send command to cancel presence for all remote Neovim instances - self:cancel_all_remote_presences() + self:cancel_all_remote_instances() -- Parse vim buffer local filename = self.get_filename(buffer)