Skip to content

Commit

Permalink
feat(tpipeline): implement the throttle mechanism for FocusGained e…
Browse files Browse the repository at this point in the history
…vent
  • Loading branch information
WilliamHsieh committed Nov 8, 2024
1 parent e8b3be5 commit ce853ef
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions config/nvim/lua/plugins/tpipeline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
})

Expand Down

0 comments on commit ce853ef

Please sign in to comment.