Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 ability to add more than 1 beastie #8

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ https://github.com/user-attachments/assets/ca2db35e-6ae5-45f2-87ef-9c4c9458ee78
"Abizrh/beastie.nvim",
lazy = false, -- needed so the your beastie can start at launch
opts = {
frames = {
"🐱",
"😺",
"😸",
"😹",
"😼",
"😽"
beasties = {
{
name = "cat",
frames = { "🐱", "😺", "😸", "😹", "😼", "😽" }
},
{
name = "dog",
frames = { "🐶", "🐕", "🦮", "🐕" }
},
{
name = "bird",
frames = { "🐦", "🐤", "🐧", "🦜" }
}
},
start_at_launch = false,
animation_speed = 400, -- ms
start_at_launch = true,
animation_speed = 200, -- ms
active_beastie = 1, --
},
},
```
Expand All @@ -33,9 +40,19 @@ https://github.com/user-attachments/assets/ca2db35e-6ae5-45f2-87ef-9c4c9458ee78

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| frames | table | { '🐻', '🐼', '🐨', '🦊', '🐶', '🐱' } | Sprites emojis |
| beastie | table | { {name = 'cat', frames = {'🐱', '😺', '😸', '😹', '😼', '😽'}} } | The list of beasties to choose from |
| animation_speed | number | 200 | The speed of the animation |
| start_at_launch | boolean | false | Start the animation at launch |
| active_beastie | number | 1 | The index of the active beastie |


## CMD

| Command | Description |
| --------------------- | ------------------------------------------------------- |
| `:BeastieStart` | Start your Beastie |
| `:BeastieStop` | Stop your Beastie |
| `:BeastieSwitch [index]` | Switch to a specific beastie [given index] |

## License

Expand Down
53 changes: 42 additions & 11 deletions lua/beastie/beastie.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
local ui = require('beastie.ui')
local log = require('beastie.log')
local uv = require('beastie.uv')
local beastie = {}

local beastie = {}
local config, frame_idx, buf, win, window_opts, timer

---@class BeastieSet
---@field frames string[]
---@field name string

---@class BeastieOpts
---@field beasties BeastieSet[]
---@field start_at_launch boolean
---@field animation_speed number
---@field active_beastie number

local function change_beastie_position()
if not buf or not win then
buf, win, window_opts = ui.create_buffer_ui(config.frames[frame_idx])
local active_set = config.beasties[config.active_beastie]
buf, win, window_opts = ui.create_buffer_ui(active_set.frames[frame_idx])
end

local direction = math.random(4) -- 1: left, 2: right, 3: up, 4: down
Expand All @@ -23,10 +34,10 @@ local function change_beastie_position()
window_opts.row = math.min(vim.o.lines - 2, window_opts.row + step)
end

-- select random frame
frame_idx = math.random(#config.frames)

ui.update_beastie(buf, win, window_opts, config.frames[frame_idx])
-- select random frame from active set
local active_set = config.beasties[config.active_beastie]
frame_idx = math.random(#active_set.frames)
ui.update_beastie(buf, win, window_opts, active_set.frames[frame_idx])
end

local function start_beastie()
Expand All @@ -50,22 +61,42 @@ end
---@param opts BeastieOpts
local function initialize(opts)
config = vim.tbl_deep_extend("force", {
frames = { "🐱", "😺", "😸", "😹", "😼", "😽" },
beasties = {
{
name = "cat",
frames = { "🐱", "😺", "😸", "😹", "😼", "😽" }
},
},
start_at_launch = false,
animation_speed = 200,
active_beastie = 1
}, opts or {})
frame_idx = 1
end

local function switch_beastie(index)
if index > 0 and index <= #config.beasties then
config.active_beastie = index
frame_idx = 1
if buf and win then
-- Update the existing beastie if it's already running
local active_set = config.beasties[config.active_beastie]
ui.update_beastie(buf, win, window_opts, active_set.frames[frame_idx])
end
log.info("Switched to beastie set: " .. config.beasties[index].name)
else
log.error("Invalid beastie index")
end
end

local function register_cmds()
vim.api.nvim_create_user_command('BeastieStart', start_beastie, {})
vim.api.nvim_create_user_command('BeastieStop', stop_beastie, {})
vim.api.nvim_create_user_command('BeastieSwitch', function(opts)
switch_beastie(tonumber(opts.args))
end, { nargs = 1 })
end

---@class BeastieOpts
---@field frames string[]
---@field start_at_launch boolean
---@field animation_speed number
function beastie.setup(opts)
initialize(opts)
register_cmds()
Expand Down