|
| 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