Skip to content

Commit 69de32b

Browse files
committed
feat: allow disabling builtin UI in favor of vim.notify
1 parent 368eb69 commit 69de32b

4 files changed

Lines changed: 94 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ M.config = {
286286
},
287287
-- UI configuration.
288288
ui = {
289+
-- Whether to enable builtin output buffer or fallback to `vim.notify`.
290+
enabled = true,
289291
-- Auto-close window after repository was opened.
290292
auto_close = true,
291293
-- Delay window closing.

lua/git-dev/init.lua

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ M.config = {
5151
},
5252
-- UI configuration.
5353
ui = {
54+
-- Whether to enable builtin output buffer or fallback to `vim.notify`.
55+
enabled = true,
5456
-- Auto-close window after repository was opened.
5557
auto_close = true,
5658
-- Delay window closing.
@@ -162,6 +164,7 @@ M.open = function(repo, ref, opts)
162164
local config = vim.tbl_deep_extend("force", M.config, opts or {})
163165
local ui = M.ui
164166
ui:show()
167+
ui:print("Opening " .. repo):emit()
165168

166169
local gitcmd = require("git-dev.gitcmd"):init {
167170
cmd = config.git.command,
@@ -189,7 +192,7 @@ M.open = function(repo, ref, opts)
189192
or parsed_repo.branch
190193

191194
if not branch and parsed_repo.full_blob then
192-
ui:print "Could not detect branch / tag / commit in given URI."
195+
ui:print "Could not detect a ref in given URI, falling back to default."
193196
end
194197

195198
local repo_dir = config.repositories_dir
@@ -200,7 +203,7 @@ M.open = function(repo, ref, opts)
200203
ui:print(parsed_repo)
201204
end
202205

203-
local post_action_callback = vim.schedule_wrap(function()
206+
local post_action_callback = function()
204207
if vim.fn.isdirectory(repo_dir) == 0 then
205208
ui:print(
206209
"Repository not found at: " .. parsed_repo.repo_url .. ", aborting..."
@@ -279,6 +282,10 @@ M.open = function(repo, ref, opts)
279282
M.history:add(repo, ref, opts, parsed_repo)
280283

281284
ui:print "Done."
285+
end
286+
287+
local clone_callback = vim.schedule_wrap(function()
288+
pcall(post_action_callback)
282289
if config.ui.auto_close then
283290
ui:close(config.ui.close_after_ms)
284291
end
@@ -288,7 +295,7 @@ M.open = function(repo, ref, opts)
288295
ui:print "Repository directory exists, refreshing..."
289296
gitcmd:refresh(
290297
{ repo_dir = repo_dir, extra_args = config.git.fetch_args },
291-
post_action_callback
298+
clone_callback
292299
)
293300
else
294301
-- Fresh clone
@@ -298,7 +305,7 @@ M.open = function(repo, ref, opts)
298305
repo_dir = repo_dir,
299306
branch = parsed_repo.branch,
300307
extra_args = config.git.clone_args,
301-
}, post_action_callback)
308+
}, clone_callback)
302309
end
303310
end
304311

@@ -462,26 +469,37 @@ M.setup = function(opts)
462469
vim.fn.mkdir(vim.fs.dirname(M.config.history.path), "p")
463470

464471
-- Prepare UI
465-
local win_config
466-
if M.config.ui.mode == "split" then
467-
local v = vim.version()
468-
-- `nvim_open_win` supports splitting in Neovim>=0.10.0
469-
-- `ge` was added in 0.10.0.
470-
if vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) == false then
471-
win_config = M.config.ui.split_win_config
472+
if M.config.ui.enabled then
473+
local win_config
474+
if M.config.ui.mode == "split" then
475+
local v = vim.version()
476+
-- `nvim_open_win` supports splitting in Neovim>=0.10.0
477+
-- `ge` was added in 0.10.0.
478+
if
479+
vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) == false
480+
then
481+
win_config = M.config.ui.split_win_config
482+
else
483+
vim.notify(
484+
"Split mode is not supported in Neovim < 0.10.0. "
485+
.. "Falling back to floating mode."
486+
)
487+
win_config = M.config.ui.floating_win_config
488+
end
472489
else
473-
vim.notify(
474-
"Split mode is not supported in Neovim < 0.10.0. "
475-
.. "Falling back to floating mode."
476-
)
477490
win_config = M.config.ui.floating_win_config
478491
end
492+
M.ui = require("git-dev.ui"):init {
493+
win_config = win_config,
494+
}
495+
vim.api.nvim_create_user_command("GitDevToggleUI", function(_)
496+
require("git-dev").toggle_ui()
497+
end, {
498+
desc = "Toggle output window.",
499+
})
479500
else
480-
win_config = M.config.ui.floating_win_config
501+
M.ui = require("git-dev.uistub"):init {}
481502
end
482-
M.ui = require("git-dev.ui"):init {
483-
win_config = win_config,
484-
}
485503

486504
-- Initialize store
487505
---@type GitDevHistory
@@ -496,7 +514,7 @@ M.setup = function(opts)
496514
xdg.enable(M.config.xdg_handler)
497515
vim.api.nvim_create_user_command("GitDevXDGHandle", function(cmd_args)
498516
local uri = U.parse_cmd_args(cmd_args)
499-
M.ui:print(xdg.handle(uri))
517+
xdg.handle(uri)
500518
end, {
501519
desc = "xdg-open handler for git-dev.nvim URIs.",
502520
nargs = "*",
@@ -541,12 +559,6 @@ M.setup = function(opts)
541559
complete = complete_from_session,
542560
})
543561

544-
vim.api.nvim_create_user_command("GitDevToggleUI", function(_)
545-
require("git-dev").toggle_ui()
546-
end, {
547-
desc = "Toggle output window.",
548-
})
549-
550562
vim.api.nvim_create_user_command(
551563
"GitDevRecents",
552564
"Telescope git_dev recents",

lua/git-dev/ui.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ function UI:print(...)
111111
return self
112112
end
113113

114+
function UI:emit() end
115+
114116
---Closes window after given delay.
115117
---@param delay? integer Time to wait before closing the window in ms.
116118
function UI:close(delay)

lua/git-dev/uistub.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---A UI stub, implementing the same API as UI class.
2+
---It concatenates with each `print` invocation, and displays text once closed
3+
---with a single call to `vim.notify`.
4+
---@class UIStub
5+
local UIStub = {}
6+
7+
function UIStub:init(o)
8+
setmetatable(o or {}, self)
9+
self.__index = self
10+
return o
11+
end
12+
13+
function UIStub:show(_)
14+
return self
15+
end
16+
17+
function UIStub:redraw() end
18+
function UIStub:toggle(_) end
19+
20+
local st = ""
21+
22+
function UIStub:print(...)
23+
-- Transform arguments into a space delimited string and append '\n'.
24+
local args = { ... }
25+
local s = ""
26+
for i = 1, #args do
27+
if type(args[i]) == "string" then
28+
s = s .. " " .. args[i]
29+
else
30+
s = s .. " " .. vim.inspect(args[i])
31+
end
32+
end
33+
s = s .. "\n"
34+
vim.schedule(function()
35+
st = st .. s
36+
end)
37+
return self
38+
end
39+
40+
function UIStub:emit(_)
41+
-- Schedule the write.
42+
vim.schedule(function()
43+
vim.notify(st)
44+
st = ""
45+
end)
46+
end
47+
48+
function UIStub:close()
49+
self:emit()
50+
end
51+
52+
return UIStub

0 commit comments

Comments
 (0)