Skip to content

Commit d424a17

Browse files
committed
feat: add -keep_cursor flag for UgatermOpen
1 parent 09e07a8 commit d424a17

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

doc/ugaterm.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ COMMANDS *ugaterm-commands*
4545
recently used one.
4646
-toggle If the terminal is already open, it will be closed.
4747
-select Uses |vim.ui.select()| to select the terminal to open.
48+
-keep_cursor Open a terminal without moving the cursor.
4849

4950

5051
*:UgatermHide*

lua/ugaterm/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151
function M.create_commands()
5252
vim.api.nvim_create_user_command("UgatermOpen", function(opt)
5353
local parsedArgs = parse(opt.fargs, {
54-
boolean = { "new", "toggle", "select" },
54+
boolean = { "new", "toggle", "select", "keep_cursor" },
5555
string = { "name" },
5656
})
5757
local cmd
@@ -74,7 +74,7 @@ function M.create_commands()
7474
if cmdline:sub(1, cursor_pos):find("%-name%s+%S*$") then
7575
return get_bufnames()
7676
end
77-
local items = { "-new", "-toggle", "-select", "-name" }
77+
local items = { "-new", "-toggle", "-select", "-keep_cursor", "-name" }
7878
for i = #items, 1, -1 do
7979
if cmdline:find(items[i], 1, true) then
8080
table.remove(items, i)

lua/ugaterm/terminal.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262

6363
--- Open a most recently used terminal or new one.
6464
--- If it's already open, exit immediately.
65-
---@param flags { new: boolean?, toggle: boolean?, select: boolean? }
65+
---@param flags { new: boolean?, toggle: boolean?, select: boolean?, keep_cursor: boolean? }
6666
---@param name? string
6767
---@param cmd? string | string[]
6868
function Terminal:open(flags, name, cmd)
@@ -93,6 +93,11 @@ function Terminal:open(flags, name, cmd)
9393

9494
self:send(cmd)
9595
vim.cmd("do User UgatermEnter")
96+
if flags.keep_cursor then
97+
vim.schedule(function()
98+
vim.fn.win_gotoid(self.prev_winid)
99+
end)
100+
end
96101
end
97102

98103
if flags.select then

lua/ugaterm/terminal_spec.lua

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ describe("Test for terminal", function()
6262
terminal:open({ select = true })
6363
assert.equal("terminal://1", vim.api.nvim_buf_get_name(0))
6464
end)
65+
66+
it("-keep_cursor", function()
67+
assert.equal(1, num_win())
68+
local prev_winid = vim.api.nvim_get_current_win()
69+
terminal:open({ keep_cursor = true })
70+
vim.wait(500)
71+
assert.equal(2, num_win())
72+
assert.equal(prev_winid, vim.api.nvim_get_current_win())
73+
end)
6574
end)
6675

6776
describe("hide()", function()

0 commit comments

Comments
 (0)