Skip to content

Commit

Permalink
feat(popup): add show_text function and refactor commands
Browse files Browse the repository at this point in the history
- Add a new function `show_text` in `popup.lua` to display text in a popup window.
- Refactor `HurlManageVariable` and `HurlDebugInfo` commands in `main.lua` to use the new `show_text` function.
- This change improves code reusability and readability by reducing code duplication.
  • Loading branch information
jellydn committed Mar 18, 2024
1 parent 00d76db commit 4408df9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 66 deletions.
78 changes: 12 additions & 66 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -466,48 +466,20 @@ function M.setup()

-- Show all global variables
utils.create_cmd('HurlManageVariable', function()
-- Check if there are any global variables
-- Prepare the lines to display in the popup
local lines = {}
if not _HURL_GLOBAL_CONFIG.global_vars or vim.tbl_isempty(_HURL_GLOBAL_CONFIG.global_vars) then
utils.log_info('hurl: no global variables set')
utils.notify('hurl: no global variables set', vim.log.levels.INFO)
return
end

-- Prepare the lines to display in the popup
local lines = { 'Hurl.nvim Global Variables:' }
for var_name, var_value in pairs(_HURL_GLOBAL_CONFIG.global_vars) do
table.insert(lines, var_name .. ' = ' .. var_value)
end

-- Calculate the width of the popup
local width = 0
for _, line in ipairs(lines) do
width = math.max(width, #line)
table.insert(lines, 'No global variables set. Please use :HurlSetVariable to set one.')
else
for var_name, var_value in pairs(_HURL_GLOBAL_CONFIG.global_vars) do
table.insert(lines, var_name .. ' = ' .. var_value)
end
end

-- Create a popup with the global variables
local height = #lines
local opts = {
relative = 'editor',
width = width + 4,
height = height + 2,
row = (vim.o.lines - height) / 2 - 1,
col = (vim.o.columns - width) / 2,
style = 'minimal',
border = 'rounded',
}
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_open_win(bufnr, true, opts)

-- Bind 'q' to close the window
vim.api.nvim_buf_set_keymap(
bufnr,
'n',
'q',
'<cmd>close<CR>',
{ noremap = true, silent = true }
)
local popup = require('hurl.popup')
popup.show_text('Hurl.nvim - Global variables', lines)
end, {
nargs = '*',
range = true,
Expand All @@ -517,35 +489,9 @@ function M.setup()
utils.create_cmd('HurlDebugInfo', function()
-- Get the log file path
local log_file_path = utils.get_log_file_path()

-- Create a popup with the log file path
local lines = { 'Hurl.nvim Info:', 'Log file path: ' .. log_file_path }
local width = 0
for _, line in ipairs(lines) do
width = math.max(width, #line)
end
local height = #lines
local opts = {
relative = 'editor',
width = width + 4,
height = height + 2,
row = (vim.o.lines - height) / 2 - 1,
col = (vim.o.columns - width) / 2,
style = 'minimal',
border = 'rounded',
}
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.api.nvim_open_win(bufnr, true, opts)

-- Bind 'q' to close the window
vim.api.nvim_buf_set_keymap(
bufnr,
'n',
'q',
'<cmd>close<CR>',
{ noremap = true, silent = true }
)
local lines = { 'Log file path: ' .. log_file_path }
local popup = require('hurl.popup')
popup.show_text('Hurl.nvim - Debug info', lines)
end, {
nargs = '*',
range = true,
Expand Down
40 changes: 40 additions & 0 deletions lua/hurl/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,44 @@ M.clear = function()
vim.api.nvim_buf_set_lines(popups.bottom.bufnr, 0, -1, false, { 'Processing...' })
end

M.show_text = function(title, lines)
-- Create a new popup
local text_popup = Popup({
enter = true,
focusable = true,
border = {
style = 'rounded',
text = {
top = title,
top_align = 'center',
bottom = 'Press `q` to close',
bottom_align = 'left',
},
},
position = '50%',
size = {
width = '50%',
height = '50%',
},
})

text_popup:on('BufLeave', function()
text_popup:unmount()
end, { once = true })

vim.api.nvim_buf_set_lines(text_popup.bufnr, 0, 1, false, lines)

local function quit()
vim.cmd('q')
text_popup:unmount()
end

-- Map q to quit
text_popup:map('n', 'q', function()
quit()
end)

text_popup:mount()
end

return M

0 comments on commit 4408df9

Please sign in to comment.