Skip to content

Commit 8e31ccd

Browse files
committed
feat: add Lazygit theming integration
1 parent 8a9fa27 commit 8e31ccd

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ require("astroui").setup({
113113
},
114114
},
115115
},
116+
-- Configure theming of Lazygit, set to `false` to disable
117+
lazygit = {
118+
theme_path = vim.fs.normalize(vim.fn.stdpath "cache" .. "/lazygit-theme.yml"),
119+
theme = {
120+
[241] = { fg = "Special" },
121+
activeBorderColor = { fg = "MatchParen", bold = true },
122+
cherryPickedCommitBgColor = { fg = "Identifier" },
123+
cherryPickedCommitFgColor = { fg = "Function" },
124+
defaultFgColor = { fg = "Normal" },
125+
inactiveBorderColor = { fg = "FloatBorder" },
126+
optionsTextColor = { fg = "Function" },
127+
searchingActiveBorderColor = { fg = "MatchParen", bold = true },
128+
selectedLineBgColor = { bg = "Visual" },
129+
unstagedChangesColor = { fg = "DiagnosticError" },
130+
},
131+
},
116132
}
117133
```
118134

lua/astroui/config.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
---```
3030
---@field file_icon AstroUIFileIconHighlights?
3131

32+
---@class AstroUILazygitOpts
33+
---@field theme_path string? the path to the storage location for the lazygit theme configuration
34+
---@field theme table<string|number,{fg:string?,bg:string?,bold:boolean?,reverse:boolean?,underline:boolean?,strikethrough:boolean?}>? table of highlight groups to use for the lazygit theme
35+
---@field config table? arbitrary lazygit configuration structure
36+
3237
---@class AstroUISeparators
3338
---@field none string[]? placeholder separator for elements with "no" separator, typically two empty strings
3439
---@field left string[]? Separators used for elements designated as being on the left of the statusline
@@ -187,6 +192,28 @@
187192
---}
188193
---```
189194
---@field status AstroUIStatusOpts?
195+
---Configuration options for the Lazygit theme integration
196+
---Example:
197+
---
198+
---```lua
199+
---lazygit = {
200+
--- theme_path = vim.fs.normalize(vim.fn.stdpath "cache" .. "/lazygit-theme.yml"),
201+
--- config = { os = { editPreset = "nvim-remote" } },
202+
--- theme = {
203+
--- [241] = { fg = "Special" },
204+
--- activeBorderColor = { fg = "MatchParen", bold = true },
205+
--- cherryPickedCommitBgColor = { fg = "Identifier" },
206+
--- cherryPickedCommitFgColor = { fg = "Function" },
207+
--- defaultFgColor = { fg = "Normal" },
208+
--- inactiveBorderColor = { fg = "FloatBorder" },
209+
--- optionsTextColor = { fg = "Function" },
210+
--- searchingActiveBorderColor = { fg = "MatchParen", bold = true },
211+
--- selectedLineBgColor = { bg = "Visual" },
212+
--- unstagedChangesColor = { fg = "DiagnosticError" },
213+
--- },
214+
---}
215+
---```
216+
---@field lazygit AstroUILazygitOpts?
190217

191218
---@type AstroUIOpts
192219
local M = {
@@ -205,6 +232,7 @@ local M = {
205232
sign_handlers = {},
206233
winbar = {},
207234
},
235+
lazygit = false,
208236
}
209237

210238
return M

lua/astroui/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function M.setup(opts)
3737
if colorscheme and not pcall(vim.cmd.colorscheme, colorscheme) then
3838
vim.notify(("Error setting up colorscheme: `%s`"):format(colorscheme), vim.log.levels.ERROR, { title = "AstroUI" })
3939
end
40+
41+
require("astroui.lazygit").setup()
4042
end
4143

4244
--- Get highlight properties for a given highlight name

lua/astroui/lazygit.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--- Adapted from folke's snack.nvim lazygit integration:
2+
--- https://github.com/folke/snacks.nvim/blob/14c787540828946126f2acb5e542dc57956c2711/lua/snacks/lazygit.lua
3+
local M = {}
4+
5+
local astroui = require "astroui"
6+
local config = astroui.config.lazygit
7+
8+
-- Build theme configuration file
9+
function M.update_config()
10+
if not config then return end
11+
---@type table<string, string[]>
12+
local theme = {}
13+
14+
for k, v in pairs(config.theme or {}) do
15+
local color = {}
16+
for _, c in ipairs { "fg", "bg" } do
17+
if v[c] then
18+
local hl_color = astroui.get_hlgroup(v[c] --[[ @as string ]])[c]
19+
if type(hl_color) == "number" then table.insert(color, string.format("#%06x", hl_color)) end
20+
end
21+
end
22+
for _, modifier in ipairs { "bold", "reverse", "underline", "strikethrough" } do
23+
if v[modifier] then table.insert(color, modifier) end
24+
end
25+
if type(k) == "number" then
26+
pcall(io.write, ("\27]4;%d;%s\7"):format(k, color[1]))
27+
else
28+
theme[k] = color
29+
end
30+
end
31+
32+
local lg_config = vim.tbl_deep_extend("force", { gui = { theme = theme } }, config.config or {})
33+
34+
local function yaml_val(val) return type(val) == "string" and not val:find "^\"'`" and ("%q"):format(val) or val end
35+
36+
local function to_yaml(tbl, indent)
37+
indent = indent or 0
38+
local lines = {}
39+
for k, v in pairs(tbl) do
40+
table.insert(lines, string.rep(" ", indent) .. k .. (type(v) == "table" and ":" or ": " .. yaml_val(v)))
41+
if type(v) == "table" then
42+
if (vim.islist or vim.tbl_islist)(v) then
43+
for _, item in ipairs(v) do
44+
table.insert(lines, string.rep(" ", indent + 2) .. "- " .. yaml_val(item))
45+
end
46+
else
47+
vim.list_extend(lines, to_yaml(v, indent + 2))
48+
end
49+
end
50+
end
51+
return lines
52+
end
53+
vim.fn.writefile(to_yaml(lg_config), config.theme_path)
54+
end
55+
56+
function M.setup()
57+
if type(config) ~= "table" then return end
58+
59+
M.update_config()
60+
61+
local lg_config_file = vim.env.LG_CONFIG_FILE --[[ @as string? ]]
62+
if not lg_config_file and vim.fn.executable "lazygit" == 1 then
63+
lg_config_file = require("astrocore").cmd({ "lazygit", "-cd" }, false)
64+
if lg_config_file then lg_config_file = vim.split(lg_config_file, "\n", { plain = true })[1] .. "/config.yml" end
65+
end
66+
lg_config_file = lg_config_file and lg_config_file .. "," or ""
67+
vim.env.LG_CONFIG_FILE = vim.fs.normalize(lg_config_file .. config.theme_path)
68+
69+
vim.api.nvim_create_autocmd("User", {
70+
pattern = "AstroColorScheme",
71+
callback = M.update_config,
72+
desc = "Update lazygit theme configuration when changing colorscheme",
73+
group = vim.api.nvim_create_augroup("astroui_lazygit", { clear = true }),
74+
})
75+
end
76+
77+
return M

0 commit comments

Comments
 (0)