feat: migrate chameleon.nvim script
This commit is contained in:
parent
fa1b2b49a1
commit
c0ca115d7f
2 changed files with 126 additions and 81 deletions
66
fnl/scripts/chameleon.fnl
Normal file
66
fnl/scripts/chameleon.fnl
Normal 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
|
127
lua/scripts/chameleon.lua
generated
127
lua/scripts/chameleon.lua
generated
|
@ -1,95 +1,74 @@
|
|||
-- MIT License
|
||||
-- Copyright (c) 2023 Shaun
|
||||
-- <https://github.com/shaun-mathew/Chameleon.nvim>
|
||||
|
||||
-- [nfnl] Compiled from ./fnl/scripts/chameleon.fnl by https://github.com/Olical/nfnl, do not edit.
|
||||
local M = {}
|
||||
local fn = vim.fn
|
||||
local ___fn___ = vim.fn
|
||||
local api = vim.api
|
||||
M.original_color = nil
|
||||
|
||||
local get_kitty_background = function()
|
||||
if M.original_color == nil then
|
||||
fn.jobstart({ "kitty", "@", "get-colors" }, {
|
||||
on_stdout = function(_, d, _)
|
||||
for _, result in ipairs(d) do
|
||||
local function get_kitty_background()
|
||||
if (M.original_color == nil) then
|
||||
local function _1_(_, d, _0)
|
||||
if (#d > 1) then
|
||||
return vim.notify("Chameleon.nvim: Error getting background. Make sure kitty remote control is turned on.", vim.log.levels.DEBUG)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local function _3_(_, d, _0)
|
||||
for _1, result in ipairs(d) do
|
||||
if string.match(result, "^background") then
|
||||
local color = vim.split(result, "%s+")[2]
|
||||
M.original_color = color
|
||||
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
|
||||
fn.system(command)
|
||||
end
|
||||
end
|
||||
|
||||
local setup_autocmds = function()
|
||||
return nil
|
||||
end
|
||||
return ___fn___.jobstart({"kitty", "@", "get-colors"}, {on_stderr = _1_, on_stdout = _3_})
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local function change_background(color, sync)
|
||||
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 autogroup = api.nvim_create_augroup
|
||||
local bg_change = autogroup("BackgroundChange", {clear = true})
|
||||
|
||||
autocmd({ "ColorScheme", "VimResume" }, {
|
||||
pattern = "*",
|
||||
callback = function()
|
||||
local function _9_()
|
||||
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()
|
||||
return change_background(color)
|
||||
end
|
||||
autocmd({"ColorScheme", "VimResume"}, {callback = _9_, group = bg_change, pattern = "*"})
|
||||
local function _10_()
|
||||
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]]
|
||||
return change_background(color)
|
||||
end
|
||||
end,
|
||||
group = autogroup("BackgroundRestore", { clear = true }),
|
||||
})
|
||||
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
|
||||
|
||||
M.setup = function()
|
||||
get_kitty_background()
|
||||
setup_autocmds()
|
||||
return setup_autocmds()
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Reference in a new issue