Skip to content

Commit 138de4a

Browse files
committed
📝 Docs(floating terminal): update documentation to reflect new callback function
Updates the documentation to reflect the new callback function option and note the deprecation of ispreupdate
1 parent 997c612 commit 138de4a

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

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] )

0 commit comments

Comments
 (0)