aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorGiuseppe Gadola <giusgadola@gmail.com>2023-02-07 18:20:11 +0100
committerGiuseppe Gadola <giusgadola@gmail.com>2023-02-10 16:05:59 +0100
commit9c16ec2e3887b90670c28cc845d0b2822bc4f704 (patch)
treed84167f4e0cd073bd87fb393d6bba6a0045e61be /lua
parentstyle: change indentation to 4 spaces (diff)
feat: one pet walks
Diffstat (limited to 'lua')
-rw-r--r--lua/pets.lua78
-rw-r--r--lua/pets/animations.lua61
-rw-r--r--lua/pets/commands.lua4
-rw-r--r--lua/pets/popup.lua28
-rw-r--r--lua/pets/utils.lua13
5 files changed, 133 insertions, 51 deletions
diff --git a/lua/pets.lua b/lua/pets.lua
index 8a0058c..914e68a 100644
--- a/lua/pets.lua
+++ b/lua/pets.lua
@@ -1,48 +1,66 @@
local M = {}
M.stack = {
- popups = {},
- images = {},
+ popups = {},
+ images = {},
+ jobs = {},
+}
+
+M.options = {
+ pet_name = "cat",
+ pet_style = "brown",
+ fps = 8,
+ offset_rows = 0,
+ offset_cols = 0,
}
function M.setup(options)
- options = options or {}
+ options = options or {}
+ M.options = vim.tbl_deep_extend("force", M.options, options)
- -- init hologram
- local ok = pcall(require, "hologram")
- if ok then
- require("hologram").setup({ auto_display = false })
- end
+ -- init hologram
+ local ok = pcall(require, "hologram")
+ if ok then
+ require("hologram").setup({ auto_display = false })
+ end
- require("pets.commands") -- init autocommands
+ require("pets.commands") -- init autocommands
end
+--[[ This function creates the popup for the image to be displayed and
+takes care of calling the utils.ShowPet function.]]
function M.show()
- local popup = require("pets.popup").popup
- local utils = require("pets.utils")
- popup:mount()
+ local popup = require("pets.popup").popup
+ local utils = require("pets.utils")
+ popup:mount()
- -- insert lines to avoid the image being stretched
- vim.api.nvim_buf_set_lines(popup.bufnr, 0, -1, false, { "", "", "", "", "", "", "", "", "", "" })
- popup.buf_options = { -- then set the buffer to be readonly
- modifiable = false,
- readonly = true,
- }
+ popup.buf_options = { -- then set the buffer to be readonly
+ modifiable = false,
+ readonly = true,
+ }
- local image = utils.ShowPet(popup.bufnr)
- table.insert(M.stack.popups, popup)
- table.insert(M.stack.images, image)
+ local image = utils.ShowPet(
+ popup.bufnr,
+ M.options.offset_rows,
+ M.options.offset_cols,
+ M.options.pet_name,
+ M.options.pet_style,
+ M.options.fps
+ )
+ table.insert(M.stack.popups, popup) -- TODO: update for multiple images
+ table.insert(M.stack.images, image)
end
-function M.closeAll()
- for _, image in pairs(M.stack.images) do
- image:delete(0, {
- free = false,
- })
- end
- for _, popup in pairs(M.stack.popups) do
- popup:unmount()
- end
+-- close all the popups and stop displaying any image
+function M.closeAll() -- TODO: update for multiple images
+ for _, image in pairs(M.stack.images) do
+ image:delete(0, {
+ free = false,
+ })
+ 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
new file mode 100644
index 0000000..20e0520
--- /dev/null
+++ b/lua/pets/animations.lua
@@ -0,0 +1,61 @@
+local M = {}
+M.timer = nil
+M.bufnr = nil
+M.frame_counter = 1
+M.frames = {}
+M.current_image = nil
+
+local lines = {}
+local string = ""
+for _ = 0, 150 do
+ string = string .. " "
+end
+for _ = 0, 15 do
+ table.insert(lines, string)
+end
+
+function M.animate(buf, sourcedir, fps)
+ local files = M._listdir(sourcedir .. "8fps/" .. "walk/")
+ for _, file in pairs(files) do
+ table.insert(M.frames, sourcedir .. "8fps/" .. "walk/" .. file)
+ end
+ M.timer = vim.loop.new_timer()
+ M.bufnr = buf
+
+ M.timer:start(100, 1000 / 8, vim.schedule_wrap(M.next_frame))
+end
+
+function M.next_frame()
+ M.frame_counter = M.frame_counter + 1
+ vim.api.nvim_buf_set_lines(M.bufnr, 0, -1, false, lines)
+ if not M.current_image then
+ M.frame_counter = 1
+ else
+ M.current_image:delete(0, { free = false })
+ end
+ if M.frame_counter > #M.frames then
+ M.frame_counter = 1
+ end
+ local image = require("hologram.image"):new(M.frames[M.frame_counter])
+ image:display(1, M.frame_counter, M.bufnr, {})
+ M.current_image = image
+ return true
+end
+
+function M._listdir(directory)
+ local i, t, popen = 0, {}, io.popen
+ local pfile = popen('/bin/ls -a "' .. directory .. '"')
+ if pfile == nil then
+ error("Error getting assets for specified pet")
+ end
+ for filename in pfile:lines() do
+ if filename ~= "." and filename ~= ".." then
+ i = i + 1
+ t[i] = filename
+ end
+ end
+ pfile:close()
+ return t
+end
+
+return M
diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua
index 8124cbc..a211d2d 100644
--- a/lua/pets/commands.lua
+++ b/lua/pets/commands.lua
@@ -1,9 +1,9 @@
local pets = require("pets")
vim.api.nvim_create_user_command("Pets", function()
- pets.show()
+ pets.show()
end, {}) -- use nargs = 1 to accept arguments
vim.api.nvim_create_user_command("PetsCloseAll", function()
- pets.closeAll()
+ pets.closeAll()
end, {})
diff --git a/lua/pets/popup.lua b/lua/pets/popup.lua
index 8fe7ebf..9a1b3a4 100644
--- a/lua/pets/popup.lua
+++ b/lua/pets/popup.lua
@@ -1,19 +1,19 @@
local M = {}
local Popup = require("nui.popup")
+
M.popup = Popup({
- position = {
- row = "100%", -- FIX: set row for different sizes / implement offset
- col = "100%",
- },
- relative = "editor",
- size = {
- width = "25%",
- height = 10,
- },
- focusable = false,
- enter = false,
- win_options = {
- winblend = 10,
- },
+ position = {
+ row = "100%",
+ col = "100%",
+ },
+ size = {
+ width = "25%",
+ height = 10,
+ },
+ focusable = false,
+ enter = false,
+ win_options = {
+ winblend = 100,
+ },
})
return M
diff --git a/lua/pets/utils.lua b/lua/pets/utils.lua
index 4325928..a83b750 100644
--- a/lua/pets/utils.lua
+++ b/lua/pets/utils.lua
@@ -1,11 +1,14 @@
local M = {}
-function M.ShowPet(buf)
- local source = "/mnt/shared/coding/lua/plugins/pets.nvim/media/test/brown_idle-0.png"
- local image = require("hologram.image"):new(source, {})
+function M.ShowPet(buf, offset_rows, offset_cols, pet_name, pet_style, fps)
+ local wd = "/mnt/shared/coding/lua/plugins/pets.nvim/media/" -- TODO: adapt to use the correct path when plugin is installed
+ local sourcedir = wd .. pet_name .. "/" .. pet_style .. "/"
- image:display(5, 0, buf, {}) -- TODO: offset option to show the pet at the desired height
- return image
+ require("pets.animations").animate(buf, sourcedir, fps)
+
+ -- local image = require("hologram.image"):new(sourcedir .. "8fps/walk/0.png", {})
+ -- image:display(1 + offset_rows, 0 + offset_cols, buf, {}) -- TODO: offset option to show the pet at the desired height
+ return -1
end
return M