Skip to content

Instantly share code, notes, and snippets.

@zation
Last active July 19, 2017 02:45
Show Gist options
  • Save zation/1ebba63972ef6ce623238af664972250 to your computer and use it in GitHub Desktop.
Save zation/1ebba63972ef6ce623238af664972250 to your computer and use it in GitHub Desktop.
A shell file to show current hostname in iTerm2 window for iTerm2 version 3.0.0
#!/bin/bash
# SSH with host name and IP address in background (only in iTerm.app)
# This is depended on imagemagick
# First, check to see if we have the correct terminal!
if [ "$(tty)" == 'not a tty' ] || [ "$TERM_PROGRAM" != "iTerm.app" ] ; then
/usr/bin/ssh "$@"
exit $?
fi
function __calculate_iterm_window_dimensions {
local size=( $(osascript -e "tell application \"iTerm\"
get bounds of window 1
end tell" | tr ',' ' ') )
local x1=${size[0]} y1=${size[1]} x2=${size[2]} y2=${size[3]}
# 15px - scrollbar width
local w=$(( $x2 - $x1 - 15 ))
# 44px - titlebar + tabs height
local h=$(( $y2 - $y1 - 44))
echo "${w}x${h}"
}
# Console dimensions
DIMENSIONS=$(__calculate_iterm_window_dimensions)
BG_COLOR="#000000" # Background color
FG_COLOR="#FFFFFF" # Foreground color
GRAVITY="NorthEast" # Text gravity (NorthWest, North, NorthEast,
# West, Center, East, SouthWest, South, SouthEast)
OFFSET1="20,10" # Text offset (host name)
OFFSET2="20,80" # Text offset (ip address)
FONT_SIZE="60" # Font size in points
FONT_STYLE="Normal" # Font style (Any, Italic, Normal, Oblique)
# Font path
FONT="/System/Library/Fonts/Helvetica.dfont"
HOSTNAME=`echo $@ | sed -e "s/.*@//" -e "s/ .*//"`
output=`dscacheutil -q host -a name $HOSTNAME`
RESOLVED_HOSTNAME=`echo -e "$output"|grep '^name:'|awk '{print $2}'`
RESOLVED_IP=`echo -e "$output"|grep '^ip_address:'|awk '{print $2}'`
function set_bg {
osascript -e "
tell application \"iTerm2\"
repeat with theWindow in windows
tell theWindow
try
tell current session
set background image to \"$1\"
end tell
on error errmesg number errn
end try
end tell
end repeat
end tell"
}
on_exit () {
if [ ! -f /tmp/iTermBG.empty.png ]; then
convert -size "$DIMENSIONS" xc:"$BG_COLOR" "/tmp/iTermBG.empty.png"
fi
set_bg "/tmp/iTermBG.empty.png"
rm "/tmp/iTermBG.$$.png"
}
trap on_exit EXIT
convert \
-size "$DIMENSIONS" xc:"$BG_COLOR" -gravity "$GRAVITY" -fill "$FG_COLOR" \
-font "$FONT" -style "$FONT_STYLE" -pointsize "$FONT_SIZE" -antialias \
-draw "text $OFFSET1 '${RESOLVED_HOSTNAME:-$HOSTNAME}'" \
-pointsize 30 -draw "text $OFFSET2 '${RESOLVED_IP:-}'" \
"/tmp/iTermBG.$$.png"
set_bg "/tmp/iTermBG.$$.png"
/usr/bin/ssh "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment