From cb1512b14b8c7ee6f29dc7b9be57dbb582cda2cd Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Tue, 4 Jan 2011 23:39:48 +1100 Subject: [PATCH] Use named constants for terminal control codes. (apenwarr slightly changed the minimal/do tty detection.) --- log.py | 44 ++++++++++++++++++++++---------------------- minimal/do | 16 ++++++++++++++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/log.py b/log.py index 736f732..c217e7e 100644 --- a/log.py +++ b/log.py @@ -1,6 +1,22 @@ import sys, os import vars +# By default, no output colouring. +RED = "" +GREEN = "" +YELLOW = "" +BOLD = "" +PLAIN = "" + +if sys.stderr.isatty(): + # ...use ANSI formatting codes. + RED = "\x1b[31m" + GREEN = "\x1b[32m" + YELLOW = "\x1b[33m" + BOLD = "\x1b[1m" + PLAIN = "\x1b[m" + + def log_(s): sys.stdout.flush() if vars.DEBUG_PIDS: @@ -10,30 +26,14 @@ def log_(s): sys.stderr.flush() -def _clog(s): - log_('\x1b[32mredo %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) -def _bwlog(s): - log_('redo %s%s' % (vars.DEPTH, s)) +def log(s): + log_(''.join([GREEN, "redo ", vars.DEPTH, BOLD, s, PLAIN])) -def _cerr(s): - log_('\x1b[31mredo: %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) -def _bwerr(s): - log_('redo: %s%s' % (vars.DEPTH, s)) +def err(s): + log_(''.join([RED, "redo ", vars.DEPTH, BOLD, s, PLAIN])) -def _cwarn(s): - log_('\x1b[33mredo: %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) -def _bwwarn(s): - log_('redo: %s%s' % (vars.DEPTH, s)) - - -if os.isatty(2): - log = _clog - err = _cerr - warn = _cwarn -else: - log = _bwlog - err = _bwerr - warn = _bwwarn +def warn(s): + log_(''.join([YELLOW, "redo ", vars.DEPTH, BOLD, s, PLAIN])) def debug(s): diff --git a/minimal/do b/minimal/do index 30d740c..e4b02ae 100755 --- a/minimal/do +++ b/minimal/do @@ -6,6 +6,18 @@ # The author disclaims copyright to this source file and hereby places it in # the public domain. (2010 12 14) # + +# By default, no output coloring. +GREEN="" +BOLD="" +PLAIN="" + +if tty <&2 >/dev/null 2>&1; then + GREEN="$(printf '\033[32m')" + BOLD="$(printf '\033[1m')" + PLAIN="$(printf '\033[m')" +fi + _dirsplit() { base=${1##*/} @@ -72,8 +84,8 @@ _do() DIR=$1 TARGET=$2 if [ ! -e "$TARGET" ] || [ -e "$TARGET/." -a ! -e "$TARGET.did" ]; then - printf '\033[32mdo %s\033[1m%s\033[m\n' \ - "$DO_DEPTH" "$DIR$TARGET" >&2 + printf '%sdo %s%s%s%s\n' \ + "$GREEN" "$DO_DEPTH" "$BOLD" "$DIR$TARGET" "$PLAIN" >&2 echo "$PWD/$TARGET" >>"$DO_BUILT" DOFILE=$TARGET.do BASE=$TARGET