summaryrefslogtreecommitdiff
path: root/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
commitc80a54e42b52ce297f0f2f71af23c562832025c7 (patch)
treedcce8bb977a770f473325d48f6f70b21d9818a40 /ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet
init
Diffstat (limited to 'ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet')
-rw-r--r--ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/grid.lua105
-rw-r--r--ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/init.lua116
2 files changed, 221 insertions, 0 deletions
diff --git a/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/grid.lua b/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/grid.lua
new file mode 100644
index 0000000..6616b1e
--- /dev/null
+++ b/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/grid.lua
@@ -0,0 +1,105 @@
+local api = vim.api
+local ch = require("thesiahxyz.utils.cheatsheet.init")
+local state = ch.state
+
+local ascii = {
+ " ",
+ " ",
+ "█▀▀ █░█ █▀▀ ▄▀█ ▀█▀ █▀ █░█ █▀▀ █▀▀ ▀█▀",
+ "█▄▄ █▀█ ██▄ █▀█ ░█░ ▄█ █▀█ ██▄ ██▄ ░█░",
+ " ",
+ " ",
+}
+
+return function(buf, win, action)
+ action = action or "open"
+
+ local ns = api.nvim_create_namespace("thesiah_cheatsheet")
+
+ if action == "open" then
+ state.mappings_tb = ch.organize_mappings()
+
+ buf = buf or api.nvim_create_buf(false, true)
+ win = win or api.nvim_get_current_win()
+
+ api.nvim_set_current_win(win)
+
+ -- Calculate maximum widths for lhs and rhs
+ local lhs_max_width, rhs_max_width = 0, 0
+ for _, section in pairs(state.mappings_tb) do
+ for _, mapping in ipairs(section) do
+ lhs_max_width = math.max(lhs_max_width, vim.fn.strdisplaywidth(mapping[1]))
+ rhs_max_width = math.max(rhs_max_width, vim.fn.strdisplaywidth(mapping[2]))
+ end
+ end
+
+ local total_width = lhs_max_width + rhs_max_width + 6 -- Add spacing for readability
+ local center_offset = math.floor((total_width - vim.fn.strdisplaywidth(ascii[1])) / 2)
+
+ -- Align ASCII art to the center
+ local ascii_header = vim.tbl_values(ascii)
+ for i, line in ipairs(ascii_header) do
+ ascii_header[i] = string.rep(" ", center_offset) .. line
+ end
+
+ -- Center-align the title
+ local title = "Cheatsheet"
+ local title_padding = math.floor((total_width - vim.fn.strdisplaywidth(title)) / 2)
+ local title_line = string.rep(" ", title_padding) .. title
+
+ -- Prepare buffer lines
+ local lines = {}
+ for _, line in ipairs(ascii_header) do
+ table.insert(lines, line)
+ end
+ table.insert(lines, "") -- Blank line after ASCII art
+ table.insert(lines, title_line)
+ table.insert(lines, "") -- Blank line after title
+
+ -- Add mappings grouped by section
+ for section_name, mappings in pairs(state.mappings_tb) do
+ -- Center-align the section name
+ local section_padding = math.floor((total_width - vim.fn.strdisplaywidth(section_name)) / 2)
+ table.insert(lines, string.rep(" ", section_padding) .. section_name) -- Section header
+
+ -- Add mappings aligned to lhs and rhs
+ for _, mapping in ipairs(mappings) do
+ local lhs = mapping[1]
+ local rhs = mapping[2]
+ local lhs_padding = string.rep(" ", lhs_max_width - vim.fn.strdisplaywidth(lhs))
+ local rhs_padding = string.rep(" ", rhs_max_width - vim.fn.strdisplaywidth(rhs))
+ table.insert(lines, lhs .. lhs_padding .. " " .. rhs_padding .. rhs)
+ end
+ table.insert(lines, "") -- Blank line after each section
+ end
+
+ -- Set lines into the buffer
+ api.nvim_buf_set_lines(buf, 0, -1, false, lines)
+
+ -- Highlight ASCII art and title
+ for i = 1, #ascii_header do
+ api.nvim_buf_add_highlight(buf, ns, "ThesiahAsciiHeader", i - 1, 0, -1)
+ end
+ api.nvim_buf_add_highlight(buf, ns, "Title", #ascii + 1, 0, -1)
+
+ -- Highlight section headers
+ local current_line = #ascii + 3 -- Adjust for blank lines and title
+ for section_name, mappings in pairs(state.mappings_tb) do
+ api.nvim_buf_add_highlight(buf, ns, "ThesiahSection", current_line, 0, -1)
+ current_line = current_line + #mappings + 2 -- Count section header, mappings, and blank line
+ end
+
+ -- Configure the buffer
+ vim.bo[buf].modifiable = false
+ vim.bo[buf].readonly = false
+ vim.bo[buf].buftype = ""
+ vim.bo[buf].buflisted = true
+ vim.bo[buf].filetype = "cheatsheet"
+
+ -- Set up autocmds for the cheatsheet
+ ch.autocmds(buf, win)
+
+ -- Focus on the cheatsheet buffer
+ api.nvim_set_current_buf(buf)
+ end
+end
diff --git a/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/init.lua b/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/init.lua
new file mode 100644
index 0000000..c4cc1f0
--- /dev/null
+++ b/ar/.config/TheSiahxyz/lua/thesiahxyz/utils/cheatsheet/init.lua
@@ -0,0 +1,116 @@
+local M = {}
+local api = vim.api
+local config = require("thesiahxyz.utils.cheatsheet") -- Load cheatsheet options
+
+local function capitalize(str)
+ return (str:gsub("^%l", string.upper))
+end
+
+M.get_mappings = function(mappings, tb_to_add)
+ local excluded_groups = config.cheatsheet.excluded_groups
+
+ for _, v in ipairs(mappings) do
+ local desc = v.desc
+
+ if not desc or (select(2, desc:gsub("%S+", "")) <= 1) or string.find(desc, "\n") then
+ goto continue
+ end
+
+ local heading = desc:match("%S+") -- Get first word
+ heading = (v.mode ~= "n" and heading .. " (" .. v.mode .. ")") or heading
+
+ if
+ vim.tbl_contains(excluded_groups, heading)
+ or vim.tbl_contains(excluded_groups, desc:match("%S+"))
+ or string.find(v.lhs, "<Plug>")
+ then
+ goto continue
+ end
+
+ heading = capitalize(heading)
+
+ if not tb_to_add[heading] then
+ tb_to_add[heading] = {}
+ end
+
+ local keybind = string.sub(v.lhs, 1, 1) == " " and "<leader> +" .. v.lhs or v.lhs
+
+ desc = v.desc:match("%s(.+)") -- Remove first word from description
+ desc = capitalize(desc)
+
+ table.insert(tb_to_add[heading], { desc, keybind })
+
+ ::continue::
+ end
+end
+
+M.organize_mappings = function()
+ local tb_to_add = {}
+ local modes = { "n", "i", "v", "t" }
+
+ for _, mode in ipairs(modes) do
+ local keymaps = api.nvim_get_keymap(mode)
+ M.get_mappings(keymaps, tb_to_add)
+
+ local bufkeymaps = api.nvim_buf_get_keymap(0, mode)
+ M.get_mappings(bufkeymaps, tb_to_add)
+ end
+
+ return tb_to_add
+end
+
+M.rand_hlgroup = function()
+ local hlgroups = {
+ "blue",
+ "red",
+ "green",
+ "yellow",
+ "orange",
+ "baby_pink",
+ "purple",
+ "white",
+ "cyan",
+ "vibrant_green",
+ "teal",
+ }
+
+ return "ThesiahHead" .. hlgroups[math.random(1, #hlgroups)]
+end
+
+M.autocmds = function(buf, win)
+ -- Set buffer options to make it searchable and navigable
+ vim.bo[buf].buflisted = true
+ vim.bo[buf].buftype = "" -- Treat it as a regular buffer
+ vim.bo[buf].swapfile = false
+ vim.bo[buf].modifiable = false -- Prevent accidental edits
+ vim.bo[buf].readonly = false -- Enable navigation and search
+ vim.bo[buf].filetype = "cheatsheet" -- Optional, to customize behavior
+
+ -- Create autocmd group for cheatsheet
+ local group_id = api.nvim_create_augroup("ThesiahCheatsheet", { clear = true })
+
+ -- Clean up when buffer is closed
+ api.nvim_create_autocmd("BufWinLeave", {
+ group = group_id,
+ buffer = buf,
+ callback = function()
+ vim.g.thesiah_cheatsheet_displayed = false
+ api.nvim_del_augroup_by_id(group_id)
+ end,
+ })
+
+ -- Keymaps for cheatsheet buffer
+ vim.keymap.set("n", "q", function()
+ api.nvim_buf_delete(buf, { force = true })
+ end, { buffer = buf })
+
+ vim.keymap.set("n", "<ESC>", function()
+ api.nvim_buf_delete(buf, { force = true })
+ end, { buffer = buf })
+end
+
+M.state = {
+ mappings_tb = {},
+}
+
+return M