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

5
jsvn
View File

@ -71,11 +71,13 @@ 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):
if using_color:
bc = 1 if bold else 0 bc = 1 if bold else 0
if fg is not None: if fg is not None:
out.write('\033[%d;%dm' % (bc, 30 + COLORS[fg])) out.write('\033[%d;%dm' % (bc, 30 + COLORS[fg]))
@ -83,6 +85,7 @@ def ansi_color(out, fg=None, bg=None, bold=False):
out.write('\033[%d;%dm' % (bc, 40 + COLORS[bg])) out.write('\033[%d;%dm' % (bc, 40 + COLORS[bg]))
def ansi_reset(out): def ansi_reset(out):
if using_color:
out.write('\033[0m') out.write('\033[0m')
def colordiff(out, line): def colordiff(out, 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'