diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 93918b3..fa42fac 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -1,3 +1,5 @@ +local Util = require("cheaty.util") + ---@class cheaty local M = {} @@ -6,7 +8,7 @@ local M = {} ---@field width? number ---@field height? number ---@field save_file? string ----@field cheatsheet? string[] +---@field cheatsheet? string[]|string local defaults = { width = 0.6, height = 0.6, @@ -19,11 +21,46 @@ M.config = {} ---@type cheatyOpts ---@param opts? cheatyOpts function M.setup(opts) + Util.validate({ opts = { opts, { "table", "nil" }, true } }) + opts = opts or {} + + Util.validate({ + ["opts.width"] = { opts.width, { "number", "nil" }, true }, + ["opts.height"] = { opts.height, { "number", "nil" }, true }, + ["opts.save_file"] = { opts.save_file, { "string", "nil" }, true }, + ["opts.cheatsheet"] = { opts.cheatsheet, { "table", "string", "nil" }, true }, + }) + M.config = vim.tbl_deep_extend("force", defaults, opts or {}) - vim.api.nvim_create_user_command("Cheaty", function() - require("cheaty.window").toggle(M.config) - end, { desc = "Open Cheatsheet" }) + if M.config.cheatsheet and type(M.config.cheatsheet) == "string" then + M.config.cheatsheet = vim.split(M.config.cheatsheet, "\n", { trimempty = false }) + end + + vim.api.nvim_create_user_command("Cheaty", function(ctx) + if vim.tbl_isempty(ctx.fargs) then + require("cheaty.window").toggle(M.config) + end + + if ctx.fargs[1] == "reset" then + require("cheaty.window").reset(M.config) + end + end, { + desc = "Open Cheatsheet", + nargs = "?", + complete = function(_, line) + local args = vim.split(line, "%s+", { trimempty = false }) + if args[1]:sub(-1) == "!" then + return {} + end + + if #args == 2 then + return { "reset" } + end + + return {} + end, + }) end return M diff --git a/lua/cheaty/util.lua b/lua/cheaty/util.lua new file mode 100644 index 0000000..ce42b79 --- /dev/null +++ b/lua/cheaty/util.lua @@ -0,0 +1,33 @@ +---Non-legacy validation spec (>=v0.11) +---@class ValidateSpec +---@field [1] any +---@field [2] vim.validate.Validator +---@field [3]? boolean +---@field [4]? string + +---@class cheaty.Util +local M = {} + +---Dynamic `vim.validate()` wrapper. Covers both legacy and newer implementations +---@param T table +function M.validate(T) + local max = vim.fn.has("nvim-0.11") == 1 and 3 or 4 + for name, spec in pairs(T) do + while #spec > max do + table.remove(spec, #spec) + end + T[name] = spec + end + + if vim.fn.has("nvim-0.11") == 1 then + for name, spec in pairs(T) do + table.insert(spec, 1, name) + vim.validate(unpack(spec)) + end + return + end + + vim.validate(T) +end + +return M diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 5fddf41..7513e96 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -8,12 +8,15 @@ local fd, stat = nil, nil ---@type integer|nil, uv.fs_stat.result|nil ---@param cfg cheatyOpts ---@param flags? uv.fs_open.flags -local function open_file(cfg, flags) +---@param reset? boolean +local function open_file(cfg, flags, reset) + reset = reset ~= nil and reset or false + if not cfg.save_file or cfg.save_file == "" then error("No valid path!", vim.log.levels.ERROR) end - if vim.fn.filereadable(cfg.save_file) ~= 1 then + if vim.fn.filereadable(cfg.save_file) ~= 1 or reset then vim.fn.writefile(cfg.cheatsheet or {}, cfg.save_file) end @@ -100,6 +103,15 @@ function M.close() buf_id = nil end +---@param cfg cheatyOpts +function M.reset(cfg) + open_file(cfg, "r", true) + + if fd then + uv.fs_close(fd) + end +end + ---@param cfg cheatyOpts function M.toggle(cfg) if win_id and buf_id then