Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): allow text keys and preset.header to be functions #420

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
9 changes: 6 additions & 3 deletions lua/snacks/dashboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ math.randomseed(os.time())
---@field header? string
---@field icon? string
---@field title? string
---@field text? string|snacks.dashboard.Text[]
---@field text? snacks.dashboard.TextFunc

---@alias snacks.dashboard.Format.ctx {width?:number}
---@alias snacks.dashboard.Action string|fun(self:snacks.dashboard.Class)
---@alias snacks.dashboard.Gen fun(self:snacks.dashboard.Class):snacks.dashboard.Section?
---@alias snacks.dashboard.Section snacks.dashboard.Item|snacks.dashboard.Gen|snacks.dashboard.Section[]
---@alias snacks.dashboard.TextFunc snacks.dashboard.Text[]|snacks.dashboard.Text|string|fun():string|snacks.dashboard.Text|snacks.dashboard.Text[]

---@class snacks.dashboard.Text
---@field [1] string the text
Expand Down Expand Up @@ -101,6 +102,7 @@ local defaults = {
{ icon = " ", key = "q", desc = "Quit", action = ":qa" },
},
-- Used by the `header` section
---@type string|snacks.dashboard.TextFunc
header = [[
███╗ ██╗███████╗ ██████╗ ██╗ ██╗██╗███╗ ███╗
████╗ ██║██╔════╝██╔═══██╗██║ ██║██║████╗ ████║
Expand Down Expand Up @@ -357,9 +359,10 @@ function D:align(item, width, align)
end
end

---@param texts snacks.dashboard.Text[]|snacks.dashboard.Text|string
---@param texts snacks.dashboard.TextFunc
function D:texts(texts)
texts = type(texts) == "string" and { { texts } } or texts
texts = type(texts) == "function" and { texts() } or texts
texts = type(texts[1]) == "string" and { texts } or texts
return texts --[[ @as snacks.dashboard.Text[] ]]
end
Expand Down Expand Up @@ -921,7 +924,7 @@ end
---@return snacks.dashboard.Gen
function M.sections.header()
return function(self)
return { header = self.opts.preset.header, padding = 2 }
return { header = type(self.opts.preset.header) == "function" and self.opts.preset.header() or self.opts.preset.header, padding = 2 }
end
end

Expand Down
Loading