Skip to content

Commit

Permalink
feat(lazygit): allow customizing the lazygit theme. Check the code to…
Browse files Browse the repository at this point in the history
… change the hl group mapping. Fixes #2846
  • Loading branch information
folke committed Mar 27, 2024
1 parent 6ed771d commit bb1480a
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions lua/lazyvim/util/lazygit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ local M = setmetatable({}, {
end,
})

---@alias LazyGitColor {fg?:string, bg?:string, bold?:boolean}

---@class LazyGitTheme: table<number, LazyGitColor>
---@field activeBorderColor LazyGitColor
---@field cherryPickedCommitBgColor LazyGitColor
---@field cherryPickedCommitFgColor LazyGitColor
---@field defaultFgColor LazyGitColor
---@field inactiveBorderColor LazyGitColor
---@field optionsTextColor LazyGitColor
---@field searchingActiveBorderColor LazyGitColor
---@field selectedLineBgColor LazyGitColor
---@field unstagedChangesColor LazyGitColor
M.theme = {
[241] = { fg = "Special" },
activeBorderColor = { fg = "MatchParen", bold = true },
cherryPickedCommitBgColor = { fg = "Identifier" },
cherryPickedCommitFgColor = { fg = "Function" },
defaultFgColor = { fg = "Normal" },
inactiveBorderColor = { fg = "FloatBorder" },
optionsTextColor = { fg = "Function" },
searchingActiveBorderColor = { fg = "MatchParen", bold = true },
selectedLineBgColor = { bg = "Visual" }, -- set to `default` to have no background colour
unstagedChangesColor = { fg = "DiagnosticError" },
}

M.theme_path = vim.fn.stdpath("cache") .. "/lazygit-theme.yml"

-- re-create config file on startup
Expand Down Expand Up @@ -63,22 +88,37 @@ function M.set_ansi_color(idx, color)
io.write(("\27]4;%d;%s\7"):format(idx, color))
end

---@param v LazyGitColor
---@return string[]
function M.get_color(v)
---@type string[]
local color = {}
if v.fg then
color[1] = LazyVim.ui.color(v.fg)
elseif v.bg then
color[1] = LazyVim.ui.color(v.bg, true)
end
if v.bold then
table.insert(color, "bold")
end
return color
end

function M.update_config()
-- LazyGit uses color 241 a lot, so also set it to a nice color
-- pcall, since some terminals don't like this
pcall(M.set_ansi_color, 241, LazyVim.ui.color("Special") or "blue")

local theme = {
activeBorderColor = { LazyVim.ui.color("MatchParen") or "orange", "bold" },
cherryPickedCommitBgColor = { "cyan" },
cherryPickedCommitFgColor = { "blue" },
defaultFgColor = { "default" },
inactiveBorderColor = { LazyVim.ui.color("FloatBorder") or "blue" },
optionsTextColor = { "blue" },
searchingActiveBorderColor = { LazyVim.ui.color("MatchParen") or "orange", "bold" },
selectedLineBgColor = { LazyVim.ui.color("Visual", true) }, -- set to `default` to have no background colour
unstagedChangesColor = { "red" },
}
---@type table<string, string[]>
local theme = {}

for k, v in pairs(M.theme) do
if type(k) == "number" then
local color = M.get_color(v)
-- LazyGit uses color 241 a lot, so also set it to a nice color
-- pcall, since some terminals don't like this
pcall(M.set_ansi_color, k, color[1])
else
theme[k] = M.get_color(v)
end
end

local config = [[
os:
editPreset: "nvim-remote"
Expand Down

0 comments on commit bb1480a

Please sign in to comment.