jsvn: only output ANSI color codes if output is a terminal

This commit is contained in:
Josh Holtrop 2012-02-20 15:57:25 -05:00
parent 4d96b6eed8
commit 820b6d0bf2

17
jsvn
View File

@ -71,19 +71,22 @@ COLORS = {
'cyan': 6, 'cyan': 6,
'white': 7, 'white': 7,
} }
using_color = False
########################################################################### ###########################################################################
# Utility Functions # # Utility Functions #
########################################################################### ###########################################################################
def ansi_color(out, fg=None, bg=None, bold=False): def ansi_color(out, fg=None, bg=None, bold=False):
bc = 1 if bold else 0 if using_color:
if fg is not None: bc = 1 if bold else 0
out.write('\033[%d;%dm' % (bc, 30 + COLORS[fg])) if fg is not None:
if bg is not None: out.write('\033[%d;%dm' % (bc, 30 + COLORS[fg]))
out.write('\033[%d;%dm' % (bc, 40 + COLORS[bg])) if bg is not None:
out.write('\033[%d;%dm' % (bc, 40 + COLORS[bg]))
def ansi_reset(out): def ansi_reset(out):
out.write('\033[0m') if using_color:
out.write('\033[0m')
def colordiff(out, line): def colordiff(out, line):
if re.search(r'^Index:\s', line): if re.search(r'^Index:\s', line):
@ -462,10 +465,12 @@ def root(argv, svn, out):
# Main # # Main #
########################################################################### ###########################################################################
def main(argv): def main(argv):
global using_color
realsvn = findInPath('svn') realsvn = findInPath('svn')
colorsvn = findInPath('colorsvn') colorsvn = findInPath('colorsvn')
out = sys.stdout out = sys.stdout
using_pager = False using_pager = False
using_color = sys.stdout.isatty()
if sys.stdout.isatty(): if sys.stdout.isatty():
if not (len(argv) >= 1 and argv[0] in ('commit', 'propedit')): if not (len(argv) >= 1 and argv[0] in ('commit', 'propedit')):
pager = 'less -FRX' pager = 'less -FRX'