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

feat: Blacklist repositories (#41)
All checks were successful
Luacheck / Luacheck (push) Successful in 9m40s
StyLua / StyLua (push) Successful in 20m33s

Blacklist URLs based on keywords

* Check repo URL in blacklisted

* readme update (for split blacklist config options)

* moved git repo check into its own loop

* fixed not loading if blacklist empty + other stuff ;-;

* fix(formatting): Format using StyLua to be in sync with upstream

* fix(init.lua): Change : to . so argument `self` is not created

---------

Co-authored-by: Jiří Štefka <jiri@stefka.eu>
This commit is contained in:
&Cherry 2024-05-14 09:33:20 -04:00 committed by GitHub
parent ab97802975
commit 883347db4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 14 deletions

@ -97,6 +97,7 @@ require("presence").setup({
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
blacklist = {}, -- A list of strings or Lua patterns that disable Rich Presence if the current file name, path, or workspace matches
blacklist_repos = {}, -- A blacklist that applies to git remote repo URLs instead of folder/file names
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)
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
@ -126,6 +127,7 @@ let g:presence_log_level
let g:presence_debounce_timeout = 10
let g:presence_enable_line_number = 0
let g:presence_blacklist = []
let g:presence_blacklist_repos = []
let g:presence_buttons = 1
let g:presence_file_assets = {}
let g:presence_show_time = 1

@ -130,6 +130,7 @@ function Presence:setup(...)
self:set_option("workspace_text", "Working on %s")
self:set_option("line_number_text", "Line %s out of %s")
self:set_option("blacklist", {})
self:set_option("blacklist_repos", {})
self:set_option("buttons", true)
self:set_option("show_time", true)
-- File assets options
@ -688,7 +689,8 @@ function Presence:check_blacklist(buffer, parent_dirpath, project_dirpath)
end
-- Blacklist table
local blacklist_table = self.options["blacklist"]
local blacklist_table = self.options["blacklist"] or {}
local blacklist_repos_table = self.options["blacklist_repos"] or {}
-- Loop over the values to see if the provided project/path is in the blacklist
for _, val in pairs(blacklist_table) do
@ -705,6 +707,7 @@ function Presence:check_blacklist(buffer, parent_dirpath, project_dirpath)
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 (
@ -716,9 +719,48 @@ function Presence:check_blacklist(buffer, parent_dirpath, project_dirpath)
end
end
-- check against git repo blacklist
local git_repo = Presence.get_git_repo_url(parent_dirpath)
if git_repo then
self.log:debug(string.format("Checking git repo blacklist for %s", git_repo))
else
self.log:debug("No git repo, skipping blacklist check")
return false
end
for _, val in pairs(blacklist_repos_table) do
if buffer:match(val) == buffer then
return true
end
local is_git_repo_blacklisted = git_repo
and ((git_repo:match(val) == git_repo) == git_repo or (git_repo:find(val, nil, true)))
if is_git_repo_blacklisted then
return true
end
end
return false
end
function Presence.get_git_repo_url(parent_dirpath)
local repo_url
if parent_dirpath then
-- Escape quotes in the file path
local path = parent_dirpath:gsub([["]], [[\"]])
local git_url_cmd = "git config --get remote.origin.url"
local cmd = path and string.format([[cd "%s" && %s]], path, git_url_cmd) or git_url_cmd
-- Trim and coerce empty string value to null
repo_url = vim.trim(vim.fn.system(cmd))
repo_url = repo_url ~= "" and repo_url or nil
return repo_url
end
end
-- Get either user-configured buttons or the create default "View Repository" button definition
function Presence:get_buttons(buffer, parent_dirpath)
-- User configured a static buttons table
@ -731,17 +773,7 @@ function Presence:get_buttons(buffer, parent_dirpath)
end
-- Retrieve the git repository URL
local repo_url
if parent_dirpath then
-- Escape quotes in the file path
local path = parent_dirpath:gsub([["]], [[\"]])
local git_url_cmd = "git config --get remote.origin.url"
local cmd = path and string.format([[cd "%s" && %s]], path, git_url_cmd) or git_url_cmd
-- Trim and coerce empty string value to null
repo_url = vim.trim(vim.fn.system(cmd))
repo_url = repo_url ~= "" and repo_url or nil
end
local repo_url = Presence.get_git_repo_url(parent_dirpath)
-- User configured a function to dynamically create buttons table
if type(self.options.buttons) == "function" then
@ -817,10 +849,11 @@ function Presence:update_for_buffer(buffer, should_debounce)
local project_name, project_path = self:get_project_name(parent_dirpath)
-- Check for blacklist
local is_blacklisted = #self.options.blacklist > 0 and self:check_blacklist(buffer, parent_dirpath, project_path)
local blacklist_not_empty = (#self.options.blacklist > 0 or #self.options.blacklist_repos > 0)
local is_blacklisted = blacklist_not_empty and self:check_blacklist(buffer, parent_dirpath, project_path)
if is_blacklisted then
self.last_activity.file = buffer
self.log:debug("Either project or directory name is blacklisted, skipping...")
self.log:debug("Either project, directory name, or repository URL is blacklisted. Skipping...")
self:cancel()
return
end