aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorGiuseppe Gadola <giusgadola@gmail.com>2023-02-14 18:54:04 +0100
committerGiuseppe Gadola <giusgadola@gmail.com>2023-02-14 19:17:20 +0100
commitca5a509f36a7a3a938dfc48b48cb662a79df7a47 (patch)
tree93616aea113d922eec1b7d63459874bced88acb5 /lua
parentchore(actions): only update docs when readme changes (diff)
refactor(commands): validation and autocompletion
Diffstat (limited to 'lua')
-rw-r--r--lua/pets/commands.lua52
-rw-r--r--lua/pets/utils.lua51
2 files changed, 59 insertions, 44 deletions
diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua
index 975ec9b..bae97aa 100644
--- a/lua/pets/commands.lua
+++ b/lua/pets/commands.lua
@@ -1,64 +1,34 @@
local pets = require("pets")
-local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/"
-local available_pets = {}
local utils = require("pets.utils")
-for _, pet in pairs(utils.listdir(wd)) do
- local styles = utils.listdir(wd .. pet)
- available_pets[pet] = styles
-end
vim.api.nvim_create_user_command("PetsNew", function(input)
local pet, style = pets.options.default_pet, pets.options.default_style
-- validate the pets and style options
- if available_pets[pet] == nil then
- utils.warning('The pet "' .. pet .. '" does not exist')
- return
- end
- if available_pets[pet][style] then
- utils.warning('The style "' .. style .. '" for "' .. pet .. '" does not exist')
+ if not utils.validate_type_style(pet, style) then
return
end
if pets.options.random then
- local styles = available_pets["cat"]
+ local styles = utils.available_pets["cat"]
pet, style = "cat", styles[math.random(#styles)]
end
pets.create_pet(input.args, pet, style)
end, { nargs = 1 })
-local function autocomplete_petsnew(_, cmdline)
- local matches = {}
- local words = vim.split(cmdline, " ", { trimempty = true })
-
- if not vim.endswith(cmdline, " ") then
- table.remove(words, #words)
- end
-
- if #words == 1 then
- for _, v in available_pets do
- table.insert(matches, v)
- end
- elseif #words == 2 then
- local styles = available_pets[words[2]]
- 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)
+ utils.warning("Inccorect number of arguments!\nUSAGE: PetsNewCustom {type} {style} {name}")
return
end
local type, style, name = args[1], args[2], args[3]
+ if not utils.validate_type_style(type, style) then
+ return
+ end
pets.create_pet(name, type, style)
-end, { nargs = 1, complete = autocomplete_petsnew })
+end, { nargs = 1, complete = utils.complete_typestyle })
vim.api.nvim_create_user_command("PetsKillAll", function()
pets.kill_all()
@@ -68,13 +38,7 @@ vim.api.nvim_create_user_command("PetsKill", function(input)
pets.kill_pet(input.args)
end, {
nargs = 1,
- complete = function()
- local matches = {}
- for k, _ in pairs(pets.pets) do
- table.insert(matches, k)
- end
- return matches
- end,
+ complete = utils.complete_name,
})
vim.api.nvim_create_user_command("PetsList", function()
diff --git a/lua/pets/utils.lua b/lua/pets/utils.lua
index 0bf6e9b..90f933d 100644
--- a/lua/pets/utils.lua
+++ b/lua/pets/utils.lua
@@ -1,4 +1,5 @@
local M = {}
+M.available_pets = {}
-- list all files in a directory
-- source: https://stackoverflow.com/a/11130774
@@ -24,6 +25,56 @@ function M.listdir(directory, suppress)
return t
end
+local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/"
+for _, pet in pairs(M.listdir(wd)) do
+ local styles = M.listdir(wd .. pet)
+ M.available_pets[pet] = styles
+end
+
+function M.validate_type_style(pet, style)
+ if M.available_pets[pet] == nil then
+ M.warning('The pet "' .. pet .. '" does not exist')
+ return false
+ end
+ if not vim.tbl_contains(M.available_pets[pet], style) then
+ M.warning('The style "' .. style .. '" for "' .. pet .. '" does not exist')
+ return false
+ end
+ return true
+end
+
+function M.complete_typestyle(_, cmdline)
+ local matches = {}
+ local words = vim.split(cmdline, " ", { trimempty = true })
+
+ if not vim.endswith(cmdline, " ") then
+ table.remove(words, #words)
+ end
+
+ if #words == 1 then
+ for k, _ in pairs(M.available_pets) do
+ table.insert(matches, k)
+ end
+ elseif #words == 2 then
+ local styles = M.available_pets[words[2]]
+ if styles == nil then
+ return {}
+ end
+ for _, v in pairs(styles) do
+ table.insert(matches, v)
+ end
+ end
+ return matches
+end
+
+function M.complete_name()
+ local matches = {}
+ for k, _ in pairs(require("pets").pets) do
+ table.insert(matches, k)
+ end
+ return matches
+end
+
function M.warning(msg)
vim.notify(msg, vim.log.levels.WARN)
end