From ce853ef8fa06581116320fe7b095a26dcb7aa409 Mon Sep 17 00:00:00 2001 From: William Hsieh Date: Fri, 8 Nov 2024 11:08:38 +0800 Subject: [PATCH] feat(tpipeline): implement the throttle mechanism for `FocusGained` event --- config/nvim/lua/plugins/tpipeline.lua | 40 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/config/nvim/lua/plugins/tpipeline.lua b/config/nvim/lua/plugins/tpipeline.lua index a9b7ca7..b970a60 100644 --- a/config/nvim/lua/plugins/tpipeline.lua +++ b/config/nvim/lua/plugins/tpipeline.lua @@ -21,10 +21,10 @@ function M.config() local augroup = vim.api.nvim_create_augroup("dotfiles_tpipeline_integration", { clear = true }) -- cache status-bg - local status_bg = nil + local neovim_status_style = nil local function update_status_bg() local bg = require("core.utils").get_hl("Normal").bg - status_bg = string.format("bg=%s,fg=%s", bg, bg) + neovim_status_style = string.format("bg=%s,fg=%s", bg, bg) end vim.schedule(update_status_bg) @@ -45,6 +45,14 @@ function M.config() vim.system { "tmux", "set-option", "-u", opt } end + -- set window name to current directory + local function set_tmux_window_name_to_cwd() + local ok, cwd = pcall(vim.fn.fnamemodify, vim.uv.cwd(), ":t") + if ok and cwd then + vim.system { "tmux", "rename-window", cwd } + end + end + -- update tmux status by neovim statusline vim.api.nvim_create_autocmd({ "DiagnosticChanged", "RecordingEnter" }, { desc = "update tpipeline", @@ -63,16 +71,21 @@ function M.config() -- matched tmux status style and statusline vim.api.nvim_create_autocmd({ "ColorScheme", "FocusGained" }, { callback = function() - vim.schedule(function() - if tmux_status_style ~= status_bg then - set_tmux_status_style(status_bg) - end - end) - vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { once = true, group = vim.api.nvim_create_augroup("dotfiles_force_update_tpipeline", { clear = true }), - command = "call tpipeline#forceupdate()", + callback = function() + -- color + if tmux_status_style ~= neovim_status_style then + set_tmux_status_style(neovim_status_style) + end + + -- window name + set_tmux_window_name_to_cwd() + + -- statusline + vim.fn["tpipeline#forceupdate"]() + end, }) end, group = augroup, @@ -81,7 +94,7 @@ function M.config() -- reset tmux status style vim.api.nvim_create_autocmd("FocusLost", { callback = function() - if tmux_status_style then + if tmux_status_style ~= neovim_status_style then unset_tmux_option("status-style") end end, @@ -90,12 +103,7 @@ function M.config() -- rename tmux window with CWD vim.api.nvim_create_autocmd("DirChanged", { - callback = function() - local ok, cwd = pcall(vim.fn.fnamemodify, vim.uv.cwd(), ":t") - if ok and cwd then - vim.system { "tmux", "rename-window", cwd } - end - end, + callback = set_tmux_window_name_to_cwd, group = augroup, })