diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 885b15e..bee6723 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -1,13 +1,20 @@ -local M = {} - +---@class Cheaty.Config +---@field keymap? string +---@field width? number +---@field height? number +---@field cheatsheet? string[] local defaults = { width = 0.6, height = 0.6, cheatsheet = { "# This is a sample cheatsheet!", "Tailor it to your liking in the config!" } } -M.config = {} +---@class Cheaty +local M = {} + +M.config = {} ---@type Cheaty.Config +---@param opts? Cheaty.Config function M.setup(opts) M.config = vim.tbl_deep_extend("force", defaults, opts or {}) @@ -19,3 +26,4 @@ function M.setup(opts) end return M +-- vim: set ts=4 sts=4 sw=0 noet ai si sta: diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 59f6e83..d193184 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -1,58 +1,72 @@ +---@class Cheaty.Window +---@field config? Cheaty.Config local M = {} -local win_id = nil -local buf_id = nil +M.win_id = nil ---@type integer +M.buf_id = nil ---@type integer -local function create_window(cfg) - buf_id = vim.api.nvim_create_buf(false, true) +function M.create_window() + if not M.config then + return + end + + M.buf_id = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, cfg.cheatsheet) + vim.api.nvim_buf_set_lines(M.buf_id, 0, -1, false, M.config.cheatsheet) -- Buffer options - local buffer = vim.bo[buf_id] + local buffer = vim.bo[M.buf_id] - buffer.buftype = "nofile" - buffer.bufhidden = "wipe" + buffer.buftype = "nofile" + buffer.bufhidden = "wipe" buffer.modifiable = false - buffer.swapfile = false - buffer.filetype = "markdown" + buffer.swapfile = false + buffer.filetype = "markdown" - vim.api.nvim_buf_call(buf_id, function() - vim.cmd("doautocmd FileType markdown") + vim.api.nvim_buf_call(M.buf_id, function() + -- vim.cmd("doautocmd FileType markdown") + vim.api.nvim_exec_autocmds("FileType", { pattern = "markdown" }) end) - local width = math.floor(vim.o.columns * cfg.width) - local height = math.floor(vim.o.lines * cfg.height) + local width = math.floor(vim.o.columns * M.config.width) + local height = math.floor(vim.o.lines * M.config.height) local row = math.floor((vim.o.lines - height) / 2) local col = math.floor((vim.o.columns - width) / 2) - - win_id = vim.api.nvim_open_win(buf_id, true, { + M.win_id = vim.api.nvim_open_win(M.buf_id, true, { relative = "editor", - row = row, - col = col, - width = width, - height = height, - style = "minimal", - border = "rounded" + row = row, + col = col, + width = width, + height = height, + style = "minimal", + border = "rounded", + title = "Cheatsheet", + title_pos = "center", + footer_pos = "center", + footer = ": Close Window", }) + + vim.keymap.set("n", "q", M.close, { buffer = M.buf_id, noremap = true, silent = true }) end function M.close() - if win_id and vim.api.nvim_win_is_valid(win_id) then - vim.api.nvim_win_close(win_id, true) + if M.win_id and vim.api.nvim_win_is_valid(M.win_id) then + vim.api.nvim_win_close(M.win_id, true) end - win_id = nil - buf_id = nil + M.win_id = nil + M.buf_id = nil end -function M.toggle(cfg) - if win_id and vim.api.nvim_win_is_valid(win_id) then +function M.toggle() + if M.win_id and vim.api.nvim_win_is_valid(M.win_id) then M.close() - else - create_window(cfg) + return end + + M.create_window() end return M +-- vim: set ts=4 sts=4 sw=0 noet ai si sta: diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..aa28065 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,12 @@ +call_parentheses = "Always" +collapse_simple_statement = "Never" +column_width = 100 +indent_type = "Tabs" +indent_width = 4 +line_endings = "Unix" +quote_style = "AutoPreferDouble" +space_after_function_names = "Never" +syntax = 'LuaJIT' + +[sort_requires] +enabled = false