Skip to content

Commit a8b3117

Browse files
committed
feat!: command redirection! Redirect messages generated by a command or function to a different view
1 parent b329e0b commit a8b3117

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ require("noice").setup({
7878
long_message_to_split = true, -- long messages will be sent to a split
7979
inc_rename = false, -- enables an input dialog for inc-rename.nvim
8080
lsp_doc_border = false, -- add a border to hover docs and signature help
81-
cmdline_output_to_split = false, -- send the output of a command you executed in the cmdline to a split
8281
},
8382
})
8483
```
@@ -147,6 +146,13 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
147146
-- Icons for completion item kinds (see defaults at noice.config.icons.kinds)
148147
kind_icons = {}, -- set to `false` to disable icons
149148
},
149+
-- default options for require('noice').redirect
150+
-- see the section on Command Redirection
151+
---@type NoiceRouteConfig
152+
redirect = {
153+
view = "popup",
154+
filter = { event = "msg_show" },
155+
},
150156
-- You can add any custom commands below that will be available with `:Noice command`
151157
---@type table<string, NoiceCommand>
152158
commands = {
@@ -286,7 +292,6 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
286292
long_message_to_split = false, -- long messages will be sent to a split
287293
inc_rename = false, -- enables an input dialog for inc-rename.nvim
288294
lsp_doc_border = false, -- add a border to hover docs and signature help
289-
cmdline_output_to_split = false, -- send the output of a command you executed in the cmdline to a split
290295
},
291296
throttle = 1000 / 30, -- how frequently does Noice need to check for ui updates? This has no effect when in blocking mode.
292297
---@type NoiceConfigViews
@@ -644,6 +649,33 @@ end)
644649

645650
> You can add custom commands with `config.commands`
646651
652+
### ↪️ Command Redirection
653+
654+
Sometimes it's useful to redirect the messages generated by a command or function
655+
to a different view. That can be easily achieved with command redirection.
656+
657+
The redirect API can taken an optional `routes` parameter, which defaults to `{config.redirect}`.
658+
659+
```lua
660+
-- redirect ":hi"
661+
require("noice").redirect("hi")
662+
663+
-- redirect some function
664+
require("noice").redirect(function()
665+
print("test")
666+
end)
667+
```
668+
669+
Adding the following keymap, will redirect the active cmdline when pressing `<S-Enter>`.
670+
The cmdline stays open, so you can change the command and execute it again.
671+
When exiting the cmdline, the popup window will be focused.
672+
673+
```lua
674+
vim.keymap.set("c", "<S-Enter>", function()
675+
require("noice").redirect(vim.fn.getcmdline())
676+
end, { desc = "Redirect Cmdline" })
677+
```
678+
647679
### Lsp Hover Doc Scrolling
648680

649681
```lua

lua/noice/config/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ function M.defaults()
4848
-- Icons for completion item kinds (see defaults at noice.config.icons.kinds)
4949
kind_icons = {}, -- set to `false` to disable icons
5050
},
51+
-- default options for require('noice').redirect
52+
-- see the section on Command Redirection
53+
---@type NoiceRouteConfig
54+
redirect = {
55+
view = "popup",
56+
filter = { event = "msg_show" },
57+
},
5158
-- You can add any custom commands below that will be available with `:Noice command`
5259
---@type table<string, NoiceCommand>
5360
commands = {

lua/noice/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ function M.enable()
7171
end
7272
end
7373

74+
-- Redirect any messages generated by a command or function
75+
---@param cmd string|fun() command or function to execute
76+
---@param routes? NoiceRouteConfig[] custom routes. Defaults to `config.redirect`
77+
function M.redirect(cmd, routes)
78+
return require("noice.message.router").redirect(cmd, routes)
79+
end
80+
7481
---@param msg string
7582
---@param level number|string
7683
---@param opts? table<string, any>

lua/noice/message/router.lua

+45-3
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,59 @@ function M.disable()
4444
end
4545

4646
---@param route NoiceRouteConfig
47-
function M.add(route)
47+
---@param pos? number
48+
function M.add(route, pos)
4849
local ret = {
4950
filter = route.filter,
5051
opts = route.opts or {},
5152
view = route.view and View.get_view(route.view, route.opts) or nil,
5253
}
5354
if ret.view == nil then
54-
ret.view = nil
5555
ret.opts.skip = true
5656
end
57-
table.insert(M._routes, ret)
57+
if pos then
58+
table.insert(M._routes, pos, ret)
59+
else
60+
table.insert(M._routes, ret)
61+
end
62+
return ret
63+
end
64+
65+
-- Redirect any messages generated by a command or function
66+
---@param cmd string|fun() command or function to execute
67+
---@param routes? NoiceRouteConfig[] custom routes. Defaults to `config.redirect`
68+
function M.redirect(cmd, routes)
69+
routes = routes or { Config.options.redirect }
70+
if type(cmd) == "string" then
71+
local cmd_str = cmd
72+
cmd = function()
73+
vim.cmd(cmd_str)
74+
end
75+
end
76+
-- process any pending messages
77+
M.update()
78+
79+
local added = {}
80+
local pos = 1
81+
-- add temporary routes
82+
for _, route in ipairs(routes) do
83+
table.insert(added, M.add(route, pos))
84+
pos = pos + 1
85+
end
86+
87+
-- execute callback
88+
Util.try(cmd)
89+
90+
-- force a redraw to make sure we received all msg_show events
91+
vim.cmd.redraw()
92+
93+
-- process messages
94+
M.update()
95+
96+
-- remove temporary routes
97+
M._routes = vim.tbl_filter(function(r)
98+
return not vim.tbl_contains(added, r)
99+
end, M._routes)
58100
end
59101

60102
function M.setup()

0 commit comments

Comments
 (0)