[picker]: Cycle between your preferred layouts! #458
pkazmier
started this conversation in
Show and tell
Replies: 1 comment 4 replies
-
Cycle preview sizes: opts = {
picker = {
actions = {
cycle_preview = function(picker)
local layout_config = vim.deepcopy(picker.resolved_layout)
if layout_config.preview == 'main' or not picker.preview.win:valid() then
return
end
local function find_preview(root) ---@param root snacks.layout.Box|snacks.layout.Win
if root.win == 'preview' then
return root
end
if #root then
for _, w in ipairs(root) do
local preview = find_preview(w)
if preview then
return preview
end
end
end
return nil
end
local preview = find_preview(layout_config.layout)
if not preview then
return
end
local eval = function(s)
return type(s) == 'function' and s(preview.win) or s
end
--- @type number?, number?
local width, height = eval(preview.width), eval(preview.height)
if not width and not height then
return
end
local cycle_sizes = { 0.1, 0.9 }
local size_prop, size
if height then
size_prop, size = 'height', height
else
size_prop, size = 'width', width
end
picker.init_size = picker.init_size or size ---@diagnostic disable-line: inject-field
table.insert(cycle_sizes, picker.init_size)
table.sort(cycle_sizes)
for i, s in ipairs(cycle_sizes) do
if size == s then
local smaller = cycle_sizes[i - 1] or cycle_sizes[#cycle_sizes]
preview[size_prop] = smaller
break
end
end
for i, h in ipairs(layout_config.hidden) do
if h == 'preview' then
table.remove(layout_config.hidden, i)
end
end
picker:set_layout(layout_config)
end,
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
With the ability to dynamically change picker layouts on the fly, I wrote this to allow me to quickly cycle between my preferred layouts. While picker allows you to maximize the layout and toggle on/off the preview, sometimes I still need a wider preview. So with this I simply press
<a-c>
to cycle between layouts. My preferred layout isivy
, but I wanted slight variations as well, so I've defined the following 4 preferred layouts:Here is a video showing the cycling of layouts with
<a-c>
. When I get to my layout with the wider preview, I maximized with<a-m>
to show how I combine these.Screen.Recording.2025-01-14.at.5.13.09.PM.mov
Here is the little utility I wrote in my
util/snacks_picker.lua
:And then I set the keybinding as well as set my preferred layout in my
snacks.picker
spec:Beta Was this translation helpful? Give feedback.
All reactions