From c0ca115d7fa27f6975fadb1622ec0c5b37119ae1 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Sat, 1 Feb 2025 14:13:03 -0800 Subject: [PATCH] feat: migrate chameleon.nvim script --- fnl/scripts/chameleon.fnl | 66 ++++++++++++++++++ lua/scripts/chameleon.lua | 141 ++++++++++++++++---------------------- 2 files changed, 126 insertions(+), 81 deletions(-) create mode 100644 fnl/scripts/chameleon.fnl diff --git a/fnl/scripts/chameleon.fnl b/fnl/scripts/chameleon.fnl new file mode 100644 index 0000000..f2cf80a --- /dev/null +++ b/fnl/scripts/chameleon.fnl @@ -0,0 +1,66 @@ +;; MIT License +;; Copyright (c) 2023 Shaun Mathew +;; +;; Transformed to Fennel by antifennel + +(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 diff --git a/lua/scripts/chameleon.lua b/lua/scripts/chameleon.lua index c3516b0..4b18ac1 100644 --- a/lua/scripts/chameleon.lua +++ b/lua/scripts/chameleon.lua @@ -1,95 +1,74 @@ --- MIT License --- Copyright (c) 2023 Shaun --- - +-- [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 - if string.match(result, "^background") then - local color = vim.split(result, "%s+")[2] - M.original_color = color - break - end +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 + else 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, - }) + end + return nil + end + return ___fn___.jobstart({"kitty", "@", "get-colors"}, {on_stderr = _1_, on_stdout = _3_}) else - fn.system(command) + return nil end end - -local setup_autocmds = function() +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 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, - group = autogroup("BackgroundRestore", { clear = true }), - }) + local bg_change = autogroup("BackgroundChange", {clear = true}) + local function _9_() + local color = string.format("#%06X", vim.api.nvim_get_hl(0, {name = "Normal"}).bg) + 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) + 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 - M.setup = function() get_kitty_background() - setup_autocmds() + return setup_autocmds() end - return M