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

Add the ability to show the current line number in presence (#16)

* fix: rewrite for latest commit
* fix: formatting
* fix: use BufEnter instead of BufRead for more stable presence updates
* fix: change vimscript variable from false to 0
Co-authored-by: Andrew Kwon <andrewshky@gmail.com>
* fix: revert back to TextChanged rather than InsertEnter, refactor bits of the codebase
* fix: formatting
* fix: luacheck error and compare options to numeric values rather than booleans
Co-authored-by: Andrew Kwon <andrewshky@gmail.com>
This commit is contained in:
vhyrro 2021-05-22 19:46:41 +00:00 committed by GitHub
parent 70a3c23ae0
commit 9fd2a30127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 50 deletions

@ -37,7 +37,8 @@ require("presence"):setup({
main_image = "neovim", -- Main image display (either "neovim" or "file") main_image = "neovim", -- Main image display (either "neovim" or "file")
client_id = "793271441293967371", -- Use your own Discord application client id (not recommended) client_id = "793271441293967371", -- Use your own Discord application client id (not recommended)
log_level = nil, -- Log messages at or above this level (one of the following: "debug", "info", "warn", "error") log_level = nil, -- Log messages at or above this level (one of the following: "debug", "info", "warn", "error")
debounce_timeout = 15, -- Number of seconds to debounce TextChanged events (or calls to `:lua package.loaded.presence:update(<filename>, true)`) debounce_timeout = 10, -- Number of seconds to debounce events (or calls to `:lua package.loaded.presence:update(<filename>, true)`)
enable_line_number = false, -- Displays the current line number instead of the current project
-- Rich Presence text options -- Rich Presence text options
editing_text = "Editing %s", -- Format string rendered when an editable file is loaded in the buffer editing_text = "Editing %s", -- Format string rendered when an editable file is loaded in the buffer
@ -46,6 +47,7 @@ require("presence"):setup({
plugin_manager_text = "Managing plugins" -- Format string rendered when managing plugins plugin_manager_text = "Managing plugins" -- Format string rendered when managing plugins
reading_text = "Reading %s" -- Format string rendered when a read-only or unmodifiable file is loaded in the buffer reading_text = "Reading %s" -- Format string rendered when a read-only or unmodifiable file is loaded in the buffer
workspace_text = "Working on %s", -- Workspace format string (either string or function(git_project_name: string|nil, buffer: string): string) workspace_text = "Working on %s", -- Workspace format string (either string or function(git_project_name: string|nil, buffer: string): string)
line_number_text = "Line %s out of %s", -- Line number format string (for when enable_line_number is set to true)
}) })
``` ```
@ -58,7 +60,8 @@ let g:presence_neovim_image_text = "The One True Text Editor"
let g:presence_main_image = "neovim" let g:presence_main_image = "neovim"
let g:presence_client_id = "793271441293967371" let g:presence_client_id = "793271441293967371"
let g:presence_log_level let g:presence_log_level
let g:presence_debounce_timeout = 15 let g:presence_debounce_timeout = 10
let g:presence_enable_line_number = 0
" Rich Presence text options " Rich Presence text options
let g:presence_editing_text = "Editing %s" let g:presence_editing_text = "Editing %s"
@ -67,6 +70,7 @@ let g:presence_git_commit_text = "Committing changes"
let g:presence_plugin_manager_text = "Managing plugins" let g:presence_plugin_manager_text = "Managing plugins"
let g:presence_reading_text = "Reading %s" let g:presence_reading_text = "Reading %s"
let g:presence_workspace_text = "Working on %s" let g:presence_workspace_text = "Working on %s"
let g:presence_line_number_text = "Line %s out of %s"
``` ```
## Troubleshooting ## Troubleshooting

@ -3,7 +3,7 @@ function presence#SetAutoCmds()
augroup presence_events augroup presence_events
autocmd! autocmd!
if exists("g:presence_auto_update") && g:presence_auto_update if exists("g:presence_auto_update") && g:presence_auto_update
autocmd BufRead * lua package.loaded.presence:update() autocmd BufEnter * lua package.loaded.presence:update()
autocmd TextChanged * lua package.loaded.presence:update(nil, true) autocmd TextChanged * lua package.loaded.presence:update(nil, true)
autocmd VimLeavePre * lua package.loaded.presence:unregister_self() autocmd VimLeavePre * lua package.loaded.presence:unregister_self()
endif endif

@ -91,9 +91,10 @@ function Presence:setup(options)
-- General options -- General options
self:set_option("auto_update", 1) self:set_option("auto_update", 1)
self:set_option("client_id", "793271441293967371") self:set_option("client_id", "793271441293967371")
self:set_option("debounce_timeout", 15) self:set_option("debounce_timeout", 10)
self:set_option("main_image", "neovim") self:set_option("main_image", "neovim")
self:set_option("neovim_image_text", "The One True Text Editor") self:set_option("neovim_image_text", "The One True Text Editor")
self:set_option("enable_line_number", false)
-- Status text options -- Status text options
self:set_option("editing_text", "Editing %s") self:set_option("editing_text", "Editing %s")
self:set_option("file_explorer_text", "Browsing %s") self:set_option("file_explorer_text", "Browsing %s")
@ -101,6 +102,7 @@ function Presence:setup(options)
self:set_option("plugin_manager_text", "Managing plugins") self:set_option("plugin_manager_text", "Managing plugins")
self:set_option("reading_text", "Reading %s") self:set_option("reading_text", "Reading %s")
self:set_option("workspace_text", "Working on %s") self:set_option("workspace_text", "Working on %s")
self:set_option("line_number_text", "Line %s out of %s")
local discord_socket = self:get_discord_socket() local discord_socket = self:get_discord_socket()
if not discord_socket then if not discord_socket then
@ -459,12 +461,11 @@ end
-- Update Rich Presence for the provided vim buffer -- Update Rich Presence for the provided vim buffer
function Presence:update_for_buffer(buffer, should_debounce) function Presence:update_for_buffer(buffer, should_debounce)
if should_debounce and self.last_activity.file == buffer then
self.log:debug(string.format("Activity already set for %s, skipping...", buffer))
return
end
local activity_set_at = os.time() local activity_set_at = os.time()
-- If we shouldn't debounce and we trigger an activity, keep this value the same.
-- Otherwise set it to the current time.
local relative_activity_set_at = should_debounce and self.last_activity.relative_set_at or os.time()
self.log:debug(string.format("Setting activity for %s...", buffer)) self.log:debug(string.format("Setting activity for %s...", buffer))
@ -502,60 +503,87 @@ function Presence:update_for_buffer(buffer, should_debounce)
state = status_text, state = status_text,
assets = assets, assets = assets,
timestamps = { timestamps = {
start = activity_set_at, start = relative_activity_set_at,
}, },
} }
self.log:debug(string.format("Getting project name for %s...", parent_dirpath)) -- Get the current line number and line count if the user has set the enable_line_number option
local workspace_text = self.options.workspace_text if self.options.enable_line_number == 1 then
local project_name, project_path = self:get_project_name(parent_dirpath) self.log:debug("Getting line number for current buffer...")
-- Include project details if available local line_number = vim.api.nvim_win_get_cursor(0)[1]
if project_name then local line_count = vim.api.nvim_buf_line_count(0)
self.log:debug(string.format("Detected project: %s", project_name)) local line_number_text = self.options.line_number_text
activity.details = type(workspace_text) == "function" activity.details = type(line_number_text) == "function"
and workspace_text(project_name, buffer) and line_number_text(line_number, line_count)
or string.format(workspace_text, project_name) or string.format(line_number_text, line_number, line_count)
self.workspace = project_path
self.last_activity = {
file = buffer,
set_at = activity_set_at,
workspace = project_path,
}
if self.workspaces[project_path] then
self.workspaces[project_path].updated_at = activity_set_at
activity.timestamps = {
start = self.workspaces[project_path].started_at,
}
else
self.workspaces[project_path] = {
started_at = activity_set_at,
updated_at = activity_set_at,
}
end
else
self.log:debug("No project detected")
self.workspace = nil self.workspace = nil
self.last_activity = { self.last_activity = {
file = buffer, file = buffer,
set_at = activity_set_at, set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = nil, workspace = nil,
} }
else
-- Include project details if available and if the user hasn't set the enable_line_number option
-- When no project is detected, set custom workspace text if: self.log:debug(string.format("Getting project name for %s...", parent_dirpath))
-- * The custom function returns custom workspace text
-- * The configured workspace text does not contain a directive local workspace_text = self.options.workspace_text
if type(workspace_text) == "function" then local project_name, project_path = self:get_project_name(parent_dirpath)
local custom_workspace_text = workspace_text(nil, buffer)
if custom_workspace_text then if project_name then
activity.details = custom_workspace_text
self.log:debug(string.format("Detected project: %s", project_name))
activity.details = type(workspace_text) == "function"
and workspace_text(project_name, buffer)
or string.format(workspace_text, project_name)
self.workspace = project_path
self.last_activity = {
file = buffer,
set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = project_path,
}
if self.workspaces[project_path] then
self.workspaces[project_path].updated_at = activity_set_at
activity.timestamps = {
start = self.workspaces[project_path].started_at,
}
else
self.workspaces[project_path] = {
started_at = activity_set_at,
updated_at = activity_set_at,
}
end
else
self.log:debug("No project detected")
self.workspace = nil
self.last_activity = {
file = buffer,
set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = nil,
}
-- When no project is detected, set custom workspace text if:
-- * The custom function returns custom workspace text
-- * The configured workspace text does not contain a directive
if type(workspace_text) == "function" then
local custom_workspace_text = workspace_text(nil, buffer)
if custom_workspace_text then
activity.details = custom_workspace_text
end
elseif not workspace_text:find("%s") then
activity.details = workspace_text
end end
elseif not workspace_text:find("%s") then
activity.details = workspace_text
end end
end end
@ -577,14 +605,13 @@ Presence.update = Presence.discord_event(function(self, buffer, should_debounce)
-- Default update to not debounce by default -- Default update to not debounce by default
if should_debounce == nil then should_debounce = false end if should_debounce == nil then should_debounce = false end
-- Debounce Rich Presence updates (default to 15 seconds): -- Debounce Rich Presence updates (default to 10 seconds):
-- https://discord.com/developers/docs/rich-presence/how-to#updating-presence -- https://discord.com/developers/docs/rich-presence/how-to#updating-presence
local last_updated_at = self.last_activity.set_at local last_updated_at = self.last_activity.set_at
local debounce_timeout = self.options.debounce_timeout local debounce_timeout = self.options.debounce_timeout
local should_skip = local should_skip =
should_debounce and should_debounce and
debounce_timeout and debounce_timeout and
self.last_activity.file == buffer and
last_updated_at and os.time() - last_updated_at <= debounce_timeout last_updated_at and os.time() - last_updated_at <= debounce_timeout
if should_skip then if should_skip then