Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,29 @@ Require the plugin and call `setup` with a config table with one or more of the
-- The setup config table shows all available config options with their default values:
require("neocord").setup({
-- General options
logo = "auto", -- "auto" or url
logo_tooltip = nil, -- nil or string
main_image = "language", -- "language" or "logo"
client_id = "1157438221865717891", -- 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 = 10, -- Number of seconds to debounce events (or calls to `:lua package.loaded.presence:update(<filename>, true)`)
blacklist = {}, -- A list of strings or Lua patterns that disable Rich Presence if the current file name, path, or workspace matches
file_assets = {}, -- Custom file asset definitions keyed by file names and extensions (see default config at `lua/presence/file_assets.lua` for reference)
show_time = true, -- Show the timer
global_timer = false, -- if set true, timer won't update when any event are triggered
logo = "auto", -- "auto" or url
logo_tooltip = nil, -- nil or string
main_image = "language", -- "language" or "logo"
client_id = "1157438221865717891", -- 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 = 10, -- Number of seconds to debounce events (or calls to `:lua package.loaded.presence:update(<filename>, true)`)
blacklist = {}, -- A list of strings or Lua patterns that disable Rich Presence if the current file name, path, or workspace matches
file_assets = {}, -- Custom file asset definitions keyed by file names and extensions (see default config at `lua/presence/file_assets.lua` for reference)
show_time = true, -- Show the timer
global_timer = false, -- if set true, timer won't update when any event are triggered
global_timer = false, -- if set true, timer won't update when any event are triggered
project_detection_fallback_fn = nil -- project detection function executed if the working directory is not a git repository (either nil or function(file_path: string|nil): string, string)
-- cont'd: first return value is project name, second return value is full project path

-- Rich Presence text options
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 (either string or function(file_explorer_name: string): string)
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 (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 (either string or function(filename: 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", -- Format string rendered when `enable_line_number` is set to true (either string or function(line_number: number, line_count: number): string)
terminal_text = "Using Terminal", -- Format string rendered when in terminal mode.
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 (either string or function(file_explorer_name: string): string)
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 (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 (either string or function(filename: 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", -- Format string rendered when `enable_line_number` is set to true (either string or function(line_number: number, line_count: number): string)
terminal_text = "Using Terminal", -- Format string rendered when in terminal mode.
})
```

Expand Down
54 changes: 32 additions & 22 deletions lua/neocord/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function neocord:setup(...)
if self.os.name then
local setup_message = self.os.is_wsl
and string.format(setup_message_fmt .. " in WSL (%s)", self.os.name, vim.inspect(wsl_distro_name))
or string.format(setup_message_fmt, self.os.name)
or string.format(setup_message_fmt, self.os.name)
self.log:debug(setup_message)
else
self.log:error(string.format("Unable to detect operating system: %s", vim.inspect(vim.loop.os_uname())))
Expand All @@ -68,11 +68,11 @@ function neocord:setup(...)
end

-- General options
utils.set_option(self, "auto_update", true) -- Your discord application id
utils.set_option(self, "auto_update", true) -- Your discord application id
utils.set_option(self, "client_id", "1157438221865717891") -- Your discord application id
utils.set_option(self, "logo", "auto") -- auto or url
utils.set_option(self, "logo_tooltip", nil) -- nil or string
utils.set_option(self, "main_image", "language") -- "language" or "logo"
utils.set_option(self, "logo", "auto") -- auto or url
utils.set_option(self, "logo_tooltip", nil) -- nil or string
utils.set_option(self, "main_image", "language") -- "language" or "logo"
utils.set_option(self, "editing_text", "Editing %s")
utils.set_option(self, "file_explorer_text", "Browsing %s")
utils.set_option(self, "git_commit_text", "Committing changes")
Expand All @@ -85,6 +85,7 @@ function neocord:setup(...)
utils.set_option(self, "line_number_text", "Line %s out of %s")
utils.set_option(self, "show_time", true)
utils.set_option(self, "global_timer", false)
utils.set_option(self, "project_detection_fallback_fn", nil)
utils.set_option(self, "file_assets", {})
for name, asset in pairs(default_file_assets) do
if not self.options.file_assets[name] then
Expand Down Expand Up @@ -339,6 +340,11 @@ end
-- Gets the current project name
function neocord:get_project_name(file_path)
if not file_path then
self.log:info("Not a file, checking if fallback function defined...")
if self.options.project_detection_fallback_fn then
self.log:info("Fallback function defined, executing fallback function")
return self.options.project_detection_fallback_fn(file_path)
end
return nil
end

Expand All @@ -349,13 +355,17 @@ function neocord:get_project_name(file_path)
-- Might want to run this in a background process depending on performance
local project_path_cmd = "git rev-parse --show-toplevel"
project_path_cmd = file_path and string.format([[bash -c 'cd "%s" && %s']], file_path, project_path_cmd)
or project_path_cmd
or project_path_cmd

local project_path = vim.fn.system(project_path_cmd)
project_path = vim.trim(project_path)

if project_path:find("fatal.*") then
self.log:info("Not a git repository, skipping...")
self.log:info("Not a git repository, checking if fallback function defined...")
if self.options.project_detection_fallback_fn then
self.log:info("Fallback function defined, executing fallback function")
return self.options.project_detection_fallback_fn(file_path)
end
return nil
end
if vim.v.shell_error ~= 0 or #project_path == 0 then
Expand Down Expand Up @@ -401,7 +411,7 @@ end
-- Get the status text for the current buffer
function neocord:get_status_text(filename)
local file_explorer = file_explorers[vim.bo.filetype:match("[^%d]+")]
or file_explorers[(filename or ""):match("[^%d]+")]
or file_explorers[(filename or ""):match("[^%d]+")]
local plugin_manager = plugin_managers[vim.bo.filetype]
local dashboard = dashboards[vim.bo.filetype]
local terminal = vim.api.nvim_get_mode()["mode"] == "t"
Expand Down Expand Up @@ -612,19 +622,19 @@ function neocord:check_blacklist(buffer, parent_dirpath, project_dirpath)
end
-- Match parent either by Lua pattern or by plain string
local is_parent_directory_blacklisted = parent_dirpath
and (
(parent_dirpath:match(val) == parent_dirpath or parent_dirname:match(val) == parent_dirname)
or (parent_dirpath:find(val, nil, true) or parent_dirname:find(val, nil, true))
)
and (
(parent_dirpath:match(val) == parent_dirpath or parent_dirname:match(val) == parent_dirname)
or (parent_dirpath:find(val, nil, true) or parent_dirname:find(val, nil, true))
)
if is_parent_directory_blacklisted then
return true
end
-- Match project either by Lua pattern or by plain string
local is_project_directory_blacklisted = project_dirpath
and (
(project_dirpath:match(val) == project_dirpath or project_dirname:match(val) == project_dirname)
or (project_dirpath:find(val, nil, true) or project_dirname:find(val, nil, true))
)
and (
(project_dirpath:match(val) == project_dirpath or project_dirname:match(val) == project_dirname)
or (project_dirpath:find(val, nil, true) or project_dirname:find(val, nil, true))
)
if is_project_directory_blacklisted then
return true
end
Expand Down Expand Up @@ -743,8 +753,8 @@ function neocord:update_for_buffer(buffer, should_debounce)
-- 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 = self.options.global_timer == 1 and global_start
or should_debounce and self.last_activity.relative_set_at
or os.time()
or should_debounce and self.last_activity.relative_set_at
or os.time()

self.log:debug(string.format("Setting activity for %s...", buffer and #buffer > 0 and buffer or "unnamed buffer"))

Expand Down Expand Up @@ -840,7 +850,7 @@ function neocord:update_for_buffer(buffer, should_debounce)
and {
start = self.workspaces[project_path].started_at,
}
or nil
or nil
else
self.workspaces[project_path] = {
started_at = activity_set_at,
Expand Down Expand Up @@ -902,9 +912,9 @@ neocord.update = neocord.discord_event(function(self, buffer, should_debounce)
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 last_updated_at
and os.time() - last_updated_at <= debounce_timeout
and debounce_timeout
and last_updated_at
and os.time() - last_updated_at <= debounce_timeout

if should_skip then
local message_fmt = "Last activity sent was within %d seconds ago, skipping..."
Expand Down