feat: migrate chameleon.nvim script

This commit is contained in:
Youwen Wu 2025-02-01 14:13:03 -08:00
parent fa1b2b49a1
commit c0ca115d7f
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
2 changed files with 126 additions and 81 deletions

66
fnl/scripts/chameleon.fnl Normal file
View file

@ -0,0 +1,66 @@
;; MIT License
;; Copyright (c) 2023 Shaun Mathew
;; <https://github.com/shaun-mathew/Chameleon.nvim>
;; Transformed to Fennel by antifennel <https://fennel-lang.org/see>
(local M {})
(local ___fn___ vim.fn)
(local api vim.api)
(set M.original_color nil)
(fn get-kitty-background []
(when (= M.original_color nil)
(___fn___.jobstart [:kitty "@" :get-colors]
{:on_stderr (fn [_ d _]
(when (> (length d) 1)
(vim.notify "Chameleon.nvim: Error getting background. Make sure kitty remote control is turned on."
vim.log.levels.DEBUG)))
:on_stdout (fn [_ d _]
(each [_ result (ipairs d)]
(when (string.match result :^background)
(local color
(. (vim.split result "%s+") 2))
(set M.original_color color)
(lua :break))))})))
(fn change-background [color sync]
(let [arg (.. "background=\"" color "\"")
command (.. "kitty @ set-colors " arg)]
(if (not sync)
(___fn___.jobstart command
{:on_stderr (fn [_ d _]
(when (> (length d) 1)
(vim.notify "Chameleon.nvim: Error changing background. Make sure kitty remote control is turned on."
vim.log.levels.DEBUG)))})
(___fn___.system command))))
(fn setup-autocmds []
(let [autocmd api.nvim_create_autocmd
autogroup api.nvim_create_augroup
bg-change (autogroup :BackgroundChange {:clear true})]
(autocmd [:ColorScheme :VimResume]
{:callback (fn []
(local color
(string.format "#%06X"
(. (vim.api.nvim_get_hl 0
{:name :Normal})
:bg)))
(change-background color))
:group bg-change
:pattern "*"})
(autocmd :User {:callback (fn []
(local color
(string.format "#%06X"
(. (vim.api.nvim_get_hl 0
{:name :Normal})
:bg)))
(change-background color))
:group bg-change
:pattern :NvChadThemeReload})
(autocmd [:VimLeavePre :VimSuspend]
{:callback (fn []
(when (not= M.original_color nil)
(change-background M.original_color true)))
:group (autogroup :BackgroundRestore {:clear true})})))
(fn M.setup [] (get-kitty-background) (setup-autocmds))
M

View file

@ -1,95 +1,74 @@
-- MIT License -- [nfnl] Compiled from ./fnl/scripts/chameleon.fnl by https://github.com/Olical/nfnl, do not edit.
-- Copyright (c) 2023 Shaun
-- <https://github.com/shaun-mathew/Chameleon.nvim>
local M = {} local M = {}
local fn = vim.fn local ___fn___ = vim.fn
local api = vim.api local api = vim.api
M.original_color = nil M.original_color = nil
local function get_kitty_background()
local get_kitty_background = function() if (M.original_color == nil) then
if M.original_color == nil then local function _1_(_, d, _0)
fn.jobstart({ "kitty", "@", "get-colors" }, { if (#d > 1) then
on_stdout = function(_, d, _) return vim.notify("Chameleon.nvim: Error getting background. Make sure kitty remote control is turned on.", vim.log.levels.DEBUG)
for _, result in ipairs(d) do else
return nil
end
end
local function _3_(_, d, _0)
for _1, result in ipairs(d) do
if string.match(result, "^background") then if string.match(result, "^background") then
local color = vim.split(result, "%s+")[2] local color = vim.split(result, "%s+")[2]
M.original_color = color M.original_color = color
break break
end
end
end,
on_stderr = function(_, d, _)
if #d > 1 then
vim.notify(
"Chameleon.nvim: Error getting background. Make sure kitty remote control is turned on.",
vim.log.levels.DEBUG
)
end
end,
})
end
end
local change_background = function(color, sync)
local arg = 'background="' .. color .. '"'
local command = "kitty @ set-colors " .. arg
if not sync then
fn.jobstart(command, {
on_stderr = function(_, d, _)
if #d > 1 then
vim.notify(
"Chameleon.nvim: Error changing background. Make sure kitty remote control is turned on.",
vim.log.levels.DEBUG
)
end
end,
})
else else
fn.system(command) end
end
return nil
end
return ___fn___.jobstart({"kitty", "@", "get-colors"}, {on_stderr = _1_, on_stdout = _3_})
else
return nil
end end
end end
local function change_background(color, sync)
local setup_autocmds = function() local arg = ("background=\"" .. color .. "\"")
local command = ("kitty @ set-colors " .. arg)
if not sync then
local function _6_(_, d, _0)
if (#d > 1) then
return vim.notify("Chameleon.nvim: Error changing background. Make sure kitty remote control is turned on.", vim.log.levels.DEBUG)
else
return nil
end
end
return ___fn___.jobstart(command, {on_stderr = _6_})
else
return ___fn___.system(command)
end
end
local function setup_autocmds()
local autocmd = api.nvim_create_autocmd local autocmd = api.nvim_create_autocmd
local autogroup = api.nvim_create_augroup local autogroup = api.nvim_create_augroup
local bg_change = autogroup("BackgroundChange", { clear = true }) local bg_change = autogroup("BackgroundChange", {clear = true})
local function _9_()
autocmd({ "ColorScheme", "VimResume" }, { local color = string.format("#%06X", vim.api.nvim_get_hl(0, {name = "Normal"}).bg)
pattern = "*", return change_background(color)
callback = function()
local color = string.format("#%06X", vim.api.nvim_get_hl(0, { name = "Normal" }).bg)
change_background(color)
end,
group = bg_change,
})
autocmd("User", {
pattern = "NvChadThemeReload",
callback = function()
local color = string.format("#%06X", vim.api.nvim_get_hl(0, { name = "Normal" }).bg)
change_background(color)
end,
group = bg_change,
})
autocmd({ "VimLeavePre", "VimSuspend" }, {
callback = function()
if M.original_color ~= nil then
change_background(M.original_color, true)
-- Looks like it was silently fixed in NVIM 0.10. At least, I can't reproduce it anymore,
-- so for now disable it and see if anyone reports it again.
-- https://github.com/neovim/neovim/issues/21856
-- vim.cmd[[sleep 10m]]
end end
end, autocmd({"ColorScheme", "VimResume"}, {callback = _9_, group = bg_change, pattern = "*"})
group = autogroup("BackgroundRestore", { clear = true }), local function _10_()
}) local color = string.format("#%06X", vim.api.nvim_get_hl(0, {name = "Normal"}).bg)
return change_background(color)
end
autocmd("User", {callback = _10_, group = bg_change, pattern = "NvChadThemeReload"})
local function _11_()
if (M.original_color ~= nil) then
return change_background(M.original_color, true)
else
return nil
end
end
return autocmd({"VimLeavePre", "VimSuspend"}, {callback = _11_, group = autogroup("BackgroundRestore", {clear = true})})
end end
M.setup = function() M.setup = function()
get_kitty_background() get_kitty_background()
setup_autocmds() return setup_autocmds()
end end
return M return M