Skip to content

Commit 5388008

Browse files
authored
Merge pull request #9 from rootiest/v2.0.0
V2.0.0 Terminal Callbacks
2 parents 66c902c + 138de4a commit 5388008

File tree

5 files changed

+252
-37
lines changed

5 files changed

+252
-37
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 rootiest
3+
Copyright (c) 2024 Rootiest (https://github.com/rootiest)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,72 @@ Available `[options]`:
457457
#### Open floating terminal
458458

459459
```lua
460-
require("nvim_updater.utils").open_floating_terminal( [options] )
460+
require("nvim_updater.utils").open_floating_terminal( [TerminalOptions] )
461461
```
462462

463463
This is a helper function for opening a floating terminal that is used by the
464464
updater to display the terminal output.
465465

466-
Available `[options]`:
466+
Available `[TerminalOptions]`:
467467

468468
- **`cmd`**: Command to run in the terminal.
469469
- **`filetype`**: Filetype to assign to the terminal buffer.
470470
Default is `"nvim_updater_term"`.
471-
- **`ispreupdate`**: Whether the terminal will be followed by an update build.
471+
- **`ispreupdate`**: (Deprecated)
472+
Whether the terminal will be followed by an update build.
472473
Default is `false`.
473474
- **`autoclose`**: Whether the terminal buffer will be closed when the process ends.
474475
Default is `false`.
476+
- **`callback`**: A function to call when the terminal buffer is closed.
477+
Default is `nil`.
478+
479+
> [!NOTE]
480+
> The `ispreupdate` option is now deprecated and will be removed in a future version.
481+
482+
##### Callback Function
483+
484+
The callback function allows you to define a function to be triggered when the
485+
terminal buffer is closed.
486+
487+
The callback function is called with the following arguments:
488+
489+
- `ev`: The [event object](https://neovim.io/doc/user/api.html#event-args)
490+
received from the terminal close event.
491+
- `exit_code`: The exit code of the process that was run in the terminal buffer.
492+
493+
In most cases, this will occur after the process has completed.
494+
495+
However, if the window is closed before the process is complete, the exit code
496+
returned will be `-1`. This allows us to identify those scenarios and handle them
497+
appropriately.
498+
499+
Here is an example of how to use the callback function:
500+
501+
```lua
502+
require("nvim_updater.utils").open_floating_terminal({
503+
command = "my_test_script.sh", -- Command to run
504+
filetype = "my_test_script_term", -- Filetype to assign
505+
autoclose = true, -- Close the terminal buffer automatically
506+
callback = function(result) -- Callback function
507+
if result.result_code == -1 then
508+
vim.notify(
509+
"Terminal closed before process completed",
510+
vim.log.levels.ERROR
511+
)
512+
elseif result.result_code == 0 then
513+
vim.notify(
514+
"Terminal process completed successfully",
515+
vim.log.levels.INFO
516+
)
517+
else
518+
vim.notify(
519+
"Terminal process failed with exit code: " .. result.result_code,
520+
vim.log.levels.ERROR
521+
)
522+
end
523+
end,
524+
})
525+
```
475526

476527
#### Setup
477528

doc/nvim_updater.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,57 @@ Available `[options]`:
326326
- `filetype` -> Filetype to assign to the terminal buffer.
327327
Default is `"nvim_updater_term"`.
328328
- `ispreupdate` -> Whether the terminal will be followed by an update build.
329+
(Deprecated)
329330
Default is `false`.
330331
- `autoclose` -> Whether the terminal buffer will be closed when the process ends.
331332
Default is `false`.
332333

334+
- `callback` -> A function to call when the terminal buffer is closed.
335+
Default is `nil`.
336+
337+
- Callback Function -
338+
339+
The callback function allows you to define a function to be triggered when the
340+
terminal buffer is closed.
341+
342+
The callback function is called with the following arguments:
343+
344+
- `ev`: -> The event object received from the terminal close event.
345+
See: *event-args*
346+
- `exit_code`: -> The exit code of the process that was run in the terminal buffer.
347+
348+
In most cases, this will occur after the process has completed.
349+
350+
However, if the window is closed before the process is complete, the exit code
351+
returned will be `-1`. This allows us to identify those scenarios and handle them
352+
appropriately.
353+
354+
Example callback function 󱞣
355+
>lua
356+
require("nvim_updater.utils").open_floating_terminal({
357+
command = "my_test_script.sh", -- Command to run
358+
filetype = "my_test_script_term", -- Filetype to assign
359+
autoclose = true, -- Close the terminal buffer automatically
360+
callback = function(result) -- Callback function
361+
if result.result_code == -1 then
362+
vim.notify(
363+
"Terminal closed before process completed",
364+
vim.log.levels.ERROR
365+
)
366+
elseif result.result_code == 0 then
367+
vim.notify(
368+
"Terminal process completed successfully",
369+
vim.log.levels.INFO
370+
)
371+
else
372+
vim.notify(
373+
"Terminal process failed with exit code: " .. result.result_code,
374+
vim.log.levels.ERROR
375+
)
376+
end
377+
end,
378+
})
379+
<
333380
--- Setup ---
334381
>lua
335382
require("nvim_updater").setup( [options] )

lua/nvim_updater/init.lua

Lines changed: 103 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,23 @@ function P.update_neovim(opts)
141141

142142
local build_command = "cd " .. source_dir .. " && make CMAKE_BUILD_TYPE=" .. build_type .. " && sudo make install"
143143

144+
local update_command = git_commands .. " && " .. build_command
145+
144146
-- Use the open_floating_terminal from the 'utils' module
145-
utils.open_floating_terminal(git_commands .. " && " .. build_command, "neovim_updater_term.updating", false, true)
147+
utils.open_floating_terminal({
148+
command = update_command,
149+
filetype = "neovim_updater_term.updating",
150+
ispreupdate = false,
151+
autoclose = true,
152+
callback = function(results)
153+
if results.result_code ~= 0 then
154+
utils.notify("Neovim update failed with error code: " .. results.result_code, vim.log.levels.ERROR)
155+
else
156+
utils.notify("Neovim update complete!", vim.log.levels.INFO)
157+
utils.notify("Please restart Neovim for the changes to take effect.", vim.log.levels.INFO)
158+
end
159+
end,
160+
})
146161

147162
-- Go to insert mode
148163
vim.cmd("startinsert")
@@ -154,7 +169,10 @@ end
154169
--- Remove the Neovim source directory or a custom one.
155170
---@function P.remove_source_dir
156171
---@param opts table|nil Optional table for 'source_dir'
157-
---@return boolean success True if the directory was successfully removed, false otherwise
172+
---@return boolean|nil success True if the directory was successfully removed
173+
--- False if the directory does not exist or an error occurred
174+
--- nil if the function is delayed until the terminal is closed
175+
--- Check the U.defered_value variable for the result
158176
function P.remove_source_dir(opts)
159177
opts = opts or {}
160178
local source_dir = opts.source_dir ~= "" and opts.source_dir or P.default_config.source_dir
@@ -172,12 +190,47 @@ function P.remove_source_dir(opts)
172190
if not err then
173191
err = "Unknown error"
174192
end
175-
utils.notify(
176-
"Error removing Neovim source directory: " .. source_dir .. "\n" .. err,
177-
vim.log.levels.ERROR
178-
)
179193
utils.notify("Source directory removal failed with vim.fs.rm", vim.log.levels.DEBUG)
180-
return false
194+
195+
-- Define callback function for checking rm
196+
local function check_rm()
197+
-- Check if the source directory still exists
198+
if not utils.directory_exists(source_dir) then
199+
utils.notify(
200+
"Successfully removed Neovim source directory: " .. source_dir,
201+
vim.log.levels.INFO
202+
)
203+
return true
204+
end
205+
utils.notify("Failed to remove Neovim source directory: " .. source_dir, vim.log.levels.ERROR)
206+
return false
207+
end
208+
209+
-- Attempt to remove with elevated privileges
210+
local rm_msg = "echo Attempting to remove source directory with elevated privileges.\n"
211+
.. "echo Please authorize sudo and press enter.\n"
212+
local privileged_rm = rm_msg .. "sudo rm -rf " .. source_dir
213+
utils.open_floating_terminal({
214+
command = privileged_rm,
215+
filetype = "neovim_updater_term.privileged_rm",
216+
ispreupdate = false,
217+
autoclose = true,
218+
callback = function(results)
219+
if results.result_code == 0 then
220+
-- Double-check the results
221+
check_rm()
222+
else
223+
utils.notify(
224+
"Failed to remove Neovim source directory: " .. source_dir,
225+
vim.log.levels.ERROR
226+
)
227+
end
228+
end,
229+
})
230+
-- Go to insert mode
231+
vim.cmd("startinsert")
232+
233+
return nil
181234
end
182235
end
183236
-- Fallback to vim.fn.delete if vim.fs.rm is not available
@@ -211,23 +264,40 @@ function P.generate_source_dir(opts)
211264
local repo = "https://github.com/neovim/neovim.git"
212265
local branch = opts.branch ~= "" and opts.branch or P.default_config.branch
213266

214-
-- Build the command to fetch the latest changes from the remote repository
215-
local fetch_command = ("cd ~ && git clone %s %s"):format(repo, source_dir)
216-
217-
-- Checkout the branch
218-
local checkout_command = "cd " .. source_dir .. " && git checkout " .. branch
219-
220-
-- Combine commands
221-
local complete_command = fetch_command .. " && " .. checkout_command
222-
223-
-- Notify the user that the clone is starting
224-
utils.notify("Cloning Neovim source...", vim.log.levels.INFO)
225-
226-
-- Open a terminal window
227-
utils.open_floating_terminal(complete_command, "neovim_updater_term.cloning", false, false)
267+
if not utils.directory_exists(source_dir) then
268+
-- Build the command to fetch the latest changes from the remote repository
269+
local fetch_command = ("cd ~ && git clone %s %s"):format(repo, source_dir)
270+
271+
-- Checkout the branch
272+
local checkout_command = "cd " .. source_dir .. " && git checkout " .. branch
273+
274+
-- Combine commands
275+
local complete_command = fetch_command .. " && " .. checkout_command
276+
277+
-- Notify the user that the clone is starting
278+
utils.notify("Cloning Neovim source...", vim.log.levels.INFO)
279+
280+
-- Open a terminal window
281+
utils.open_floating_terminal({
282+
command = complete_command,
283+
filetype = "neovim_updater_term.cloning",
284+
ispreupdate = false,
285+
autoclose = true,
286+
callback = function(results)
287+
if results.result_code == 0 then
288+
utils.notify("Neovim source cloned successfully", vim.log.levels.INFO)
289+
else
290+
utils.notify("Failed to clone Neovim source: " .. results.result_code, vim.log.levels.ERROR)
291+
end
292+
end,
293+
})
228294

229-
-- Set the update count to "0"
230-
P.last_status.count = "0"
295+
-- Set the update count to "0"
296+
P.last_status.count = "0"
297+
else
298+
-- Notify the user that the source directory already exists
299+
utils.notify("Neovim source directory already exists: " .. source_dir, vim.log.levels.WARN)
300+
end
231301

232302
-- Return the source directory
233303
return source_dir
@@ -354,9 +424,10 @@ end
354424
--- @param short? boolean Optional. Whether to show a short commit list. Only used if `isupdate` is a boolean.
355425
function P.show_new_commits(isupdate, short)
356426
-- If the first argument is a table, treat it as an options table.
427+
local doupdate = false
357428
if type(isupdate) == "table" then
358429
local opts = isupdate
359-
isupdate = opts.isupdate
430+
doupdate = opts.isupdate
360431
short = opts.short
361432
end
362433
-- Define the path to the Neovim source directory
@@ -409,8 +480,15 @@ function P.show_new_commits(isupdate, short)
409480
utils.open_floating_terminal({
410481
command = term_command,
411482
filetype = "neovim_updater_term.changes",
412-
ispreupdate = isupdate,
483+
ispreupdate = false,
413484
autoclose = false,
485+
callback = function()
486+
if doupdate then
487+
utils.ConfirmPrompt("Perform Neovim update?", function()
488+
P.update_neovim()
489+
end)
490+
end
491+
end,
414492
})
415493
else
416494
utils.notify("No new Neovim commits.", vim.log.levels.INFO)

0 commit comments

Comments
 (0)