repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/lua/std/cheatsheet.lua
Rawlocal buffer_options = {
bufhidden = 'hide',
modifiable = false,
filetype = 'markdown',
}
local window_border = 'shadow'
local window_options = {
winblend = 42,
}
local calculate_dimensions = function()
local height = math.ceil(vim.o.lines * 0.6)
local width = math.ceil(vim.o.columns * 0.7)
return {
height = height,
width = width,
row = math.ceil((vim.o.lines - height) / 2),
col = math.ceil((vim.o.columns - width) / 2),
}
end
local categories = {
lsp = 'LSP',
fuzzy = 'FZF',
files = 'FILE',
terminal = 'TERM',
editing = 'EDIT',
search = 'FIND',
session = 'SESS',
diff = 'DIFF',
operator = 'OP',
fun = 'FUN',
}
local category_patterns = {
'^lsp: ', 'lsp',
'^fuzzy: ', 'fuzzy',
'^files: ', 'files',
'^terminal: ', 'terminal',
'^editing: ', 'editing',
'^search: ', 'search',
'^session: ', 'session',
'^diff: ', 'diff',
'^operator: ', 'operator',
'^fun: ', 'fun',
}
local get_category_icon = function(category)
return categories[category] or category:upper()
end
local parse_description = function(desc)
if not desc or desc == '' then
return 'general', '*no description*'
end
for i = 1, #category_patterns, 2 do
local pattern, category = category_patterns[i], category_patterns[i + 1]
if desc:match(pattern) then
return category, desc:gsub(pattern, '')
end
end
return 'general', desc
end
local format_keymaps = function(keymaps)
if not keymaps or #keymaps == 0 then
return { '*No keymaps configured*' }
end
local lines = {}
local current_category = nil
for _, km in ipairs(keymaps) do
local category = km.category or 'general'
if category ~= current_category then
if current_category ~= nil then
table.insert(lines, '')
end
table.insert(lines, '### ' .. get_category_icon(category) .. ' ' .. category:upper())
table.insert(lines, '')
current_category = category
end
local keys = km.keys
if type(keys) == 'table' then
keys = table.concat(keys, ', ')
end
local desc = km.description or '*no description*'
table.insert(lines, '`' .. keys .. '` ' .. desc)
end
return lines
end
local collect_keymaps = function()
local keymaps = {}
local modes = { 'n', 'v', 'i', 'x', 's', 'o', 'c', 't' }
local buffer = vim.api.nvim_get_current_buf()
for _, mode in ipairs(modes) do
local mode_keymaps = vim.api.nvim_buf_get_keymap(buffer, mode)
for _, km in ipairs(mode_keymaps) do
local category, description = parse_description(km.desc)
table.insert(keymaps, {
keys = km.lhs,
description = description,
category = category,
mode = mode,
})
end
end
table.sort(keymaps, function(a, b)
if a.category ~= b.category then
return a.category < b.category
end
return a.keys < b.keys
end)
return keymaps
end
local is_cheatsheet_open = function()
for _, win in ipairs(vim.api.nvim_list_wins()) do
local buf = vim.api.nvim_win_get_buf(win)
if vim.bo[buf].filetype == 'markdown' then
local lines = vim.api.nvim_buf_get_lines(buf, 0, 2, false)
if lines[1] and lines[1]:match('^# Cheat Sheet') then
return win
end
end
end
return nil
end
local close = function(win)
if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, false)
end
end
local open = function()
local existing_win = is_cheatsheet_open()
if existing_win then
close(existing_win)
return
end
local buffer = vim.api.nvim_create_buf(false, true)
local dimensions = calculate_dimensions()
vim.api.nvim_buf_set_lines(buffer, 0, -1, false, {
'# Cheat Sheet',
'',
'> Quick reference for keybindings',
'',
'*Press `<esc>` or `q` to close*',
'',
})
local keymaps = collect_keymaps()
local formatted = format_keymaps(keymaps)
if #formatted > 0 then
vim.api.nvim_buf_set_lines(buffer, #{}, -1, false, formatted)
end
for name, value in pairs(buffer_options) do
vim.api.nvim_set_option_value(name, value, {
scope = 'local',
buf = buffer,
})
end
local window = vim.api.nvim_open_win(buffer, true, {
border = window_border,
relative = 'editor',
style = 'minimal',
height = dimensions.height,
width = dimensions.width,
row = dimensions.row,
col = dimensions.col,
})
for name, value in pairs(window_options) do
vim.api.nvim_set_option_value(name, value, {
scope = 'local',
win = window,
})
end
vim.api.nvim_buf_set_keymap(buffer, 'n', '<esc>', '', {
noremap = true,
silent = true,
callback = function()
close(window)
end,
})
vim.api.nvim_buf_set_keymap(buffer, 'n', 'q', '', {
noremap = true,
silent = true,
callback = function()
close(window)
end,
})
vim.api.nvim_buf_set_option(buffer, 'modifiable', false)
end
local toggle = function()
local existing_win = is_cheatsheet_open()
if existing_win then
close(existing_win)
else
open()
end
end
return {
open = open,
toggle = toggle,
}