Skip to content

Commit b437baf

Browse files
barrettruthibhagwan
authored andcommitted
feat(git): consistent highlighting in branches/worktrees pickers
1 parent 2e29d5f commit b437baf

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

lua/fzf-lua/providers/git.lua

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,118 @@ M.blame = function(opts)
267267
return git_cmd(opts)
268268
end
269269

270+
---@param line string
271+
---@return string
272+
local function highlight_branch_line(line)
273+
local u = FzfLua.utils
274+
local ansi = u.ansi_codes
275+
line = u.strip_ansi_coloring(line)
276+
local leader = line:sub(1, 2)
277+
local rest = line:sub(3)
278+
279+
local detached_inner = rest:match("^%(([^)]+)%)")
280+
if detached_inner and
281+
(detached_inner:match("^HEAD detached") or detached_inner:match("^no branch")) then
282+
local head = ansi.grey("(") .. ansi.green(detached_inner) .. ansi.grey(")")
283+
local after = rest:sub(#detached_inner + 3)
284+
local ws, sha, subject_ws, subject = after:match("^(%s+)(%x%x%x%x%x%x%x+)(%s+)(.*)$")
285+
if ws and sha then
286+
return leader .. head .. ws .. ansi.yellow(sha) .. subject_ws .. subject
287+
end
288+
return leader .. head .. after
289+
end
290+
291+
local branch_name, ws_after_name, after = rest:match("^(%S+)(%s+)(.*)$")
292+
if not branch_name then return line end
293+
294+
local colored_branch
295+
if leader:sub(1, 1) == "*" then
296+
colored_branch = ansi.green(branch_name)
297+
elseif leader:sub(1, 1) == "+" then
298+
colored_branch = ansi.cyan(branch_name)
299+
elseif branch_name:match("^remotes/") then
300+
colored_branch = ansi.red(branch_name)
301+
else
302+
colored_branch = branch_name
303+
end
304+
305+
if after:match("^%->") then
306+
return leader .. colored_branch .. ws_after_name .. after
307+
end
308+
309+
local sha, body_ws, body = after:match("^(%x%x%x%x%x%x%x+)(%s+)(.*)$")
310+
if not sha then
311+
return leader .. colored_branch .. ws_after_name .. after
312+
end
313+
314+
local function color_tracking(content)
315+
local ref, suffix = content:match("^([^:]+)(:.*)$")
316+
if ref and suffix then
317+
return ansi.grey("[") .. ansi.blue(ref) .. ansi.grey(suffix) .. ansi.grey("]")
318+
end
319+
return ansi.grey("[") .. ansi.blue(content) .. ansi.grey("]")
320+
end
321+
322+
if leader:sub(1, 1) == "+" then
323+
local wt_paren, remainder = body:match("^(%b())(.*)$")
324+
if wt_paren then
325+
local colored_wt = ansi.grey("(") .. ansi.cyan(wt_paren:sub(2, -2)) .. ansi.grey(")")
326+
remainder = remainder:gsub("^(%s*)(%[)([^%]]+)(%])", function(ws, _, content, _)
327+
return ws .. color_tracking(content)
328+
end)
329+
body = colored_wt .. remainder
330+
end
331+
else
332+
body = body:gsub("^(%[)([^%]]+)(%])", function(_, content, _)
333+
return color_tracking(content)
334+
end)
335+
end
336+
337+
return leader .. colored_branch .. ws_after_name .. ansi.yellow(sha) .. body_ws .. body
338+
end
339+
340+
---@param line string
341+
---@return string
342+
local function highlight_worktree_line(line)
343+
local ansi = FzfLua.utils.ansi_codes
344+
local path_s, padding, rest = line:match("^(%S+)(%s+)(.*)$")
345+
if not path_s then return line end
346+
347+
local sha, sha_ws, body = rest:match("^(%x%x%x%x%x%x%x+)(%s+)(.*)$")
348+
if not sha then
349+
body = rest
350+
end
351+
352+
body = body:gsub("^(%[)([^%]]+)(%])", function(lb, name, rb)
353+
return ansi.grey(lb) .. ansi.green(name) .. ansi.grey(rb)
354+
end)
355+
body = body:gsub("^(%()(detached HEAD)(%))", function(lp, text, rp)
356+
return ansi.grey(lp) .. ansi.green(text) .. ansi.grey(rp)
357+
end)
358+
body = body:gsub("^(%()(bare)(%))", function(lp, text, rp)
359+
return ansi.grey(lp) .. ansi.grey(text) .. ansi.grey(rp)
360+
end)
361+
body = body:gsub("(%s)(locked)(%s*)$", function(s1, w, s2)
362+
return s1 .. ansi.magenta(w) .. s2
363+
end)
364+
body = body:gsub("(%s)(prunable)(%s*)$", function(s1, w, s2)
365+
return s1 .. ansi.red(w) .. s2
366+
end)
367+
368+
local out = ansi.blue(path_s) .. padding
369+
if sha then
370+
out = out .. ansi.yellow(sha) .. sha_ws
371+
end
372+
return out .. body
373+
end
374+
270375
---@param opts fzf-lua.config.GitBranches|{}?
271376
---@return thread?, string?, table?
272377
M.branches = function(opts)
273378
---@type fzf-lua.config.GitBranches
274379
opts = config.normalize_opts(opts, "git.branches")
275380
if not opts then return end
381+
if opts.fn_transform == nil then opts.fn_transform = highlight_branch_line end
276382
if opts.preview then
277383
local preview = path.git_cwd(opts.preview, opts)
278384
opts.preview = shell.stringify_cmd(function(items)
@@ -301,6 +407,7 @@ M.worktrees = function(opts)
301407
---@type fzf-lua.config.GitWorktrees
302408
opts = config.normalize_opts(opts, "git.worktrees")
303409
if not opts then return end
410+
if opts.fn_transform == nil then opts.fn_transform = highlight_worktree_line end
304411
if opts.preview then
305412
local preview_cmd = opts.preview
306413
opts.preview = shell.stringify_cmd(function(items)

0 commit comments

Comments
 (0)