Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: loading animation provider #60

Merged
merged 2 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
47 changes: 47 additions & 0 deletions lua/package-info/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lua/package-info/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ M.install = function()
core.install()
end

M.get_status = function()
return config.loading.fetch()
end

return M
12 changes: 12 additions & 0 deletions lua/package-info/modules/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,16 @@ M.show = function()
return
end

config.loading.start("Fetching latest versions")

local dependencies = M.__get_dependencies()

M.__get_outdated_dependencies(function(outdated_dependencies_json)
M.__set_virtual_text(dependencies.dev, outdated_dependencies_json)
M.__set_virtual_text(dependencies.prod, outdated_dependencies_json)

config.state.displayed = true
config.loading.stop()
end)
end

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Binary file added media/loading.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.