diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-15 12:38:21 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-15 12:38:21 +0100 |
| commit | acc753e440fb6ce54342c1f4e4fc980c5e008448 (patch) | |
| tree | 89d1737d19b1db8d4082fa5fdbff1792150cefa0 | |
| parent | chore: renamed grey to gray (diff) | |
feat(commands): added hide and pause commands
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | TODO.md | 18 | ||||
| -rw-r--r-- | lua/pets.lua | 15 | ||||
| -rw-r--r-- | lua/pets/animations.lua | 34 | ||||
| -rw-r--r-- | lua/pets/commands.lua | 8 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 30 |
6 files changed, 83 insertions, 28 deletions
@@ -68,6 +68,8 @@ These are all the available commands: - `PetsList`: prints the names of all the pets that are currently alive - `PetsKill {name}`: kills the pet with given name, which will immediately blink out of existence. Forever. - `PetsKillAll`: kills all the pets, poor creatures. +- `PetsPauseToggle`: pause/resume animations for all pets, leaving them on screen as cute little statues +- `PetsHideToggle`: pause the animation for all pets and hide them / show all the pets again and resume animations ## ⚠️ Limitations @@ -77,6 +79,10 @@ and shares all of its limitations. Here are the most significant ones: To be more precise the only terminal I tested where I was able to have the protocol working correctly is kitty itself. - Doesn't currently work inside tmux +## ✔️ Roadmap +- [ ] Handle window resizes +- [ ] add more pets + ## 👏 Credits All the beautiful cat assets were designed by [SeethingSwarm](https://seethingswarm.itch.io/catset) diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 93bf2ce..0000000 --- a/TODO.md +++ /dev/null @@ -1,18 +0,0 @@ -# To do -## Features -- [ ] Ability to name pets - - [ ] Ability to spawn multiple pets -- [ ] Ability to play/pause the animation -- [ ] Ability to temporairly hide the pets - -## Technical - -## Usability -- [ ] Fix `:h` page formatting - -# Info -- Using `gifsicle` to extract pngs from vscode-pets' gifs - -## Limitations -- The plugin doesn't work inside tmux!! -- The plugin only works in terminals that support the kitty image protocol diff --git a/lua/pets.lua b/lua/pets.lua index ce9c61c..d82945f 100644 --- a/lua/pets.lua +++ b/lua/pets.lua @@ -54,9 +54,24 @@ function M.kill_all() end function M.list() + if #M.pets == 0 then + print("You have no pets :(") + end for pet in pairs(M.pets) do print(pet) end end +function M.toggle_pause() + for _, pet in pairs(M.pets) do + pet:toggle_pause() + end +end + +function M.toggle_hide() + for _, pet in pairs(M.pets) do + pet:toggle_hide() + end +end + return M diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index fad62e8..3965b7a 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -51,16 +51,10 @@ function M.Animation.new(sourcedir, type, style, popup, user_opts) return instance end --- @param bufnr buffer number of the popup --- @function start the animation -function M.Animation:start(bufnr) - if self.timer ~= nil then -- reset timer - self.timer = nil +function M.Animation:start_timer() + if self.timer == nil then + self.timer = vim.loop.new_timer() end - self.timer = vim.loop.new_timer() - self.bufnr = bufnr - self.current_action = "idle" - self.timer:start(0, 1000 / (self.speed_multiplier * 8), function() vim.schedule(function() M.Animation.next_frame(self) @@ -68,6 +62,26 @@ function M.Animation:start(bufnr) end) end +function M.Animation:stop_timer() + if self.timer == nil then + return + end + self.timer:stop() + self.timer:close() + self.timer = nil +end + +-- @param bufnr buffer number of the popup +-- @function start the animation +function M.Animation:start() + if self.timer ~= nil then -- reset timer + self.timer = nil + end + self.bufnr = self.popup.bufnr + self.current_action = self.current_action or "idle" + M.Animation.start_timer(self) +end + -- @function called on every tick from the timer, go to the next frame function M.Animation:next_frame() self.frame_counter = self.frame_counter + 1 @@ -146,6 +160,8 @@ function M.Animation:stop() end if self.timer then self.timer:stop() + self.timer:close() + self.timer = nil end end diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index bae97aa..b2b9888 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -44,3 +44,11 @@ end, { vim.api.nvim_create_user_command("PetsList", function() pets.list() end, {}) + +vim.api.nvim_create_user_command("PetsPauseToggle", function() + pets.toggle_pause() +end, {}) + +vim.api.nvim_create_user_command("PetsHideToggle", function() + pets.toggle_hide() +end, {}) diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index e13d9e0..2442eec 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -43,7 +43,7 @@ end -- start the animation of the pet function M.Pet:animate() self.popup:mount() - self.animation:start(self.popup.bufnr) + self.animation:start() end -- delete the pet :( @@ -56,4 +56,32 @@ function M.Pet:kill() end end +function M.Pet:toggle_pause() + if not self.paused then + self.animation:stop_timer() + self.paused = true + else + if self.animation.current_image then + self.animation.current_image:delete(0, { free = false }) + end + self.animation:start_timer() + self.paused = false + end +end + +function M.Pet:toggle_hide() + if not self.paused then + self.animation:stop_timer() + if self.animation.current_image then + self.animation.current_image:delete(0, { free = false }) + end + self.popup:unmount() + self.paused = true + else + self.popup:mount() + self.animation:start() + self.paused = false + end +end + return M |