refactor(picker): move resolving of preview / format to config

This commit is contained in:
Folke Lemaitre 2025-01-21 22:23:03 +01:00
parent efb6d1f8b8
commit fcceedf473
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 17 additions and 9 deletions

View file

@ -77,6 +77,21 @@ function M.get(opts)
-- Merge the configs
return Snacks.config.merge(unpack(todo))
---@param opts snacks.picker.Config
function M.format(opts)
local ret = type(opts.format) == "string" and Snacks.picker.format[opts.format]
or opts.format
or Snacks.picker.format.file
---@cast ret snacks.picker.format
return ret
end
---@param opts snacks.picker.Config
function M.preview(opts)
local preview = opts.preview or Snacks.picker.preview.file
preview = type(preview) == "string" and Snacks.picker.preview[preview] or preview
---@cast preview snacks.picker.preview
return preview
end
--- Resolve the layout configuration

View file

@ -128,11 +128,7 @@ function M.new(opts)
return self.opts.items or {}
end)
local format = type(self.opts.format) == "string" and Snacks.picker.format[self.opts.format]
or self.opts.format
or Snacks.picker.format.file
---@cast format snacks.picker.format
self.format = format
self.format = Snacks.picker.config.format(self.opts)
M._pickers[self] = true
M._active[self] = true

View file

@ -74,10 +74,7 @@ function M.new(opts, main)
self:clear(self.win.buf)
end, { win = true })
local preview = opts.preview or Snacks.picker.preview.file
preview = type(preview) == "string" and Snacks.picker.preview[preview] or preview
---@cast preview snacks.picker.preview
self.preview = preview
self.preview = Snacks.picker.config.preview(opts)
return self
end