diff --git a/README.md b/README.md index a8563de..4683b09 100644 --- a/README.md +++ b/README.md @@ -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(, true)`) + debounce_timeout = 10, -- Number of seconds to debounce events (or calls to `:lua package.loaded.presence:update(, 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 diff --git a/autoload/presence.vim b/autoload/presence.vim index eb149ac..b65e7af 100644 --- a/autoload/presence.vim +++ b/autoload/presence.vim @@ -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 diff --git a/lua/presence/init.lua b/lua/presence/init.lua index 32260da..c95b79b 100644 --- a/lua/presence/init.lua +++ b/lua/presence/init.lua @@ -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,60 +503,87 @@ function Presence:update_for_buffer(buffer, should_debounce) state = status_text, assets = assets, timestamps = { - start = activity_set_at, + start = relative_activity_set_at, }, } - 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) + -- 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...") - -- Include project details if available - if project_name then - self.log:debug(string.format("Detected project: %s", project_name)) + 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(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, - 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") + 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 - -- 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 + 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) + + if project_name then + + 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 - elseif not workspace_text:find("%s") then - activity.details = workspace_text end end @@ -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