Skip to content

Commit

Permalink
feat: :Noice now has subcommands, full commands and a lua api require…
Browse files Browse the repository at this point in the history
…("noice").cmd("last")
  • Loading branch information
folke committed Oct 27, 2022
1 parent 814c059 commit 88767a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ require("telescope").load_extension("noice")
- `:Noice stats` shows debugging stats
- `:Noice telescope` opens message history in Telescope

Alternatively, all commands also exist as a full name like `:NoiceLast`, `:NoiceDisable`.

You can also use `Lua` equivalents.

```lua
vim.keymap.set("n", "<leader>nl", function()
require("noice").cmd("last")
end)

vim.keymap.set("n", "<leader>nh", function()
require("noice").cmd("history")
end)
```

> You can add custom commands with `config.commands`
## 🌈 Highlight Groups
Expand Down
30 changes: 22 additions & 8 deletions lua/noice/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ local Message = require("noice.message")

local M = {}

---@type table<string, fun()>
M.commands = {}

---@param command NoiceCommand
function M.command(command)
return function()
Expand All @@ -26,8 +29,16 @@ function M.command(command)
end
end

function M.cmd(cmd)
if M.commands[cmd] then
M.commands[cmd]()
else
M.commands.history()
end
end

function M.setup()
local commands = {
M.commands = {
debug = function()
Config.options.debug = not Config.options.debug
end,
Expand Down Expand Up @@ -59,16 +70,12 @@ function M.setup()
}

for name, command in pairs(Config.options.commands) do
commands[name] = M.command(command)
M.commands[name] = M.command(command)
end

vim.api.nvim_create_user_command("Noice", function(args)
local cmd = vim.trim(args.args or "")
if commands[cmd] then
commands[cmd]()
else
commands.history()
end
M.cmd(cmd)
end, {
nargs = "?",
desc = "Noice",
Expand All @@ -79,9 +86,16 @@ function M.setup()
local prefix = line:match("^%s*Noice (%w*)")
return vim.tbl_filter(function(key)
return key:find(prefix) == 1
end, vim.tbl_keys(commands))
end, vim.tbl_keys(M.commands))
end,
})

for name in pairs(M.commands) do
local cmd = "Noice" .. name:sub(1, 1):upper() .. name:sub(2)
vim.api.nvim_create_user_command(cmd, function()
M.cmd(name)
end, { desc = "Noice " .. name })
end
end

return M
4 changes: 4 additions & 0 deletions lua/noice/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function M.disable()
require("noice.util.hacks").disable()
end

function M.cmd(name)
require("noice.commands").cmd(name)
end

function M.enable()
Config._running = true
if Config.options.notify.enabled then
Expand Down

0 comments on commit 88767a6

Please sign in to comment.