Skip to content

Commit

Permalink
feat: Blacklist repositories (#41)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Lamby777 and jiriks74 authored May 14, 2024
1 parent ab97802 commit 883347d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 47 additions & 14 deletions lua/presence/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 (
Expand All @@ -716,22 +719,34 @@ function Presence:check_blacklist(buffer, parent_dirpath, project_dirpath)
end
end

return false
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

-- 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
if type(self.options.buttons) == "table" then
local is_plural = #self.options.buttons > 1
local s = is_plural and "s" or ""
self.log:debug(string.format("Using custom-defined button%s", s))
for _, val in pairs(blacklist_repos_table) do
if buffer:match(val) == buffer then
return true
end

return self.options.buttons
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

-- Retrieve the git repository URL
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([["]], [[\"]])
Expand All @@ -741,7 +756,24 @@ function Presence:get_buttons(buffer, parent_dirpath)
-- 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
if type(self.options.buttons) == "table" then
local is_plural = #self.options.buttons > 1
local s = is_plural and "s" or ""
self.log:debug(string.format("Using custom-defined button%s", s))

return self.options.buttons
end

-- Retrieve the git repository URL
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 883347d

Please sign in to comment.