Guest User

Untitled

a guest
Mar 9th, 2026
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.89 KB | None | 0 0
  1. set -g default_bg (ghostty +show-config | grep ^background | awk -F'=' '{print $2}' | tr -d '[:space:]')
  2.  
  3. # Fire on preexec, because ghostty wrapper fires on fish_prompt, and we want to execute later.
  4. function install_ssh_wrapper --on-event fish_preexec
  5.     if functions -q ssh
  6.         if not functions -q ssh_ghostty
  7.             functions -c ssh ssh_ghostty
  8.         end
  9.     end
  10.     function ssh --wrap=ssh --description "Wrap ghostty ssh wrapper to change terminal background and foreground color when connecting via ssh."
  11.         set -l server ""
  12.         # Parse arguments to find the host (skips flags).
  13.         for arg in $argv
  14.             if not string match -q -- "-*" $arg
  15.                 set server $arg
  16.             end
  17.         end
  18.         # Remove user@ prefix if present.
  19.         set server (string replace -r '.*@' '' $server)
  20.         # Ensure the server is not an empty string (E.g. ssh -V).
  21.         if test -n "$server"
  22.             # Resolve and set the back/foreground colors, depending on the given domain.
  23.             resolve_host_colors $server | read background foreground
  24.             printf "\033]11;%s\007" $background
  25.             printf "\033]10;%s\007" $foreground
  26.         end
  27.         # Use the ghostty wrapped ssh.
  28.         if functions -q ssh_ghostty
  29.             ssh_ghostty $argv
  30.         else
  31.             command ssh $argv
  32.         end
  33.         # Force background reset after session end.
  34.         osc_reset
  35.     end
  36. end
  37.  
  38. # Function returning background color for a server
  39. function resolve_host_colors
  40.     set -l colors (host_colors_mapping $argv[1])
  41.     if test -n "$colors"
  42.         echo $colors
  43.         return
  44.     end
  45.  
  46.     # Lookup for the A record of the alias.
  47.     set -l ip (dig +short $argv[1] A)
  48.     if not test -n "$ip"
  49.         return
  50.     end
  51.     set -l colors (host_colors_mapping $ip)
  52.     if test -n "$colors"
  53.         echo $colors
  54.         return
  55.     end
  56.  
  57.     # Reverse lookup for the main host name using the ip.
  58.     set -l base_domain (dig +short -x $ip | string trim -r -c '.')
  59.     if not test -n "$base_domain"
  60.         return
  61.     end
  62.     set -l colors (host_colors_mapping $base_domain)
  63.     if test -n "$colors"
  64.         echo $colors
  65.         return
  66.     end
  67.  
  68.     # If the ip/base_domain are valid but no mapping was set
  69.     # for the given host use the following defaults.
  70.     echo "#000969" "#ffffff"
  71. end
  72.  
  73. function host_colors_mapping -a host
  74.     set -l mapping \
  75.         'xx.xx.xxx.xxx #000000 #somehex' \
  76.         'some_domain_set_in_hosts #000000 #20c20e' \
  77.         '*.mydomain.com #000969 #ffffff'
  78.  
  79.     for entry in $mapping
  80.         set -l parts (string split ' ' $entry)
  81.         if string match -q $parts[1] $host
  82.             echo $parts[2] $parts[3]
  83.             return
  84.         end
  85.     end
  86. end
  87.  
  88. # Function to reset background and foreground.
  89. function osc_reset
  90.     printf "\033]110\007"
  91.     printf "\033]11;%s\007" $default_bg
  92. end
  93.  
Advertisement
Add Comment
Please, Sign In to add comment