dotfiles/config/nvim/lua/fschauen/util/lualine.lua

72 lines
2 KiB
Lua

local M = {}
---Make a lualine component that is colored only when focused.
---@param base table|string Either a lualine component or a component name.
---@return table component
M.dynamic_color = function(base)
local component = {}
if type(base) == "string" then
component = require("lualine.components." .. base):extend()
elseif type(base) == "table" then
component = base:extend()
else
return component
end
function component:init(options)
component.super.init(
self,
vim.tbl_deep_extend("keep", options or {}, {
colored = true,
dynamic_color = true,
})
)
self.saved_highlight = self.options.color_highlight
end
function component:update_status(is_focused)
if self.options.dynamic_color then self.options.colored = is_focused end
return component.super.update_status(self, is_focused)
end
function component:draw(default_highlight, is_focused)
self.options.color_highlight = self.options.colored and self.saved_highlight or nil
return component.super.draw(self, default_highlight, is_focused)
end
return component
end
---@class LualineIndicatorSpec
---@field icon string The icon to show when the indicator is active.
---@field cond function A function that returns true when the indicator should
--- be visible, false otherwise.
---Make a lualine indicator that shows an icon when a condition is true.
---@param spec LualineIndicatorSpec
---@return table component
M.indicator = function(spec)
vim.validate("spec", spec, "table")
vim.validate("spec.icon", spec.icon, "string")
vim.validate("spec.cond", spec.cond, "function")
local component = require("lualine.component"):extend()
function component:init(options)
component.super.init(
self,
vim.tbl_deep_extend(
"keep",
options or {},
{ cond = spec.cond or function() return false end }
)
)
end
function component:update_status(--[[is_focused]]_) return spec.icon or "?" end
return component
end
return M