diff --git a/README.md b/README.md index 310b168..5067069 100644 --- a/README.md +++ b/README.md @@ -64,21 +64,37 @@ Any hints that could appear in the same place as others should have unique prior ## ❔Usage +`precognition` can be controlled with the `Precognition` user command, as well as programmatically via the Lua API. + ### Toggling The hints can be toggled on and off with +```vim +:Precognition toggle ``` -:lua require("precognition").toggle() + +or + +```lua +require("precognition").toggle() ``` +The subcommands and functions `show` and `hide` are also available. + ### Peeking The hints can be peeked, this means that the hint will be show until the next cursor movement. +```vim +:Precognition peek ``` -:lua require("precognition").peek() + +or + +```lua +require("precognition").peek() ``` ## 💻 Supported Versions diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index d9cb5f8..b0b9d49 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -314,6 +314,39 @@ local function on_buf_leave(ev) dirty = true end +local function create_command() + local subcommands = { + peek = M.peek, + toggle = M.toggle, + show = M.show, + hide = M.hide, + } + + local function execute(args) + local cmd_args = args.fargs + local subcmd = cmd_args[1] + if not subcmd then + return + end + if subcommands[subcmd] then + subcommands[subcmd]() + else + vim.notify("Invalid subcommand: " .. subcmd, vim.log.levels.ERROR, { + title = "Precognition", + }) + end + end + + local function complete(_line, _len, _arg_lead) + return vim.tbl_keys(subcommands) + end + + vim.api.nvim_create_user_command("Precognition", execute, { + nargs = "*", + complete = complete, + }) +end + --- Show the hints until the next keypress or CursorMoved event function M.peek() display_marks() @@ -395,6 +428,8 @@ function M.setup(opts) local hl_name = "PrecognitionHighlight" vim.api.nvim_set_hl(0, hl_name, config.highlightColor) + create_command() + if config.startVisible then M.show() end