mirror of
https://github.com/jiriks74/presence.nvim
synced 2024-11-23 20:37:50 +01:00
Fix various verbiage and style inconsistencies
This commit is contained in:
parent
66bd159dd3
commit
bb149540fd
@ -1,7 +1,6 @@
|
|||||||
local Log = {}
|
local Log = {}
|
||||||
|
|
||||||
Log.codes = {}
|
Log.codes = {}
|
||||||
|
|
||||||
Log.levels = {
|
Log.levels = {
|
||||||
{ "debug", "Comment" },
|
{ "debug", "Comment" },
|
||||||
{ "info", "None" },
|
{ "info", "None" },
|
||||||
@ -9,22 +8,11 @@ Log.levels = {
|
|||||||
{ "error", "ErrorMsg" },
|
{ "error", "ErrorMsg" },
|
||||||
}
|
}
|
||||||
|
|
||||||
function Log.new(options)
|
function Log:init(options)
|
||||||
options = options or {}
|
self.level = options.level
|
||||||
|
return self
|
||||||
local logger = vim.deepcopy(Log)
|
|
||||||
logger.options = options
|
|
||||||
logger.options.level = options.level
|
|
||||||
|
|
||||||
return logger
|
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(Log, {
|
|
||||||
__call = function(_, ...)
|
|
||||||
return Log.new(...)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
-- Initialize logger with log functions for each level
|
-- Initialize logger with log functions for each level
|
||||||
for i = 1, #Log.levels do
|
for i = 1, #Log.levels do
|
||||||
local level, hl = unpack(Log.levels[i])
|
local level, hl = unpack(Log.levels[i])
|
||||||
@ -33,7 +21,7 @@ for i = 1, #Log.levels do
|
|||||||
|
|
||||||
Log[level] = function(self, message)
|
Log[level] = function(self, message)
|
||||||
-- Skip if log level is not set or the log is below the configured or default level
|
-- 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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -18,11 +18,11 @@ Discord.events = {
|
|||||||
local struct = require("deps.struct")
|
local struct = require("deps.struct")
|
||||||
|
|
||||||
-- Initialize a new Discord RPC client
|
-- Initialize a new Discord RPC client
|
||||||
function Discord:new(options)
|
function Discord:init(options)
|
||||||
self.log = options.logger
|
self.log = options.logger
|
||||||
|
self.ipc_path = options.ipc_path
|
||||||
self.client_id = options.client_id
|
self.client_id = options.client_id
|
||||||
|
|
||||||
self.ipc_path = self.find_ipc_path()
|
|
||||||
self.pipe = vim.loop.new_pipe(true)
|
self.pipe = vim.loop.new_pipe(true)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
@ -71,7 +71,7 @@ function Discord:call(opcode, payload, on_response)
|
|||||||
|
|
||||||
on_response(err_message)
|
on_response(err_message)
|
||||||
else
|
else
|
||||||
self.log:debug("Successfully wrote message to pipe")
|
self.log:debug("Wrote message to pipe")
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
@ -149,34 +149,14 @@ function Discord:set_activity(activity, on_response)
|
|||||||
self:call(self.opcodes.frame, payload, on_response)
|
self:call(self.opcodes.frame, payload, on_response)
|
||||||
end
|
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()
|
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 uuid = template:gsub("[xy]", function(char)
|
||||||
local n = char == 'x'
|
local n = char == "x"
|
||||||
and math.random(0, 0xf)
|
and math.random(0, 0xf)
|
||||||
or math.random(8, 0xb)
|
or math.random(8, 0xb)
|
||||||
return string.format('%x', n)
|
return string.format("%x", n)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return uuid
|
return uuid
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
local Presence = {}
|
local Presence = {}
|
||||||
|
|
||||||
local Log = require("lib.log")
|
local log = require("lib.log")
|
||||||
local files = require("presence.files")
|
local files = require("presence.files")
|
||||||
local msgpack = require("deps.msgpack")
|
local msgpack = require("deps.msgpack")
|
||||||
local DiscordRPC = require("presence.discord")
|
local Discord = require("presence.discord")
|
||||||
|
|
||||||
function Presence:setup(options)
|
function Presence:setup(options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
self.options = options
|
self.options = options
|
||||||
|
|
||||||
-- Initialize logger with provided options
|
-- Initialize logger with provided options
|
||||||
self.log = Log {
|
self.log = log:init({
|
||||||
level = options.log_level or vim.g.presence_log_level,
|
level = options.log_level or vim.g.presence_log_level,
|
||||||
}
|
})
|
||||||
|
|
||||||
self.log:debug("Setting up plugin...")
|
self.log:debug("Setting up plugin...")
|
||||||
|
|
||||||
@ -42,8 +42,9 @@ function Presence:setup(options)
|
|||||||
self.client_id = options.client_id
|
self.client_id = options.client_id
|
||||||
end
|
end
|
||||||
|
|
||||||
self.discord = DiscordRPC:new({
|
self.discord = Discord:init({
|
||||||
client_id = self.client_id,
|
client_id = self.client_id,
|
||||||
|
ipc_path = self.get_ipc_path(),
|
||||||
logger = self.log,
|
logger = self.log,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -56,8 +57,8 @@ function Presence:setup(options)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Send a nil activity to unset the presence
|
-- Send a nil activity to unset the presence
|
||||||
function Presence:cancel_presence()
|
function Presence:cancel()
|
||||||
self.log:debug("Nullifying Discord presence...")
|
self.log:debug("Canceling Discord presence...")
|
||||||
|
|
||||||
if not self.discord:is_connected() then
|
if not self.discord:is_connected() then
|
||||||
return
|
return
|
||||||
@ -74,14 +75,14 @@ function Presence:cancel_presence()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Send command to cancel the presence for all other remote Neovim instances
|
-- 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)
|
self:get_nvim_socket_addrs(function(sockets)
|
||||||
for i = 1, #sockets do
|
for i = 1, #sockets do
|
||||||
local nvim_socket = sockets[i]
|
local nvim_socket = sockets[i]
|
||||||
|
|
||||||
-- Skip if the nvim socket is the current instance
|
-- Skip if the nvim socket is the current instance
|
||||||
if nvim_socket ~= vim.v.servername then
|
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)
|
self:call_remote_nvim_instance(nvim_socket, command)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -161,6 +162,26 @@ function Presence:authorize(on_done)
|
|||||||
end)
|
end)
|
||||||
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
|
-- Gets the file path of the current vim buffer
|
||||||
function Presence.get_current_buffer(on_buffer)
|
function Presence.get_current_buffer(on_buffer)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
@ -270,7 +291,7 @@ function Presence:update_for_buffer(buffer)
|
|||||||
self.log:debug(string.format("Setting activity for %s...", buffer))
|
self.log:debug(string.format("Setting activity for %s...", buffer))
|
||||||
|
|
||||||
-- Send command to cancel presence for all remote Neovim instances
|
-- Send command to cancel presence for all remote Neovim instances
|
||||||
self:cancel_all_remote_presences()
|
self:cancel_all_remote_instances()
|
||||||
|
|
||||||
-- Parse vim buffer
|
-- Parse vim buffer
|
||||||
local filename = self.get_filename(buffer)
|
local filename = self.get_filename(buffer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user