Last active
January 8, 2024 09:24
-
-
Save kontza/7ee30f5ed7ae949e90bd03bc0d8bc63d to your computer and use it in GitHub Desktop.
Change WezTerm background color based on SSH host; copy this snippet to your WezTerm config + define your own host_to_color mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Hash char is here to make good editors show the color in situ. | |
local host_to_color = { | |
["main.*p"] = "#660000", | |
["other.*q"] = "#660066", | |
-- key = a pattern to match a hostname | |
-- value = the color to use as pane background for the hostname | |
} | |
local colored_panes = {} | |
wezterm.on("update-status", function(window, pane) | |
local fg = pane:get_foreground_process_info() or {} | |
if fg.name == "ssh" then | |
for ndx = 2, #fg.argv do | |
for k, v in pairs(host_to_color) do | |
if string.find(fg.argv[ndx], k) then | |
if not colored_panes[pane:pane_id()] then | |
-- print(">>> NEW") | |
pane:inject_output(string.format("\x1b]11;%s\x07", string.sub(v, 2))) | |
colored_panes[pane:pane_id()] = fg.pid | |
end | |
end | |
end | |
end | |
else | |
if colored_panes[pane:pane_id()] then | |
local ssh = wezterm.procinfo.get_info_for_pid(colored_panes[pane:pane_id()]) | |
if not ssh then | |
-- print(">>> CLEARED") | |
colored_panes[pane:pane_id()] = nil | |
pane:inject_output("\x1b]104\x07") | |
end | |
end | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment