Skip to content

Commit

Permalink
fix bug for nvim 0.9 and add cava
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Apr 9, 2023
1 parent 4ba4116 commit 20d628f
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 616 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ require('wlsample.wind')
### [luffy](./lua/wlsample/airline_luffy.lua)
![luffy animation](https://github.com/windwp/windline.nvim/wiki/screenshot/airline_luffy.gif)

### [cava]
![cava animation](https://github.com/windwp/windline.nvim/wiki/screenshot/cava.gif)
```lua
-- only support linux it need install cava
lua require("windline.components.cava").toggle()
```

```lua
require('wlsample.airline_luffy')
```
Expand Down Expand Up @@ -390,9 +397,3 @@ A command to benchmark current status line by rendering it 10.000 time.

## Document
[wiki](https://github.com/windwp/windline.nvim/wiki/)

## Floating window statusline (Deprecated)
__If you are using nvim > 0.7 you should use set laststatus=3 to enable global
statusline__

[MoreInfo](https://github.com/windwp/windline.nvim/wiki/Floating-statusline)
112 changes: 112 additions & 0 deletions lua/windline/components/cava.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---@diagnostic disable: need-check-nil
local windline = require('windline')
local cava_text = "OK"
local uv = vim.loop
if _G._cava_stop then
_G._cava_stop()
end

vim.api.nvim_create_autocmd("VimLeave", {
pattern = "*",
callback = function()
vim.fn.system({ "pkill", '-9', "cava" })
end
})
local create_cava_colors = function(colors)
local HSL = require('wlanimation.utils')
local d_colors = {
"green", "blue", "yellow", "magenta", "red", "cyan"
}
local cava_colors = HSL.rgb_to_hsl(colors[d_colors[math.random(#d_colors)]]):tints(10, 8)
for i = 1, 8, 1 do
colors["cava" .. i] = cava_colors[i]:to_rgb()
end
return colors
end

local bars = { "", "", "", "", "", "", "", "" }
local cava_comp = {
name = "cava",
hl_colors = {
cava1 = { "cava1", "NormalBg" },
cava2 = { "cava2", "NormalBg" },
cava3 = { "cava3", "NormalBg" },
cava4 = { "cava4", "NormalBg" },
cava5 = { "cava5", "NormalBg" },
cava6 = { "cava6", "NormalBg" },
cava7 = { "cava7", "NormalBg" },
cava8 = { "cava8", "NormalBg" },
},
text = function()
local result = {}
for i = 1, 60, 2 do
local c = tonumber(cava_text:sub(i, i))
if c then
c = c + 1
result[#result + 1] = { bars[c], "cava" .. c }
end
end
-- result[#result+1] = {"%="}
return result
end,
click = function()
vim.notify("change cava colors")
windline.change_colors(create_cava_colors(windline.get_colors()))
end
}

local function run_cava()
local sourced_file = require('plenary.debug_utils').sourced_filepath()
local plugin_directory = vim.fn.fnamemodify(sourced_file, ':h:h:h:h')

vim.fn.system({ "pkill", '-9', "cava" })
local cava_path = vim.fn.expand(plugin_directory .. "/scripts/cava.sh")
local stdin = uv.new_pipe(false)
local stdout = uv.new_pipe(false)
local stderr = uv.new_pipe(false)
local handle = uv.spawn(cava_path,
{ stdio = { stdin, stdout, stderr }, },
function() _G._cava_stop() end
)

uv.read_start(stdout, vim.schedule_wrap(function(_, data)
if data then
cava_text = data
vim.cmd.redrawstatus()
end
end))
_G._cava_stop = function()
stdin:read_stop()
stdin:close()
stdout:read_stop()
stdout:close()
stderr:read_stop()
stderr:close()
handle:close()
_G._cava_stop = nil
end
end

local M = {}
M.is_stop = true

M.toggle = function()
if M.is_stop then
run_cava()
windline.add_component(cava_comp, {
name = "cava",
position = "right",
auto_remove = true,
colors_name = create_cava_colors
})
else
vim.fn.system({ "pkill", '-9', "cava" })
if _G._cava_stop then
_G._cava_stop()
end
windline.remove_component(cava_comp)
end
M.is_stop = not M.is_stop
end
-- M.toggle()
return M
Loading

0 comments on commit 20d628f

Please sign in to comment.