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")
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")
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
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
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)
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_client_id = "793271441293967371"
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
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_reading_text = "Reading %s"
let g:presence_workspace_text = "Working on %s"
let g:presence_line_number_text = "Line %s out of %s"
```
## Troubleshooting

@ -3,7 +3,7 @@ function presence#SetAutoCmds()
augroup presence_events
autocmd!
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 VimLeavePre * lua package.loaded.presence:unregister_self()
endif

@ -91,9 +91,10 @@ function Presence:setup(options)
-- General options
self:set_option("auto_update", 1)
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("neovim_image_text", "The One True Text Editor")
self:set_option("enable_line_number", false)
-- Status text options
self:set_option("editing_text", "Editing %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("reading_text", "Reading %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()
if not discord_socket then
@ -459,12 +461,11 @@ end
-- Update Rich Presence for the provided vim buffer
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()
-- 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))
@ -502,16 +503,39 @@ function Presence:update_for_buffer(buffer, should_debounce)
state = status_text,
assets = assets,
timestamps = {
start = activity_set_at,
start = relative_activity_set_at,
},
}
-- Get the current line number and line count if the user has set the enable_line_number option
if self.options.enable_line_number == 1 then
self.log:debug("Getting line number for current buffer...")
local line_number = vim.api.nvim_win_get_cursor(0)[1]
local line_count = vim.api.nvim_buf_line_count(0)
local line_number_text = self.options.line_number_text
activity.details = type(line_number_text) == "function"
and line_number_text(line_number, line_count)
or string.format(line_number_text, line_number, line_count)
self.workspace = nil
self.last_activity = {
file = buffer,
set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = nil,
}
else
-- Include project details if available and if the user hasn't set the enable_line_number option
self.log:debug(string.format("Getting project name for %s...", parent_dirpath))
local workspace_text = self.options.workspace_text
local project_name, project_path = self:get_project_name(parent_dirpath)
-- Include project details if available
if project_name then
self.log:debug(string.format("Detected project: %s", project_name))
activity.details = type(workspace_text) == "function"
@ -522,6 +546,7 @@ function Presence:update_for_buffer(buffer, should_debounce)
self.last_activity = {
file = buffer,
set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = project_path,
}
@ -536,6 +561,7 @@ function Presence:update_for_buffer(buffer, should_debounce)
updated_at = activity_set_at,
}
end
else
self.log:debug("No project detected")
@ -543,6 +569,7 @@ function Presence:update_for_buffer(buffer, should_debounce)
self.last_activity = {
file = buffer,
set_at = activity_set_at,
relative_set_at = relative_activity_set_at,
workspace = nil,
}
@ -558,6 +585,7 @@ function Presence:update_for_buffer(buffer, should_debounce)
activity.details = workspace_text
end
end
end
-- Sync activity to all peers
self:sync_self_activity()
@ -577,14 +605,13 @@ Presence.update = Presence.discord_event(function(self, buffer, should_debounce)
-- Default update to not debounce by default
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
local last_updated_at = self.last_activity.set_at
local debounce_timeout = self.options.debounce_timeout
local should_skip =
should_debounce and
debounce_timeout and
self.last_activity.file == buffer and
last_updated_at and os.time() - last_updated_at <= debounce_timeout
if should_skip then