Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions lua/cheaty/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Util = require("cheaty.util")

---@class cheaty
local M = {}

Expand All @@ -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,
Expand All @@ -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
33 changes: 33 additions & 0 deletions lua/cheaty/util.lua
Original file line number Diff line number Diff line change
@@ -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<string, vim.validate.Spec|ValidateSpec>
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
16 changes: 14 additions & 2 deletions lua/cheaty/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down