diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-12 12:26:27 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-12 12:26:27 +0100 |
| commit | f8f7b7570c49dc2486122f66526a5561e790f5fc (patch) | |
| tree | afe8829e129d93e5c8de9043c352b38d27ac8a50 /lua | |
| parent | refactor(commands): PetsNew spawns a pet with the default style and type (diff) | |
feat(commands): command to create pet with custom options and autocompletion
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets/commands.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index 20e3c05..3274aba 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -4,6 +4,39 @@ vim.api.nvim_create_user_command("PetsNew", function(input) pets.create_pet(input.args, pets.options.default_pet, pets.options.default_style) end, { nargs = 1 }) +local function autocomplete(_, cmdline) + local matches = {} + local words = vim.split(cmdline, " ", { trimempty = true }) + local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/" + + if not vim.endswith(cmdline, " ") then + table.remove(words, #words) + end + + if #words == 1 then + for _, v in pairs(require("pets.utils").listdir(wd)) do + table.insert(matches, v) + end + elseif #words == 2 then + local styles = require("pets.utils").listdir(wd .. words[2], true) + for _, v in pairs(styles) do + table.insert(matches, v) + end + end + return matches +end + +vim.api.nvim_create_user_command("PetsNewCustom", function(input) + local args = vim.split(input.args, " ", { trimempty = true }) + if #args ~= 3 then + local msg = "Inccorect number of arguments!\nUSAGE: PetsNewCustom {type} {style} {name}" + vim.notify(msg, vim.log.levels.WARN) + return + end + local type, style, name = args[1], args[2], args[3] + pets.create_pet(name, type, style) +end, { nargs = 1, complete = autocomplete }) + vim.api.nvim_create_user_command("PetsKillAll", function() pets.kill_all() end, {}) |