Use named constants for terminal control codes.

(apenwarr slightly changed the minimal/do tty detection.)
This commit is contained in:
Tim Allen 2011-01-04 23:39:48 +11:00 committed by Avery Pennarun
commit cb1512b14b
2 changed files with 36 additions and 24 deletions

44
log.py
View file

@ -1,6 +1,22 @@
import sys, os import sys, os
import vars 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): def log_(s):
sys.stdout.flush() sys.stdout.flush()
if vars.DEBUG_PIDS: if vars.DEBUG_PIDS:
@ -10,30 +26,14 @@ def log_(s):
sys.stderr.flush() sys.stderr.flush()
def _clog(s): def log(s):
log_('\x1b[32mredo %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) log_(''.join([GREEN, "redo ", vars.DEPTH, BOLD, s, PLAIN]))
def _bwlog(s):
log_('redo %s%s' % (vars.DEPTH, s))
def _cerr(s): def err(s):
log_('\x1b[31mredo: %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) log_(''.join([RED, "redo ", vars.DEPTH, BOLD, s, PLAIN]))
def _bwerr(s):
log_('redo: %s%s' % (vars.DEPTH, s))
def _cwarn(s): def warn(s):
log_('\x1b[33mredo: %s\x1b[1m%s\x1b[m' % (vars.DEPTH, s)) log_(''.join([YELLOW, "redo ", vars.DEPTH, BOLD, s, PLAIN]))
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 debug(s): def debug(s):

View file

@ -6,6 +6,18 @@
# The author disclaims copyright to this source file and hereby places it in # The author disclaims copyright to this source file and hereby places it in
# the public domain. (2010 12 14) # 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() _dirsplit()
{ {
base=${1##*/} base=${1##*/}
@ -72,8 +84,8 @@ _do()
DIR=$1 DIR=$1
TARGET=$2 TARGET=$2
if [ ! -e "$TARGET" ] || [ -e "$TARGET/." -a ! -e "$TARGET.did" ]; then if [ ! -e "$TARGET" ] || [ -e "$TARGET/." -a ! -e "$TARGET.did" ]; then
printf '\033[32mdo %s\033[1m%s\033[m\n' \ printf '%sdo %s%s%s%s\n' \
"$DO_DEPTH" "$DIR$TARGET" >&2 "$GREEN" "$DO_DEPTH" "$BOLD" "$DIR$TARGET" "$PLAIN" >&2
echo "$PWD/$TARGET" >>"$DO_BUILT" echo "$PWD/$TARGET" >>"$DO_BUILT"
DOFILE=$TARGET.do DOFILE=$TARGET.do
BASE=$TARGET BASE=$TARGET