Skip to content

Instantly share code, notes, and snippets.

@kyleawayan
Last active September 5, 2024 00:51
Show Gist options
  • Save kyleawayan/1fcc68cc06d9d237914bdc350962c822 to your computer and use it in GitHub Desktop.
Save kyleawayan/1fcc68cc06d9d237914bdc350962c822 to your computer and use it in GitHub Desktop.
Pet on your neovim lualine. Like the pets.nvim plugin, however it uses image.nvim instead (and also requires lualine.nvim). Requires downloading an animation from https://github.com/giusgad/pets.nvim/tree/8a443e2eae804e5abe21b5d00ceaef4041f882e4/media
--[[
How to use:
1. Download this file to your local machine.
2. Source this file in Neovim: `:luafile filename_here.lua`
3. Start the animation using the `:StartDogAnimation` command.
4. Stop the animation using the `:StopDogAnimation` command.
--]]
local api = require("image")
local uv = vim.loop
local fn = vim.fn
-- Global variable to hold the timer reference
local animation_timer = nil
-- Helper function to create a sequence of image file paths
local function get_image_sequence(directory, count)
local images = {}
for i = 0, count do
table.insert(images, string.format("%s/%d.png", directory, i))
end
return images
end
-- Function to get the position of the 'X' section in lualine
local function get_lualine_x_position()
-- Get the total width of the Neovim window
local win_width = fn.winwidth(0)
-- Assuming lualine 'X' section is aligned to the right, calculate its position
-- This assumes 'X' starts at 2/3 of the window width, adjust as needed
local x_position = math.floor(win_width * 2 / 3)
-- Position the image 1 row above the status line
local y_position = fn.winheight(0) - 2 -- One row above the lualine status line
return x_position, y_position
end
-- Function to display the image sequence
local function play_image_sequence(directory, frame_count, delay)
local images = get_image_sequence(directory, frame_count)
local current_frame = 1
local previous_image = nil
-- TODO: Dynamically update when the window is resized
local x_pos, y_pos = get_lualine_x_position()
local function show_next_image()
if current_frame > frame_count then
current_frame = 1
end
local image_path = images[current_frame]
vim.schedule(function()
local image = api.from_file(image_path, {
id = "sequence_image_" .. current_frame,
inline = false,
x = x_pos,
y = y_pos,
width = 100,
height = 100,
})
image:render()
-- Clear the previous image after rendering the new one
if previous_image then
previous_image:clear()
end
previous_image = image
end)
current_frame = current_frame + 1
animation_timer = uv.new_timer()
uv.timer_start(animation_timer, delay, 0, show_next_image)
end
show_next_image()
end
-- Function to stop the animation
local function stop_image_sequence()
if animation_timer then
uv.timer_stop(animation_timer)
uv.close(animation_timer)
animation_timer = nil
print("Animation stopped.")
-- TODO: Clear the image
end
end
-- Create Neovim commands to start and stop the animation
vim.api.nvim_create_user_command("StartDogAnimation", function()
-- Download an animation from https://github.com/giusgad/pets.nvim/tree/8a443e2eae804e5abe21b5d00ceaef4041f882e4/media
play_image_sequence("/Users/kyle.awayan/Desktop/dog/dog/brown/idle", 6, 100)
end, {})
vim.api.nvim_create_user_command("StopDogAnimation", function()
stop_image_sequence()
end, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment