feat(lazygit): allow overriding extra lazygit config options

This commit is contained in:
Folke Lemaitre 2024-11-09 17:08:07 +01:00
parent 8a1d11ad08
commit d2f4f1937e
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 48 additions and 23 deletions

View file

@ -28,6 +28,13 @@ colorscheme and integrate edit with the current neovim instance.
-- automatically configure lazygit to use the current colorscheme
-- and integrate edit with the current neovim instance
configure = true,
-- extra configuration for lazygit that will be merged with the default
-- snacks does NOT have a full yaml parser, so if you need `"test"` to appear with the quotes
-- you need to double quote it: `"\"test\""`
config = {
os = { editPreset = "nvim-remote" },
gui = { nerdFontsVersion = "3" },
},
theme_path = vim.fs.normalize(vim.fn.stdpath("cache") .. "/lazygit-theme.yml"),
-- Theme for lazygit
theme = {

View file

@ -81,12 +81,14 @@ TERMINAL *snacks-terminal-styles-terminal*
wo = {},
keys = {
gf = function(self)
local f = vim.fn.findfile(vim.fn.expand("<cfile>"))
local f = vim.fn.findfile(vim.fn.expand("<cfile>"), "**")
if f == "" then
Snacks.notify.warn("No file under cursor")
else
self:close()
vim.cmd("e " .. f)
self:hide()
vim.schedule(function()
vim.cmd("e " .. f)
end)
end
end,
term_normal = {

View file

@ -17,6 +17,13 @@ and integrate edit with the current neovim instance.
-- automatically configure lazygit to use the current colorscheme
-- and integrate edit with the current neovim instance
configure = true,
-- extra configuration for lazygit that will be merged with the default
-- snacks does NOT have a full yaml parser, so if you need `"test"` to appear with the quotes
-- you need to double quote it: `"\"test\""`
config = {
os = { editPreset = "nvim-remote" },
gui = { nerdFontsVersion = "3" },
},
theme_path = vim.fs.normalize(vim.fn.stdpath("cache") .. "/lazygit-theme.yml"),
-- Theme for lazygit
theme = {

View file

@ -28,6 +28,13 @@ local defaults = {
-- automatically configure lazygit to use the current colorscheme
-- and integrate edit with the current neovim instance
configure = true,
-- extra configuration for lazygit that will be merged with the default
-- snacks does NOT have a full yaml parser, so if you need `"test"` to appear with the quotes
-- you need to double quote it: `"\"test\""`
config = {
os = { editPreset = "nvim-remote" },
gui = { nerdFontsVersion = "3" },
},
theme_path = vim.fs.normalize(vim.fn.stdpath("cache") .. "/lazygit-theme.yml"),
-- Theme for lazygit
-- stylua: ignore
@ -70,10 +77,6 @@ local function env(opts)
if vim.v.shell_error == 0 and #lines > 1 then
config_dir = vim.split(lines[1], "\n", { plain = true })[1]
vim.env.LG_CONFIG_FILE = vim.fs.normalize(config_dir .. "/config.yml" .. "," .. opts.theme_path)
local custom_config = vim.fs.normalize(config_dir .. "/custom.yml")
if uv.fs_stat(custom_config) and uv.fs_stat(custom_config).type == "file" then
vim.env.LG_CONFIG_FILE = vim.env.LG_CONFIG_FILE .. "," .. custom_config
end
else
local msg = {
"Failed to get **lazygit** config directory.",
@ -131,24 +134,30 @@ local function update_config(opts)
end
end
local config = [[
os:
editPreset: "nvim-remote"
gui:
nerdFontsVersion: 3
theme:
]]
local config = vim.tbl_deep_extend("force", { gui = { theme = theme } }, opts.config or {})
---@type string[]
local lines = {}
for k, v in pairs(theme) do
lines[#lines + 1] = (" %s:"):format(k)
for _, c in ipairs(v) do
lines[#lines + 1] = (" - %q"):format(c)
end
local function yaml_val(val)
return type(val) == "string" and not val:find("^\"'`") and ("%q"):format(val) or val
end
config = config .. table.concat(lines, "\n")
vim.fn.writefile(vim.split(config, "\n", { plain = true }), opts.theme_path)
local function to_yaml(tbl, indent)
indent = indent or 0
local lines = {}
for k, v in pairs(tbl) do
table.insert(lines, string.rep(" ", indent) .. k .. (type(v) == "table" and ":" or ": " .. yaml_val(v)))
if type(v) == "table" then
if (vim.islist or vim.tbl_islist)(v) then
for _, item in ipairs(v) do
table.insert(lines, string.rep(" ", indent + 2) .. "- " .. yaml_val(item))
end
else
vim.list_extend(lines, to_yaml(v, indent + 2))
end
end
end
return lines
end
vim.fn.writefile(to_yaml(config), opts.theme_path)
dirty = false
end