Skip to content

Instantly share code, notes, and snippets.

@zsteva
Last active May 11, 2017 08:27
Show Gist options
  • Save zsteva/48373f425a52d9b5d0eb1488df17d89b to your computer and use it in GitHub Desktop.
Save zsteva/48373f425a52d9b5d0eb1488df17d89b to your computer and use it in GitHub Desktop.
#!/bin/bash
# src http://fnxweb.com/blog/2012/08/02/read-the-current-terminal-size-from-within-bash/
# Request size
stty -echo
echo -ne '\e[18t'
# Read size char. by char. (CSI 8 ; height ; width t)
width=
heigh=
p=0
while IFS= read -r -n1 char
do
# Get past CSI
if [[ "$p" == 0 && "$char" == ";" ]]; then
p=$((p+1))
# width
elif [[ "$p" == 1 ]]; then
[[ "$char" == ";" ]] && p=$((p+1)) || height="${height}${char}"
# height
elif [[ "$p" == 2 ]]; then
[[ "$char" == "t" ]] && break || width="${width}${char}"
fi
done
# Done
stty echo
if [ "x$1" = "xstty" ]; then
stty cols $width rows $height
else
echo -e "width=$width\nheight=$height"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment