diff --git a/README.md b/README.md index 5b2e01c..a8c8072 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,34 @@ require('package-info').setup() } ``` +### ⏰ Loading Hook + +- `package-info` provides a hook to display a loading message + +![Package Info Loading Hook](./media/loading.gif) + +#### Usage + +- It can be used anywhere in `neovim` by invoking `return require('package-info').get_status()` + +```lua +local package = require("package-info") + +-- Galaxyline +section.left[10] = { + PackageInfoStatus = { + provider = function() + return package.get_status() + end, + }, +} +``` + #### 256 Color Terminals -If the vim option `termguicolors` is false, package-info switches to 256 color mode. -In this mode [cterm color numbers](https://jonasjacek.github.io/colors/) are used -instead of truecolor hex codes and the color defaults are: +- If the vim option `termguicolors` is false, package-info switches to 256 color mode. +- In this mode [cterm color numbers](https://jonasjacek.github.io/colors/) are used + instead of truecolor hex codes and the color defaults are: ```lua colors = { diff --git a/lua/package-info/config.lua b/lua/package-info/config.lua index c363d2e..720c35d 100644 --- a/lua/package-info/config.lua +++ b/lua/package-info/config.lua @@ -90,6 +90,53 @@ M.state = { displayed = M.options.autostart or false, } +M.loading = { + animation = { + "⠋", + "⠙", + "⠹", + "⠸", + "⠼", + "⠴", + "⠦", + "⠧", + "⠇", + "⠏", + }, + index = 1, + log = "", + spinner = "", + is_running = false, + fetch = function() + return M.loading.spinner .. " " .. M.loading.log + end, + start = function(message) + M.loading.log = message + M.loading.is_running = true + M.loading.update() + end, + stop = function() + M.loading.is_running = false + M.loading.log = "" + M.loading.spinner = "" + end, + update = function() + if M.loading.is_running then + M.loading.spinner = M.loading.animation[M.loading.index] + + M.loading.index = M.loading.index + 1 + + if M.loading.index == 10 then + M.loading.index = 1 + end + + vim.fn.timer_start(100, function() + M.loading.update() + end) + end + end, +} + --- Clone options and replace empty ones with default ones -- @param user_options - all the options user can provide in the plugin config // See M.options for defaults M.__register_user_options = function(user_options) diff --git a/lua/package-info/init.lua b/lua/package-info/init.lua index 6fd3703..7fb01dc 100644 --- a/lua/package-info/init.lua +++ b/lua/package-info/init.lua @@ -30,4 +30,8 @@ M.install = function() core.install() end +M.get_status = function() + return config.loading.fetch() +end + return M diff --git a/lua/package-info/modules/core.lua b/lua/package-info/modules/core.lua index d5c3283..e009834 100644 --- a/lua/package-info/modules/core.lua +++ b/lua/package-info/modules/core.lua @@ -158,6 +158,8 @@ M.show = function() return end + config.loading.start("Fetching latest versions") + local dependencies = M.__get_dependencies() M.__get_outdated_dependencies(function(outdated_dependencies_json) @@ -165,6 +167,7 @@ M.show = function() M.__set_virtual_text(dependencies.prod, outdated_dependencies_json) config.state.displayed = true + config.loading.stop() end) end @@ -183,12 +186,15 @@ M.delete = function() if not is_valid then logger.error("No package under current line.") else + config.loading.start("Deleting " .. package_name .. " package") + ui.display_prompt({ command = config.get_command.delete(package_name), title = " Delete [" .. package_name .. "] Package ", callback = function() logger.info(package_name .. " deleted successfully") vim.cmd(":e") + config.loading.stop() if config.state.displayed then M.hide() @@ -208,12 +214,15 @@ M.update = function() if not is_valid then logger.error("No package under current line.") else + config.loading.start("Updating " .. package_name .. " package") + ui.display_prompt({ command = config.get_command.update(package_name), title = " Update [" .. package_name .. "] Package ", callback = function() logger.info(package_name .. " updated successfully") vim.cmd(":e") + config.loading.stop() if config.state.displayed then M.hide() @@ -235,11 +244,14 @@ M.install = function() local command = config.get_command.install(dependency_type, dependency_name) + config.loading.start("Updating " .. dependency_name .. " package") + vim.fn.jobstart(command, { on_stdout = function(_, stdout) if table.concat(stdout) == "" then logger.info(dependency_name .. " installed successfully") vim.cmd(":e") + config.loading.stop() if config.state.displayed then M.hide() diff --git a/media/loading.gif b/media/loading.gif new file mode 100644 index 0000000..b05afbd Binary files /dev/null and b/media/loading.gif differ