Skip to content

Instantly share code, notes, and snippets.

@nega0
Created March 24, 2016 21:06
Show Gist options
  • Save nega0/d700ad595f92d79c010c to your computer and use it in GitHub Desktop.
Save nega0/d700ad595f92d79c010c to your computer and use it in GitHub Desktop.
bash script to "snap" the current terminal emulator to an aspect ratio
#!/bin/bash
## bash script to "snap" the current terminal emulator to an aspect
## ratio maintaining either the width or the height. note that your
## terminal will actually snap to the closest size that has no
## partial character cells
ar="16/10" ## set your aspect ratio here
ar=$(bc -l <<< "$ar")
calcs() {
local success=false
exec < /dev/tty
local oldstty=$(stty -g)
stty raw -echo min 0
echo -e "\e[14t" >/dev/tty
if IFS=';' read -r -d '\' -a color ; then
h=${color[1]}; echo $h
w=${color[2]%%t*}; echo $w
success=true; stty $oldstty
return 0
fi
stty $oldstty
return 0
$success
}
## snap to aspect ratio maintaining width
snapx() {
read -r -a p <<< $(calcs)
if [ $? == 0 ]; then
local h=${p[0]}; local w=${p[1]}
echo -e "[4;$(printf %0.f $(bc -l <<< "$w / $ar"));${w}t"
return 0
fi
return 1
}
## snap to aspect ratio maintaining height
snapy() {
read -r -a p <<< $(calcs)
if [ $? == 0 ]; then
local h=${p[0]}; local w=${p[1]}
echo -e "[4;${h};$(printf %0.f $(bc -l <<< "$h * $ar"))t"
return 0
fi
return 1
}
case $0 in
snapx|*/snapx)
snapx
exit $?
;;
snapy|*/snapy)
snapy
exit $?
;;
*)
echo "Usage error: must be called as snapx or snapy" || exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment