From 84d3de643c495e76173a8484ae839f5ef5e83f24 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Mon, 23 Feb 2026 12:35:53 -0600 Subject: [PATCH 1/2] feat: add `:Cheaty reset` to reset data to default value, add validator Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 45 +++++++++++++++++++++++++++++++++++++++---- lua/cheaty/util.lua | 33 +++++++++++++++++++++++++++++++ lua/cheaty/window.lua | 16 +++++++++++++-- 3 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 lua/cheaty/util.lua diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 93918b3..23d444b 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..863b68b --- /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..34830b3 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 From d090611a43e4bb093077e3e9cb41d0efb9d7c0c8 Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Mon, 23 Feb 2026 13:44:15 -0600 Subject: [PATCH 2/2] chore: format with StyLua Signed-off-by: Guennadi Maximov C --- lua/cheaty/init.lua | 2 +- lua/cheaty/util.lua | 30 +++++++++++++++--------------- lua/cheaty/window.lua | 10 +++++----- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lua/cheaty/init.lua b/lua/cheaty/init.lua index 23d444b..fa42fac 100644 --- a/lua/cheaty/init.lua +++ b/lua/cheaty/init.lua @@ -55,7 +55,7 @@ function M.setup(opts) end if #args == 2 then - return { "reset" } + return { "reset" } end return {} diff --git a/lua/cheaty/util.lua b/lua/cheaty/util.lua index 863b68b..ce42b79 100644 --- a/lua/cheaty/util.lua +++ b/lua/cheaty/util.lua @@ -11,23 +11,23 @@ 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 + 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 + 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) + vim.validate(T) end return M diff --git a/lua/cheaty/window.lua b/lua/cheaty/window.lua index 34830b3..7513e96 100644 --- a/lua/cheaty/window.lua +++ b/lua/cheaty/window.lua @@ -10,7 +10,7 @@ local fd, stat = nil, nil ---@type integer|nil, uv.fs_stat.result|nil ---@param flags? uv.fs_open.flags ---@param reset? boolean local function open_file(cfg, flags, reset) - reset = reset ~= nil and reset or false + 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) @@ -105,11 +105,11 @@ end ---@param cfg cheatyOpts function M.reset(cfg) - open_file(cfg, "r", true) + open_file(cfg, "r", true) - if fd then - uv.fs_close(fd) - end + if fd then + uv.fs_close(fd) + end end ---@param cfg cheatyOpts