From edab526dd0df817ec832dc51c6b665301acb0b86 Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Thu, 30 May 2024 01:55:11 +0200 Subject: [PATCH] feat: introduce new Sloth command with list subcommand Only have a "list" subcommand for now. The list can be filtered with "all", "loaded", "notloaded" --- README.md | 23 +++++++++++++++ lua/sloth-flake/init.lua | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/README.md b/README.md index 2a3d0cb..e16ceae 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. diff --git a/lua/sloth-flake/init.lua b/lua/sloth-flake/init.lua index f22a5ee..f25540f 100644 --- a/lua/sloth-flake/init.lua +++ b/lua/sloth-flake/init.lua @@ -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