From bb1480a6b9ca9e524450192423eac88ba2a32e18 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 27 Mar 2024 11:18:44 +0100 Subject: [PATCH] feat(lazygit): allow customizing the lazygit theme. Check the code to change the hl group mapping. Fixes #2846 --- lua/lazyvim/util/lazygit.lua | 70 ++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/lua/lazyvim/util/lazygit.lua b/lua/lazyvim/util/lazygit.lua index 94a561a2..7a7af81e 100644 --- a/lua/lazyvim/util/lazygit.lua +++ b/lua/lazyvim/util/lazygit.lua @@ -7,6 +7,31 @@ local M = setmetatable({}, { end, }) +---@alias LazyGitColor {fg?:string, bg?:string, bold?:boolean} + +---@class LazyGitTheme: table +---@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 @@ -63,22 +88,37 @@ function M.set_ansi_color(idx, color) io.write(("\27]4;%d;%s\7"):format(idx, 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") +---@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() + ---@type table + 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 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" }, - } local config = [[ os: editPreset: "nvim-remote"