From 0e36fec20e3c27eaa459e1083833cb85ccf2b34b Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Wed, 5 Aug 2009 10:09:27 -0700 Subject: Fix stty/tput usage for columns This corrects the usage of stty and tput under various non-TTY conditions such as under cron and redirected output. [Aaron: Also corrected my USECOLOR bug I introduced last patch] Signed-off-by: Aaron Griffin --- functions | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'functions') diff --git a/functions b/functions index 96903b9..0f333d4 100644 --- a/functions +++ b/functions @@ -4,9 +4,28 @@ # width: -# disable color on output to non-tty STAT_COL=80 -USECOLOR="" +if [ -t 1 ]; then + # stty will fail when stdin isn't a terminal (but we're in this block, so stdout is) + if [ -t 0 ]; then + STAT_COL="$(/bin/stty size)" + # stty gives "rows cols"; strip the rows number, we just want columns + STAT_COL="${STAT_COL##* }" + else + # tput will fail at boot time if /usr/share/terminfo isn't yet mounted + # or TERM is otherwise unrecognized + STAT_COL="$(/bin/tput cols 2>/dev/null)" + fi + if [ "0$STAT_COL" -eq 0 ]; then + # if output was 0 (serial console), set default width to 80 + USECOLOR="" + STAT_COL=80 + fi +else + USECOLOR="" + STAT_COL=80 +fi + if [ -t 1 ]; then STAT_COL=$(/bin/stty size) # strip the rows number, we just want columns @@ -140,13 +159,17 @@ status() { # 1 - not found # Copied from makepkg in_array() { - local needle=$1; shift - [ -z "$1" ] && return 1 # Not Found - local item - for item in "$@"; do - [ "$item" = "$needle" ] && return 0 # Found - done - return 1 # Not Found + local needle=$1; shift + [ -z "$1" ] && return 1 # Not Found + local item + for item in "$@"; do + local c="${item:0:1}" + if [ "$c" == "!" -o "$c" == "@" ]; then + item="${item:1}" + fi + [ "$item" = "$needle" ] && return 0 # Found + done + return 1 # Not Found } # daemons: -- cgit v1.2.3