-
-
Save jedrzejboczar/618b531d1e0de6a956c66132287689aa to your computer and use it in GitHub Desktop.
Minimal example of using possession.nvim with alpha.nvim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- HELPER FUNCTIONS -- | |
-- https://github.com/goolord/alpha-nvim/blob/8a1477d8b99a931530f3cfb70f6805b759bebbf7/lua/alpha/themes/startify.lua#L28 | |
local leader = 'SPC' | |
local function create_button(sc, txt, keybind, keybind_opts) | |
local sc_ = sc:gsub("%s", ""):gsub(leader, "<leader>") | |
local opts = { | |
position = "left", | |
shortcut = "[" .. sc .. "] ", | |
cursor = 1, | |
-- width = 50, | |
align_shortcut = "left", | |
hl_shortcut = { { "Operator", 0, 1 }, { "Number", 1, #sc + 1 }, { "Operator", #sc + 1, #sc + 2 } }, | |
shrink_margin = false, | |
} | |
if keybind then | |
keybind_opts = vim.F.if_nil(keybind_opts, { noremap = true, silent = true, nowait = true }) | |
opts.keymap = { "n", sc_, keybind, { noremap = false, silent = true, nowait = true } } | |
end | |
local function on_press() | |
-- local key = vim.api.nvim_replace_termcodes(keybind .. "<Ignore>", true, false, true) | |
local key = vim.api.nvim_replace_termcodes(sc .. "<Ignore>", true, false, true) | |
vim.api.nvim_feedkeys(key, "t", false) | |
end | |
return { | |
type = "button", | |
val = txt, | |
on_press = on_press, | |
opts = opts, | |
} | |
end | |
local function pad_right(str, width) | |
return str .. string.rep(' ', math.max(0, width - vim.fn.strdisplaywidth(str))) | |
end | |
-- Recursively traverse layout and modify all elements to be properly | |
-- centered to given width. This is a hacky way of achieving centering, see: | |
-- https://github.com/goolord/alpha-nvim/issues/18#issuecomment-991822664 | |
local center_layout | |
center_layout = function(layout, width) | |
local pad = function(str) | |
return pad_right(str, width) | |
end | |
local wrap_fn = function(fn) | |
return function() | |
local val = fn() | |
center_layout(val, width) | |
return val | |
end | |
end | |
for _, el in ipairs(layout) do | |
if el.type == 'button' then | |
el.opts = el.opts or {} | |
el.opts.position = 'center' | |
el.opts.width = width | |
elseif el.type == 'text' then | |
el.opts = el.opts or {} | |
el.opts.position = 'center' | |
if type(el.val) == 'table' then | |
el.val = vim.tbl_map(pad, el.val) | |
else | |
el.val = pad(el.val) | |
end | |
elseif el.type == 'group' then | |
if type(el.val) == 'function' then | |
el.val = wrap_fn(el.val) | |
else | |
center_layout(el.val, width) | |
end | |
end | |
end | |
end | |
-- Smart top padding | |
local top_padding = function(height_estimate) | |
local cached | |
return function() | |
-- Use cached value if redrawing from another window | |
local filetype = vim.api.nvim_buf_get_option(0, 'filetype') | |
if cached and filetype ~= 'alpha' then | |
return cached | |
end | |
local height = vim.api.nvim_win_get_height(0) | |
local calculated = (height - height_estimate) / 2 | |
cached = math.ceil(math.max(1, calculated)) | |
return cached | |
end | |
end | |
-- LAYOUT DEFINITION -- | |
local query = require('possession.query') | |
local workspaces = { | |
{ | |
'Workspace A', -- title | |
'a', -- shortcuts prefix | |
{ | |
'/root/directory/a', | |
'/other/root/directory/', | |
}, | |
}, | |
{ | |
'Workspace B', | |
'b', | |
'/root/directory/b', | |
} | |
} | |
local get_layout = function() | |
return query.alpha_workspace_layout(workspaces, create_button, { | |
others_name = 'Sessions Without Workspace', | |
}) | |
end | |
-- use with the rest of sections for alpha.nvim, with throttling to avoid reading files on each redraw | |
local utils = require('possession.utils') | |
local layout = { | |
{ type = 'padding', val = top_padding(50) }, | |
{ -- top buttons | |
type = 'group', | |
val = { | |
create_button('e', 'New file', '<cmd>enew<cr>'), | |
}, | |
}, | |
{ type = 'padding', val = 1 }, | |
{ -- sessions | |
type = 'group', | |
val = utils.throttle(get_layout, 5000), | |
}, | |
{ type = 'padding', val = 1 }, | |
{ -- bottom buttons | |
type = 'group', | |
val = { | |
create_button('q', 'Quit', '<cmd>quit<cr>') | |
}, | |
}, | |
{ type = 'group', val = {} }, -- footer | |
} | |
center_layout(layout, 86) | |
require('alpha').setup { | |
layout = layout, | |
opts = {}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment