diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-10 11:38:56 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-10 16:05:59 +0100 |
| commit | 71fbb46029a52e840eac65a23027ae5f1a3ef7d8 (patch) | |
| tree | 2f9b00301bd146661f2c9bfe69a2bd443f7b66e7 /lua | |
| parent | docs: added some basic comments to explain functions (diff) | |
feat(pets): ability to remove pets
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets.lua | 16 | ||||
| -rw-r--r-- | lua/pets/animations.lua | 9 | ||||
| -rw-r--r-- | lua/pets/commands.lua | 12 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 6 |
4 files changed, 32 insertions, 11 deletions
diff --git a/lua/pets.lua b/lua/pets.lua index 6d984a0..41e830b 100644 --- a/lua/pets.lua +++ b/lua/pets.lua @@ -7,7 +7,7 @@ M.options = { offset_cols = 0, } -M.pets = {} +M.stack = { pets = {}, popups = {} } function M.setup(options) options = options or {} @@ -28,7 +28,19 @@ function M.create_pet(name, type, style) local popup = require("pets.popup").popup popup:mount() pet:animate(popup.bufnr) - table.insert(M.pets, pet) + table.insert(M.stack.pets, pet) + table.insert(M.stack.popups, popup) +end + +-- function M.kill_pet(name) end + +function M.kill_all() + for _, pet in pairs(M.stack.pets) do + pet:kill() + end + for _, popup in pairs(M.stack.popups) do + popup:unmount() + end end return M diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index bb0fb5a..8e10c31 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -56,4 +56,13 @@ function M.Animation:next_frame() self.current_image = image end +function M.Animation:stop() + if self.current_image then + self.current_image:delete(0, { free = false }) + end + if self.timer then + self.timer:stop() + end +end + return M diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index 80709fd..8c521cd 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -1,13 +1,9 @@ local pets = require("pets") -vim.api.nvim_create_user_command("Pets", function() - pets.show() -end, {}) -- use nargs = 1 to accept arguments - vim.api.nvim_create_user_command("PetsNew", function(input) - pets.create_pet("Giulio", "cat", "brown") -- TODO: use input.args as name -end, { nargs = 1 }) -- use nargs = 1 to accept arguments + pets.create_pet(input.args, "cat", "brown") +end, { nargs = 1 }) -vim.api.nvim_create_user_command("PetsCloseAll", function() - pets.closeAll() +vim.api.nvim_create_user_command("PetsKillAll", function() + pets.kill_all() end, {}) diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index f1f04c3..0b191c9 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -11,7 +11,7 @@ function M.Pet.new(name, type, style) instance.name = name instance.type = type instance.style = style - local wd = "/mnt/shared/coding/lua/plugins/pets.nvim/media/" -- TODO: adapt to use the correct path when plugin is installed + local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/" instance.sourcedir = wd .. type .. "/" .. style .. "/" instance.animation = require("pets.animations").Animation.new(instance.sourcedir, type, style) return instance @@ -21,4 +21,8 @@ function M.Pet:animate(bufnr) self.animation:start(bufnr) end +function M.Pet:kill() + self.animation:stop() +end + return M |