Skip to content

Instantly share code, notes, and snippets.

@kimw
Created October 5, 2016 06:32
Show Gist options
  • Save kimw/b5a6f7ce206f3d85bbd8b4a9a1af6a1f to your computer and use it in GitHub Desktop.
Save kimw/b5a6f7ce206f3d85bbd8b4a9a1af6a1f to your computer and use it in GitHub Desktop.
#!/bin/sh
# Reset pantheon-terminal window at specific size and/or position.
#
# Kim <kimw@users.noreply.github.com>, Wed Oct 5 06:15:46 UTC 2016
#
# This script is published in GPLv3.
PATH=/bin:/usr/bin
main() {
if [ ! "$PANTHEON_TERMINAL_ID" ]; then
echo "Please run in pantheon-terminal."
exit 1
fi
if ! which gsettings >/dev/null; then
echo 'Command `gsettings` is required.'; exit 1
fi
if ! which xdotool >/dev/null; then
echo 'Command `xdotool` is required.'; exit 1
fi
if [ "$1" = '-h' -o "$1" = '--help' ]; then
usage; exit
elif [ "$1" = '-v' -o "$1" = '--verbose' ]; then
local verbose='true'
shift
elif echo "$1" | grep '^-' >/dev/null; then
echo "Illegal option: $1"
exit 1
fi
case $# in
0 ) local rows=24 cols=80 ;;
1 ) if [ "$1" = '@' ]; then
local x=0 y=0
elif echo $1 | grep '^[0-9-]\+,[0-9-]\+$' >/dev/null; then
local cols=$(echo $1 | sed 's/,[0-9-]\+$//')
local rows=$(echo $1 | sed 's/^[0-9-]\+,//')
elif echo $1 | grep '^@[0-9-]\+,[0-9-]\+$' >/dev/null; then
local x=$(echo $1 | sed -e 's/^@//' -e 's/,[0-9-]\+$//')
local y=$(echo $1 | sed 's/^@[0-9-]\+,//')
elif echo $1 | grep '^@\(tl\|tr\|bl\|br\)$' >/dev/null; then
case $1 in
@tl) local x=-1000 y=-1000 ;;
@tr) local x=1000 y=-1000 ;;
@bl) local x=-1000 y=1000 ;;
@br) local x=1000 y=1000 ;;
esac
else
echo "Invalid operation. ($1)"; exit 1
fi
;;
2 ) if echo $1 | grep '^[0-9-]\+,[0-9-]\+$' >/dev/null\
&& [ "$2" = '@' ]; then
local cols=$(echo $1 | sed 's/,[0-9-]\+$//')
local rows=$(echo $1 | sed 's/^[0-9-]\+,//')
local x=0 y=0
elif echo $1 | grep '^[0-9-]\+,[0-9-]\+$' >/dev/null\
&& echo $2 | grep '^@\(tl\|tr\|bl\|br\)$' >/dev/null; then
local cols=$(echo $1 | sed 's/,[0-9-]\+$//')
local rows=$(echo $1 | sed 's/^[0-9-]\+,//')
case $2 in
@tl) local x=-1000 y=-1000 ;;
@tr) local x=1000 y=-1000 ;;
@bl) local x=-1000 y=1000 ;;
@br) local x=1000 y=1000 ;;
esac
elif echo $1 | grep '^[0-9-]\+,[0-9-]\+$' >/dev/null\
&& echo $2 | grep '^@[0-9-]\+,[0-9-]\+$' >/dev/null; then
local cols=$(echo $1 | sed 's/,[0-9-]\+$//')
local rows=$(echo $1 | sed 's/^[0-9-]\+,//')
local x=$(echo $2 | sed -e 's/^@//' -e 's/,[0-9-]\+$//')
local y=$(echo $2 | sed 's/^@[0-9-]\+,//')
elif echo $1 | grep '^[0-9-]\+,[0-9-]\+$' >/dev/null\
&& echo $2 | grep '^@\(tl\|tr\|bl\|br\)$' >/dev/null; then
local cols=$(echo $1 | sed 's/,[0-9-]\+$//')
local rows=$(echo $1 | sed 's/^[0-9-]\+,//')
case $2 in
@tl) local x=-1000 y=-1000 ;;
@tr) local x=1000 y=-1000 ;;
@bl) local x=-1000 y=1000 ;;
@br) local x=1000 y=1000 ;;
esac
else
echo 'Invalid operation.'; exit 1
fi
;;
* ) echo 'Invalid operation.'; exit 1 ;;
esac
if [ "$cols" -a "$rows" ]; then
local width=$(expr 160 + $cols \* 8)
local height=$(expr 238 + $rows \* 17)
fi
if [ "$verbose" ]; then
echo "DEBUG: x=${x:-<unset>}, y=${y:-<unset>}, cols=${cols:-<unset>}, rows=${rows:-<unset>}, width=${width:-<unset>}, height=${height:-<unset>}"
fi
if [ "$cols" -a "$rows" ]; then
xdotool windowsize $(xdotool getactivewindow) $width $height
gsettings set org.pantheon.terminal.saved-state window-width $width
gsettings set org.pantheon.terminal.saved-state window-height $height
fi
if [ "$x" -a "$y" ]; then
xdotool windowmove $(xdotool getactivewindow) $x $y
gsettings set org.pantheon.terminal.saved-state opening-x $x
gsettings set org.pantheon.terminal.saved-state opening-y $y
fi
if [ "$verbose" ]; then
local stty_cols=$(stty size | awk {print\$2})
local stty_rows=$(stty size | awk {print\$1})
echo "DEBUG: stty: ($stty_cols)x($stty_rows)"
sleep 0.4 # wait for sync
local gset_x=$(gsettings list-recursively org.pantheon.terminal.saved-state | grep opening-x | awk {print\$3})
local gset_y=$(gsettings list-recursively org.pantheon.terminal.saved-state | grep opening-y | awk {print\$3})
local gset_width=$(gsettings list-recursively org.pantheon.terminal.saved-state | grep window-width | awk {print\$3})
local gset_height=$(gsettings list-recursively org.pantheon.terminal.saved-state | grep window-height | awk {print\$3})
echo "DEBUG: gsettings: ($gset_x)x($gset_y) ($gset_width)x($gset_height)"
fi
}
usage() {
printf "Usage: ${0##*/} [-v|--verbose] [-h|--help] [cols,rows] [@x,y]\n\n"
printf "Examples: \n\n"
printf " ${0##*/}\n\n"
printf " Reset terminal window to 80 chars width and 24 chars height at current\n"
printf " postition.\n\n"
printf " ${0##*/} 100,30\n\n"
printf " Reset terminal window to 100 chars width and 30 chars height at current\n"
printf " postition.\n\n"
printf " ${0##*/} @\n\n"
printf " Move terminal window to position (0, 0).\n\n"
printf " ${0##*/} @10,10\n\n"
printf " Move terminal window to position (10, 10).\n\n"
printf " ${0##*/} 100,30 @\n\n"
printf " Reset terminal window to 100 chars width and 30 chars height at\n"
printf " position (0, 0).\n\n"
printf " ${0##*/} 100,30 @10,10\n\n"
printf " Reset terminal window to 100 chars width and 30 chars height at\n"
printf " position (10, 10).\n\n"
}
main "$@"
# vim: sw=4 sts=4 et ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment