Skip to content

Instantly share code, notes, and snippets.

@dpmcnevin
Created October 12, 2014 01:57
Show Gist options
  • Save dpmcnevin/48c0cf022fa0ca610e12 to your computer and use it in GitHub Desktop.
Save dpmcnevin/48c0cf022fa0ca610e12 to your computer and use it in GitHub Desktop.
Set iTerm2 Background Color per Git Repo (zsh)
set_color() {
local HEX_FG=$1
local HEX_BG=$2
local OPACITY=$3
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
}
## Set background color for specific directories
## Uses git config to set the color
## To set the color (fg, bg, transparency):
## git config --global core.bgcolor "ffffff 000000 0.1"
## git config core.bgcolor "ffffff 000000 0.1"
function chpwd() {
BGCOLOR=$(git config --get core.bgcolor)
if [[ -n $BGCOLOR && ($CURRENTBG != $BGCOLOR) ]]
then eval set_color $BGCOLOR; CURRENTBG=$BGCOLOR
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment