Skip to content

Commit

Permalink
Fix: Constrain highlights to DashboardHeader section and remove BufLe…
Browse files Browse the repository at this point in the history
…ave clean event (#6)

* Fix highlight issue

* Remove print and improve comments

* Fix highlight disappearing when opening telescope/other buffers along with the dashboard

* Add comment and temporary extra spaces fix

* Cleanup unneeded arguments for add_highlight
  • Loading branch information
juansalvatore authored Jun 28, 2024
1 parent 3d20b9a commit 0ff0bdb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
Empty file added NUL
Empty file.
28 changes: 14 additions & 14 deletions lua/git-dashboard-nvim/heatmap/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@ HeatmapUtils.generate_ascii_heatmap = function(
ascii_heatmap = ascii_heatmap .. "\n\n"
end

-- add highlights to the heatmap based on the config settings
vim.api.nvim_create_autocmd("FileType", {
pattern = { "dashboard", "alpha" },
callback = function()
Highlights.add_highlights(config, current_date_info, branch_label, title)

-- hide cursor
if config.hide_cursor == true then
vim.api.nvim_command("hi Cursor blend=100")
vim.api.nvim_command("set guicursor+=a:Cursor/lCursor")
end
end,
})

-- horizontal heatmap
if config.is_horizontal then
-- add month labels
Expand Down Expand Up @@ -204,6 +190,20 @@ HeatmapUtils.generate_ascii_heatmap = function(
ascii_heatmap = string.rep("\n", padding) .. ascii_heatmap
end

-- add highlights to the heatmap based on the config settings
vim.api.nvim_create_autocmd("FileType", {
pattern = { "dashboard", "alpha" },
callback = function()
Highlights.add_highlights(config, current_date_info, branch_label, title)

-- hide cursor
if config.hide_cursor == true then
vim.api.nvim_command("hi Cursor blend=100")
vim.api.nvim_command("set guicursor+=a:Cursor/lCursor")
end
end,
})

return ascii_heatmap
end

Expand Down
40 changes: 29 additions & 11 deletions lua/git-dashboard-nvim/highlights/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
--[[
Sets the highlights for the dashboard header section
Highlights will only be applied to the DashboardHeader section of the buffer
For for dashboard.nvim the containedin option works well, but for alpha.nvim
it doesn't prevent the highlights from being applied to the entire buffer
so we added temporary whitespace to the beginning of the match to prevent
easily matching other sections of the buffer
]]

Highlights = {}

---@param group_name string
---@param match string
---@param fg_color string
Highlights._add_highlight_group = function(group_name, match, fg_color)
vim.cmd("highlight " .. group_name .. " guifg=" .. fg_color)
vim.cmd('call matchadd("' .. group_name .. '", "' .. match .. '")')

-- Ensure proper escaping of special characters in `match`
local pattern = vim.fn.escape(match, "/")

-- Construct the syntax match command, using containedin ensures that the match is only applied to the DashboardHeader section
local cmd = string.format("syntax match %s /%s/ containedin=DashboardHeader", group_name, pattern)
vim.cmd(cmd)
end

---@param config Config
Expand All @@ -28,16 +45,16 @@ Highlights.add_highlights = function(config, current_date_info, branch_label, ti

for i = 1, #config.days do
Highlights._add_highlight_group(
"DashboardHeaderDay",
config.days[i]:sub(1, 3),
"DashboardHeaderDay" .. i,
" " .. config.days[i]:sub(1, 3) .. config.day_label_gap,
config.colors.days_and_months_labels
)
end

for i = 1, current_date_info.current_month do
Highlights._add_highlight_group(
"DashboardHeaderMonth",
config.months[i]:sub(1, 3),
config.months[i]:sub(1, 3) .. " ",
config.colors.days_and_months_labels
)
end
Expand All @@ -51,20 +68,21 @@ Highlights.add_highlights = function(config, current_date_info, branch_label, ti
end

-- add highlight to match any number
vim.cmd("call matchadd('DashboardHeaderMonth', '\\d\\+')")
-- vim.cmd("call matchadd('DashboardHeaderMonth', '\\d\\+')")
Highlights._add_highlight_group(
"DashboardHeaderMonth",
"\\d\\+",
config.colors.days_and_months_labels
)

Highlights._add_highlight_group("DashboardHeaderTitle", title, config.colors.dashboard_title)

Highlights._add_highlight_group(
"DashboardHeaderBranch",
branch_label,
" " .. branch_label,
config.colors.branch_highlight
)
vim.cmd.autocmd(
"BufLeave",
"*",
"highlight clear DashboardHeaderEmptySquare | highlight clear DashboardHeaderDay | highlight clear DashboardHeaderMonth | highlight clear DashboardHeaderFilledSquare | highlight clear DashboardHeaderTitle | highlight clear DashboardHeaderBranch"
)

-- set cursor color to white when leaving the buffer
vim.cmd.autocmd("BufLeave", "*", "highlight Cursor blend=0")
vim.cmd.autocmd("BufLeave", "*", "set guicursor+=a:Cursor/lCursor")
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe("config", function()
branch_highlight = "#8DC07C",
dashboard_title = "#a3cc96",
},
basepoints = { "master", "main" },
use_git_username_as_author = false,
})
end)

Expand Down Expand Up @@ -145,6 +147,8 @@ describe("config", function()
branch_highlight = "#8DC07C",
dashboard_title = "#a3cc96",
},
basepoints = { "master", "main" },
use_git_username_as_author = false,
})
end)
end)

0 comments on commit 0ff0bdb

Please sign in to comment.