1
0
Fork 0
mirror of https://github.com/jiriks74/presence.nvim synced 2024-12-29 19:32:05 +01:00

Add support for custom text formatter options

Resolves #39
This commit is contained in:
Andrew Kwon 2021-10-18 22:12:44 -07:00
parent 84bf65287f
commit 11adcec0db
2 changed files with 28 additions and 21 deletions

View file

@ -44,13 +44,13 @@ require("presence"):setup({
buttons = true, -- Configure Rich Presence button(s), either a boolean to enable/disable, a static table (`{{ label = "<label>", url = "<url>" }, ...}`, or a function(buffer: string, repo_url: string|nil): table) buttons = true, -- Configure Rich Presence button(s), either a boolean to enable/disable, a static table (`{{ label = "<label>", url = "<url>" }, ...}`, or a function(buffer: string, repo_url: string|nil): table)
-- 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 (either string or function(filename: string): string)
file_explorer_text = "Browsing %s", -- Format string rendered when browsing a file explorer file_explorer_text = "Browsing %s", -- Format string rendered when browsing a file explorer (either string or function(file_explorer_name: string): string)
git_commit_text = "Committing changes", -- Format string rendered when commiting changes in git git_commit_text = "Committing changes", -- Format string rendered when committing changes in git (either string or function(filename: string): string)
plugin_manager_text = "Managing plugins", -- Format string rendered when managing plugins plugin_manager_text = "Managing plugins", -- Format string rendered when managing plugins (either string or function(plugin_manager_name: string): string)
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 (either string or function(filename: string): string)
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", -- Format string rendered when in a git repository (either string or function(project_name: string|nil, filename: string): string)
line_number_text = "Line %s out of %s", -- Line number format string (for when enable_line_number is set to true) line_number_text = "Line %s out of %s", -- Format string rendered when `enable_line_number` is set to true (either string or function(line_number: number, line_count: number): string)
}) })
``` ```

View file

@ -442,6 +442,17 @@ function Presence.get_file_extension(path)
return path:match("^.+%.(.+)$") return path:match("^.+%.(.+)$")
end end
-- Format any status text via options and support custom formatter functions
function Presence:format_status_text(status_type, ...)
local option_name = string.format("%s_text", status_type)
local text_option = self.options[option_name]
if type(text_option) == "function" then
return text_option(...)
else
return string.format(text_option, ...)
end
end
-- Get the status text for the current buffer -- Get the status text for the current buffer
function Presence:get_status_text(filename) function Presence:get_status_text(filename)
local file_explorer = file_explorers[vim.bo.filetype:match "[^%d]+"] local file_explorer = file_explorers[vim.bo.filetype:match "[^%d]+"]
@ -449,21 +460,21 @@ function Presence:get_status_text(filename)
local plugin_manager = plugin_managers[vim.bo.filetype] local plugin_manager = plugin_managers[vim.bo.filetype]
if file_explorer then if file_explorer then
return string.format(self.options.file_explorer_text, file_explorer) return self:format_status_text("file_explorer", file_explorer)
elseif plugin_manager then elseif plugin_manager then
return string.format(self.options.plugin_manager_text, plugin_manager) return self:format_status_text("plugin_manager", plugin_manager)
end end
if not filename or filename == "" then return nil end if not filename or filename == "" then return nil end
if vim.bo.modifiable and not vim.bo.readonly then if vim.bo.modifiable and not vim.bo.readonly then
if vim.bo.filetype == "gitcommit" then if vim.bo.filetype == "gitcommit" then
return string.format(self.options.git_commit_text, filename) return self:format_status_text("git_commit", filename)
elseif filename then elseif filename then
return string.format(self.options.editing_text, filename) return self:format_status_text("editing", filename)
end end
elseif filename then elseif filename then
return string.format(self.options.reading_text, filename) return self:format_status_text("reading", filename)
end end
end end
@ -819,11 +830,9 @@ function Presence:update_for_buffer(buffer, should_debounce)
local line_number = vim.api.nvim_win_get_cursor(0)[1] local line_number = vim.api.nvim_win_get_cursor(0)[1]
local line_count = vim.api.nvim_buf_line_count(0) local line_count = vim.api.nvim_buf_line_count(0)
local line_number_text = self.options.line_number_text local line_number_text = self:format_status_text("line_number", line_number, line_count)
activity.details = type(line_number_text) == "function" activity.details = line_number_text
and line_number_text(line_number, line_count)
or string.format(line_number_text, line_number, line_count)
self.workspace = nil self.workspace = nil
self.last_activity = { self.last_activity = {
@ -835,14 +844,10 @@ function Presence:update_for_buffer(buffer, should_debounce)
} }
else else
-- Include project details if available and if the user hasn't set the enable_line_number option -- Include project details if available and if the user hasn't set the enable_line_number option
local workspace_text = self.options.workspace_text
if project_name then if project_name then
self.log:debug(string.format("Detected project: %s", project_name)) self.log:debug(string.format("Detected project: %s", project_name))
activity.details = type(workspace_text) == "function" activity.details = self:format_status_text("workspace", project_name, buffer)
and workspace_text(project_name, buffer)
or string.format(workspace_text, project_name)
self.workspace = project_path self.workspace = project_path
self.last_activity = { self.last_activity = {
@ -879,6 +884,8 @@ function Presence:update_for_buffer(buffer, should_debounce)
-- When no project is detected, set custom workspace text if: -- When no project is detected, set custom workspace text if:
-- * The custom function returns custom workspace text -- * The custom function returns custom workspace text
-- * The configured workspace text does not contain a directive -- * The configured workspace text does not contain a directive
-- (can't use the `format_status_text` method here)
local workspace_text = self.options.workspace_text
if type(workspace_text) == "function" then if type(workspace_text) == "function" then
local custom_workspace_text = workspace_text(nil, buffer) local custom_workspace_text = workspace_text(nil, buffer)
if custom_workspace_text then if custom_workspace_text then