Skip to content

Instantly share code, notes, and snippets.

@awol
Forked from BugRoger/ssh-background
Last active September 2, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awol/9362b74cd51dc9be5353 to your computer and use it in GitHub Desktop.
Save awol/9362b74cd51dc9be5353 to your computer and use it in GitHub Desktop.
Changes iTerm2's background color based on host configuration
#!/usr/bin/env bash
# Installation:
# 1. Save this script to /some/bin/ssh-background
# 2. chmod 755 /some/bin/ssh-background
# 3. alias ssh='. /some/bin/ssh-background'
# 4. Configure your host colors below.
# 5. Define environment variables ssh_<colour>=( host1 hostmatch1 ... )
# 6. Or just type ssh <colour> hostname
set_color() {
local HEX_FG=$1
local HEX_BG=$2
local OPACITY=$3
if [[ "$TERM_PROGRAM" != "iTerm.app" ]]; then
return
fi
local FG_R=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local FG_G=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local FG_B=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local BG_R=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local BG_G=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local BG_B=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
/usr/bin/osascript <<EOF
tell application "iTerm"
tell current session of current terminal
set foreground color to {$FG_R, $FG_G, $FG_B}
set background color to {$BG_R, $BG_G, $BG_B}
set transparency to "$OPACITY"
end tell
end tell
EOF
}
bgc=( ffffff ffe5e9 e9ffe5 e5e9ff f0f0f0 000000 )
fgc=( 000000 000000 000000 000000 000000 111111 )
trn=( 0 0 0 0 0 0 )
names=( white red green blue grey black )
index=-1
colour_index=-1
for colour in ${names[@]}; do
((colour_index += 1))
if [[ "$1" == "$colour" ]]; then
index=$colour_index
shift 1
break
fi
vname="ssh_${colour}[@]"
for machine_prefix in ${!vname}; do
if [[ "$@" =~ "$machine_prefix" ]]; then
index=$colour_index
break
fi
[[ $index -ge 0 ]] && break
done
done
if [[ index -ge 0 ]]; then
set_color ${fgc[$index]} ${bgc[$index]} ${trn[$index]}
fi
echo ssh $@
\ssh $@
set_color 000000 ffffff 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment