Skip to content

Commit 6c124b6

Browse files
authored
Merge pull request #10 from rootiest/v2.1.0
V2.1.0 Update Workflow Improvements
2 parents 5388008 + a9ef7a2 commit 6c124b6

File tree

3 files changed

+201
-13
lines changed

3 files changed

+201
-13
lines changed

doc/tags

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
event-args nvim_updater.txt /*event-args*
2+
neovim-updater nvim_updater.txt /*neovim-updater*
3+
neovim_updater nvim_updater.txt /*neovim_updater*
4+
nvim-updater nvim_updater.txt /*nvim-updater*
5+
nvim-updater-commands nvim_updater.txt /*nvim-updater-commands*
6+
nvim-updater-compatibility nvim_updater.txt /*nvim-updater-compatibility*
7+
nvim-updater-config nvim_updater.txt /*nvim-updater-config*
8+
nvim-updater-contents nvim_updater.txt /*nvim-updater-contents*
9+
nvim-updater-contributions nvim_updater.txt /*nvim-updater-contributions*
10+
nvim-updater-external nvim_updater.txt /*nvim-updater-external*
11+
nvim-updater-installation nvim_updater.txt /*nvim-updater-installation*
12+
nvim-updater-integrations nvim_updater.txt /*nvim-updater-integrations*
13+
nvim-updater-intro nvim_updater.txt /*nvim-updater-intro*
14+
nvim-updater-keymaps nvim_updater.txt /*nvim-updater-keymaps*
15+
nvim-updater-license nvim_updater.txt /*nvim-updater-license*
16+
nvim-updater-lua-funcs nvim_updater.txt /*nvim-updater-lua-funcs*
17+
nvim-updater-prereqs nvim_updater.txt /*nvim-updater-prereqs*
18+
nvim-updater.txt nvim_updater.txt /*nvim-updater.txt*
19+
nvim_updater nvim_updater.txt /*nvim_updater*

lua/nvim_updater/init.lua

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ P.default_config = {
2020

2121
P.last_status = {
2222
count = "?",
23+
retry = false,
2324
}
2425

2526
--- Setup default keymaps for updating Neovim or removing source based on user configuration.
@@ -111,6 +112,14 @@ function P.update_with_changes()
111112
P.show_new_commits(true)
112113
end
113114

115+
--- Helper function to retry update
116+
local function update_with_retry()
117+
if P.last_status.retry then
118+
P.last_status.retry = false
119+
P.update_neovim()
120+
end
121+
end
122+
114123
--- Update Neovim from source and show progress in a floating terminal.
115124
---@param opts table|nil Optional options for the update process (branch, build_type, etc.)
116125
function P.update_neovim(opts)
@@ -152,18 +161,22 @@ function P.update_neovim(opts)
152161
callback = function(results)
153162
if results.result_code ~= 0 then
154163
utils.notify("Neovim update failed with error code: " .. results.result_code, vim.log.levels.ERROR)
164+
utils.ConfirmPrompt("Remove build directory and try again?", function()
165+
P.last_status.count = "?"
166+
P.last_status.retry = true
167+
P.remove_source_dir({ source_dir = source_dir .. "/build" })
168+
end)
155169
else
156-
utils.notify("Neovim update complete!", vim.log.levels.INFO)
170+
utils.notify("Neovim update complete!", vim.log.levels.INFO, true)
157171
utils.notify("Please restart Neovim for the changes to take effect.", vim.log.levels.INFO)
172+
-- Update the status count
173+
P.last_status.count = "0"
158174
end
159175
end,
160176
})
161177

162178
-- Go to insert mode
163179
vim.cmd("startinsert")
164-
165-
-- Update the status count
166-
P.last_status.count = "0"
167180
end
168181

169182
--- Remove the Neovim source directory or a custom one.
@@ -183,8 +196,10 @@ function P.remove_source_dir(opts)
183196
-- Use pcall to attempt to call the function
184197
local success, err = pcall(vim.fs.rm, source_dir, { recursive = true, force = true })
185198
if success then
186-
utils.notify("Successfully removed Neovim source directory: " .. source_dir, vim.log.levels.INFO)
199+
P.last_status.count = "?"
200+
utils.notify("Successfully removed Neovim source directory: " .. source_dir, vim.log.levels.INFO, true)
187201
utils.notify("Source directory removed with vim.fs.rm", vim.log.levels.DEBUG)
202+
update_with_retry()
188203
return true
189204
else
190205
if not err then
@@ -196,18 +211,23 @@ function P.remove_source_dir(opts)
196211
local function check_rm()
197212
-- Check if the source directory still exists
198213
if not utils.directory_exists(source_dir) then
214+
P.last_status.count = "?"
199215
utils.notify(
200216
"Successfully removed Neovim source directory: " .. source_dir,
201-
vim.log.levels.INFO
217+
vim.log.levels.INFO,
218+
true
202219
)
220+
update_with_retry()
203221
return true
204222
end
205223
utils.notify("Failed to remove Neovim source directory: " .. source_dir, vim.log.levels.ERROR)
206224
return false
207225
end
208226

209227
-- Attempt to remove with elevated privileges
210-
local rm_msg = "echo Attempting to remove source directory with elevated privileges.\n"
228+
local rm_msg = "echo Attempting to remove "
229+
.. source_dir
230+
.. " directory with elevated privileges.\n"
211231
.. "echo Please authorize sudo and press enter.\n"
212232
local privileged_rm = rm_msg .. "sudo rm -rf " .. source_dir
213233
utils.open_floating_terminal({
@@ -236,8 +256,10 @@ function P.remove_source_dir(opts)
236256
-- Fallback to vim.fn.delete if vim.fs.rm is not available
237257
local success, err = vim.fn.delete(source_dir, "rf")
238258
if success == 0 then
239-
utils.notify("Successfully removed Neovim source directory: " .. source_dir, vim.log.levels.INFO)
259+
P.last_status.count = "?"
260+
utils.notify("Successfully removed Neovim source directory: " .. source_dir, vim.log.levels.INFO, true)
240261
utils.notify("Source directory removed with vim.fn.delete", vim.log.levels.DEBUG)
262+
update_with_retry()
241263
return true
242264
else
243265
if not err then
@@ -285,15 +307,15 @@ function P.generate_source_dir(opts)
285307
autoclose = true,
286308
callback = function(results)
287309
if results.result_code == 0 then
288-
utils.notify("Neovim source cloned successfully", vim.log.levels.INFO)
310+
utils.notify("Neovim source cloned successfully", vim.log.levels.INFO, true)
311+
-- Set the update count to "0"
312+
P.last_status.count = "0"
289313
else
290314
utils.notify("Failed to clone Neovim source: " .. results.result_code, vim.log.levels.ERROR)
315+
P.last_status.count = "?"
291316
end
292317
end,
293318
})
294-
295-
-- Set the update count to "0"
296-
P.last_status.count = "0"
297319
else
298320
-- Notify the user that the source directory already exists
299321
utils.notify("Neovim source directory already exists: " .. source_dir, vim.log.levels.WARN)
@@ -429,6 +451,7 @@ function P.show_new_commits(isupdate, short)
429451
local opts = isupdate
430452
doupdate = opts.isupdate
431453
short = opts.short
454+
isupdate = opts.isupdate
432455
end
433456
-- Define the path to the Neovim source directory
434457
local source_dir = P.default_config.source_dir
@@ -491,7 +514,7 @@ function P.show_new_commits(isupdate, short)
491514
end,
492515
})
493516
else
494-
utils.notify("No new Neovim commits.", vim.log.levels.INFO)
517+
utils.notify("No new Neovim commits.", vim.log.levels.INFO, true)
495518
-- Update status count
496519
P.last_status.count = "0"
497520
end

lua/nvim_updater/utils.lua

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,150 @@ function U.is_installed(plugin)
413413
end
414414
end
415415

416+
--- Function to execute a shell command and return the output as a tabl
417+
---@param command string|string[] The shell command(s) to execute
418+
---@return string[] output The output of the shell command
419+
function U.run_hidden_command(command)
420+
-- Convert the command to a string if it's a table
421+
if type(command) == "table" then
422+
command = table.concat(command, "\n")
423+
end
424+
-- Execute the command and capture the output
425+
local handle = io.popen(command)
426+
427+
-- Check if the handle is nil (command may have failed)
428+
if not handle then
429+
error("Failed to run command: " .. command) -- Raise an error if the command failed
430+
end
431+
432+
local output = handle:read("*a") -- Read all output
433+
handle:close() -- Important to close the handle safely
434+
435+
-- Check if the output is nil to avoid issues with splitting
436+
if output == nil then
437+
return {} -- Return an empty table if there is no output
438+
end
439+
440+
-- Split output by newline into a table
441+
local lines = {}
442+
for line in output:gmatch("[^\n]+") do
443+
table.insert(lines, line) -- Insert each line into the table
444+
end
445+
446+
return lines
447+
end
448+
449+
--- Function to draw a floating window to display data
450+
---@param data string[] The data to display in the floating window
451+
---@return boolean success True if the floating window was successfully drawn
452+
function U.draw_floating_window(data)
453+
-- Verify the data is not empty
454+
if not data or #data == 0 then
455+
U.notify("No data to display", vim.log.levels.WARN)
456+
return false
457+
end
458+
459+
-- Add a padding character to each line
460+
for i = 1, #data do
461+
data[i] = data[i] .. " "
462+
end
463+
464+
local width = 0
465+
local height = #data - 2
466+
467+
-- Find the width of the longest line for proper sizing
468+
for _, line in ipairs(data) do
469+
width = math.max(width, #line)
470+
end
471+
472+
-- Determine padding and calculate window size
473+
local padded_width = width + 2
474+
local padded_height = height + 2
475+
476+
-- Get the current window's dimensions
477+
local win_id = vim.api.nvim_get_current_win()
478+
local win_config = vim.api.nvim_win_get_config(win_id)
479+
local current_win_width = win_config.width
480+
local current_win_height = win_config.height
481+
482+
-- Calculate the center position for the floating window
483+
local col = math.floor((current_win_width - padded_width) / 2)
484+
local row = math.floor((current_win_height - padded_height) / 2)
485+
486+
local buf_id = vim.api.nvim_create_buf(false, true) -- Create a new buffer (scratch)
487+
488+
-- Set buffer content to the output
489+
vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, data)
490+
491+
-- Set window options for floating window
492+
local opts = {
493+
relative = "win",
494+
win = win_id,
495+
width = padded_width,
496+
height = padded_height,
497+
anchor = "NW",
498+
col = col,
499+
row = row,
500+
border = "rounded", -- Rounded border
501+
}
502+
503+
-- Create floating window
504+
local float_win_id = vim.api.nvim_open_win(buf_id, true, opts)
505+
506+
-- Disable line numbers
507+
vim.api.nvim_set_option_value("number", false, { scope = "local", win = float_win_id }) -- Disable line numbers
508+
vim.api.nvim_set_option_value("relativenumber", false, { scope = "local", win = float_win_id }) -- Disable relative line numbers
509+
510+
-- Set additional options for the floating window
511+
vim.api.nvim_set_option_value("wrap", true, { scope = "local", win = float_win_id }) -- Enable line wrapping
512+
vim.api.nvim_set_option_value("scrolloff", 0, { scope = "local", win = float_win_id }) -- Disable scrolloff for horizontal
513+
vim.api.nvim_set_option_value("sidescrolloff", 0, { scope = "local", win = float_win_id }) -- Disable scrolloff for vertical
514+
vim.api.nvim_set_option_value("list", false, { scope = "local", win = float_win_id }) -- Disable whitespace characters
515+
516+
-- Helper function to close the floating window
517+
local function closing()
518+
vim.api.nvim_win_close(float_win_id, true)
519+
end
520+
521+
-- Helper function to pick an item
522+
local function picking()
523+
local line = vim.api.nvim_get_current_line()
524+
local item = line:match("%s*(.*)"):gsub("%s+$", "") -- Remove trailing spaces
525+
-- Store item in register
526+
vim.fn.setreg('"', item)
527+
closing()
528+
end
529+
530+
-- Bind keys for closing
531+
for _, key in ipairs({ "q", "<Esc>" }) do
532+
vim.api.nvim_buf_set_keymap(buf_id, "n", key, "", {
533+
noremap = true,
534+
silent = true,
535+
callback = function()
536+
closing()
537+
end,
538+
desc = "Close terminal window",
539+
})
540+
end
541+
542+
-- Bind keys for picking
543+
for _, key in ipairs({ "y", "<CR>", "<Space>" }) do
544+
vim.api.nvim_buf_set_keymap(buf_id, "n", key, "", {
545+
noremap = true,
546+
silent = true,
547+
callback = function()
548+
picking()
549+
end,
550+
desc = "Select item",
551+
})
552+
end
553+
554+
-- Check for errors
555+
if float_win_id == 0 then
556+
U.notify("Failed to create floating window", vim.log.levels.ERROR)
557+
return false
558+
end
559+
return true
560+
end
561+
416562
return U

0 commit comments

Comments
 (0)