From 74f4a0a4aac7d28e655e046444168490c12d33cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Lam=C3=A9?= Date: Thu, 16 Jan 2025 11:08:27 +0100 Subject: [PATCH] FIX: desactivate if not in tmux --- lua/tmux-sync/init.lua | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lua/tmux-sync/init.lua b/lua/tmux-sync/init.lua index 8c77da5..95826a9 100644 --- a/lua/tmux-sync/init.lua +++ b/lua/tmux-sync/init.lua @@ -1,5 +1,15 @@ local M = {} +-- 🔍 Vérifie si tmux est installé +local function is_tmux_installed() + return vim.fn.executable("tmux") == 1 +end + +-- 🔍 Vérifie si on est dans une session tmux +local function is_in_tmux() + return os.getenv("TMUX") ~= nil +end + -- 🔎 Récupérer la couleur d'un groupe de highlight local function get_hl_color(hl, attr) local ok, hl_data = pcall(vim.api.nvim_get_hl, 0, { name = hl }) @@ -12,6 +22,10 @@ end -- 📡 Synchroniser les couleurs avec Tmux selon le mode M.set_tmux_status = function(mode) + if not (is_tmux_installed() and is_in_tmux()) then + return -- 🔒 Ne fait rien si tmux n'est pas dispo + end + local mode_colors = { n = get_hl_color("MiniStatuslineModeNormal", "bg"), i = get_hl_color("MiniStatuslineModeInsert", "bg"), @@ -25,16 +39,18 @@ M.set_tmux_status = function(mode) local color = mode_colors[mode] or get_hl_color("MiniStatuslineModeNormal", "bg") -- 🛠️ Commande silencieuse pour changer la couleur de Tmux - vim.fn.system({ "tmux", "set-option", "status-bg", color }) + vim.fn.system({ "tmux", "set-option", "-g", "status-bg", color }) end -- 🔄 Détecte les changements de mode -vim.api.nvim_create_autocmd("ModeChanged", { - pattern = "*", - callback = function() - local mode = vim.fn.mode() - M.set_tmux_status(mode) - end, -}) +if is_tmux_installed() and is_in_tmux() then + vim.api.nvim_create_autocmd("ModeChanged", { + pattern = "*", + callback = function() + local mode = vim.fn.mode() + M.set_tmux_status(mode) + end, + }) +end return M