feat: introduce new Sloth command with list subcommand

Only have a "list" subcommand for now.
The list can be filtered with "all", "loaded", "notloaded"
main
LeMarsu 2024-05-30 01:55:11 +02:00
parent 3864d3b9b9
commit edab526dd0
2 changed files with 84 additions and 0 deletions

View File

@ -16,6 +16,8 @@ A [neovim] plugin and configuration management plugin, highly inspired by [lazy]
- [neovim (lua)](#neovim-lua)
- [Using default `init.lua`](#using-default-initlua)
- [Using your own `init.lua`](#using-your-own-initlua)
- [`:Sloth` command](#sloth-command)
- [`list` subcommand](#list-subcommand)
- [API](#api)
<!-- TOC -->
@ -200,6 +202,27 @@ sloth_flake.setup {
-- From here, your plugins are loaded and their optional `config` function called
```
#### `:Sloth` command
`sloth-flake` give a `Sloth` command that you can call to gather some
informations about your plugins.
```vim
Sloth [command] [args...]
```
If no arguments are given, the `Sloth` command will call the `list` subcommand.
##### `list` subcommand
```vim
Sloth list [filter]
```
- `filter`: filter the list of plugins.
- `"all"`: list all declared plugins. Same as if no filter is given.
- `"loaded"`: list only loaded plugins.
- `"notloaded"`: list only not loaded plugins.
#### API
The lua API is not really defined yet. This documentation will be completed then.

View File

@ -182,6 +182,65 @@ function unshim_plugin(name)
end
end
function name_compare(a, b)
if a < b then
return -1
elseif a > b then
return 1
else
return 0
end
end
local function vim_error(...)
vim.api.nvim_err_writeln(string.format(...))
end
local commands = {
list = function(args)
local filter = args[1] or "all"
local deps = vim.iter(M.dep_names())
if filter == "all" then
-- Nothing to do
elseif filter == "loaded" then
deps = deps:filter(function (dep)
return M.is_loaded(dep)
end)
elseif filter == "notloaded" then
deps = deps:filter(function (dep)
return not M.is_loaded(dep)
end)
else
vim_error([[No Sloth list filter "%s".]], cmd)
vim_error("Filters are: all, loaded, notloaded")
return
end
deps = deps:totable()
table.sort(deps, dep_name_compare)
for _, dep in ipairs(deps) do
print(string.format("- %s", dep))
end
end,
}
function sloth_cmd(param)
local args = param.fargs
local cmd = args[1] or "list";
table.remove(args, 1)
local fn = commands[cmd]
if fn then
fn(args)
else
vim.api.nvim_err_writeln(string.format([[No Sloth subcommand "%s"]], cmd))
end
end
function register_command()
vim.api.nvim_create_user_command('Sloth', sloth_cmd, {
nargs = '*',
})
end
function M.setup(config)
if priv.setup_called then
return
@ -199,6 +258,8 @@ function M.setup(config)
for _, dep in ipairs(lazy_deps) do
shim_plugin(dep)
end
register_command()
end
return M