From f0fedca16f6464e20fe737d96bf116df9d1528c8 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 7 Oct 2021 13:12:25 -0700 Subject: [PATCH] detect size with `stty size` instead of `tput` --- sh/glug | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/sh/glug b/sh/glug index 9efaaf4..c811fbd 100644 --- a/sh/glug +++ b/sh/glug @@ -19,11 +19,21 @@ glug() ( # note the subshell syntax. this allows us to abuse globals like crazy. done cd "$_here" - if [ -z "$ZSH_VERSION" ]; then - # TODO: is `stty size` more portable? - COLUMNS="${COLUMNS:-$(tput cols)}" - LINES="${LINES:-$(tput lines)}" - fi + detect_size() { + # globals: $COLUMNS, $LINES + _size="$(stty size)" + COLUMNS="${_size#* }" + LINES="${_size%% *}" + if [ "$COLUMNS" -gt 0 ] 2>/dev/null && [ "$LINES" -gt 0 ] 2>/dev/null; then + : # pass + else + printf '%s: failed to determine terminal size\n' glug >&2 + return 2 + fi + unset _size + } + + [ -n "$ZSH_VERSION" ] || detect_size if [ -n "$ZSH_VERSION" ] || [ -n "$BASH_VERSION" ]; then esc=$'\e'