Skip to content

Commit

Permalink
feat(hurl): add spinner to indicate request progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Mar 14, 2024
1 parent 9f773ef commit 0e307f3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lua/hurl/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local utils = require('hurl.utils')
local git = require('hurl.git_utils')
local http = require('hurl.http_utils')
local spinner = require('hurl.spinner')

local M = {}

Expand Down Expand Up @@ -172,6 +173,7 @@ local function execute_hurl_cmd(opts, callback)
end

is_running = true
spinner.show()
head_state = ''
utils.log_info('hurl: running request')
utils.notify('hurl: running request', vim.log.levels.INFO)
Expand Down Expand Up @@ -208,6 +210,7 @@ local function execute_hurl_cmd(opts, callback)
on_exit = function(i, code)
utils.log_info('exit at ' .. i .. ' , code ' .. code)
is_running = false
spinner.hide()
if code ~= 0 then
-- Send error code and response to quickfix and open it
-- It should display the error message
Expand Down
95 changes: 95 additions & 0 deletions lua/hurl/spinner.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- spinner.lua
-- https://github.com/jellydn/spinner.nvim
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.

local M = {}

-- User configuration section
local config = {
-- Show notification when done.
-- Set to false to disable.
show_notification = true,
-- Name of the plugin.
plugin = 'hurl.nvim',
-- Spinner frames.
spinner_frames = {
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
},
}

-- {{{ NO NEED TO CHANGE

local spinner_index = 1
local spinner_timer = nil
local spinner_buf = nil
local spinner_win = nil

--- Show a spinner at the specified position.
---@param position? table
function M.show(position)
-- Default position: the top right corner
local default_position = {
relative = 'editor',
width = 1,
height = 1,
col = vim.o.columns - 1,
row = 0,
}
local options = position or default_position
options.style = 'minimal'

-- Create buffer and window for the spinner
spinner_buf = vim.api.nvim_create_buf(false, true)
spinner_win = vim.api.nvim_open_win(spinner_buf, false, options)

-- Set up timer and update spinner
spinner_timer = vim.loop.new_timer()
spinner_timer:start(
0,
100,
vim.schedule_wrap(function()
vim.api.nvim_buf_set_lines(
spinner_buf,
0,
-1,
false,
{ config.spinner_frames[spinner_index] }
)
spinner_index = spinner_index % #config.spinner_frames + 1
end)
)
end

--- Hide the spinner.
---@param show_msg? boolean
function M.hide(show_msg)
if spinner_timer then
spinner_timer:stop()
spinner_timer:close()
spinner_timer = nil
if spinner_win then
vim.api.nvim_win_close(spinner_win, true)
end
if spinner_buf then
vim.api.nvim_buf_delete(spinner_buf, { force = true })
end

if config.show_notification or show_msg then
vim.notify('Done!', vim.log.levels.INFO, { title = config.plugin })
end
end
end

-- }}}

return M

0 comments on commit 0e307f3

Please sign in to comment.