This commit is contained in:
DoodleIncident 2014-02-04 20:49:20 -08:00
commit 7fa6447cef

View file

@ -546,6 +546,9 @@ def colorup(text, pattern, color="red", style="normal", on_groups=False):
return colored_text return colored_text
def no_op(text):
return text
########### ###########
# Helpers # # Helpers #
@ -746,11 +749,16 @@ def __args_parse__(argv, usage=""):
if it is lower case, use the 8 colors mode. \ if it is lower case, use the 8 colors mode. \
Interpret COLOR as a Pygments style.") Interpret COLOR as a Pygments style.")
parser.add_argument("--autocolor", action="store_true",
help="Replicates the functionality of --color=auto in ls or grep. \
If set, colout will only color text if it's connected to a terminal.")
args = parser.parse_args() args = parser.parse_args()
return args.pattern[0], args.color, args.style, args.groups, \ return args.pattern[0], args.color, args.style, args.groups, \
args.colormap, args.theme, args.source, args.all, args.scale, args.debug, args.resources, args.palettes_dir, \ args.colormap, args.theme, args.source, args.all, args.scale, args.debug, args.resources, args.palettes_dir, \
args.themes_dir args.themes_dir, \
args.autocolor
def write_all( as_all, stream_in, stream_out, function, *args ): def write_all( as_all, stream_in, stream_out, function, *args ):
@ -785,7 +793,7 @@ if __name__ == "__main__":
# if argparse is available # if argparse is available
else: else:
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \ pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \
debug, resources, palettes_dirs, themes_dirs \ debug, resources, palettes_dirs, themes_dirs, autocolor \
= __args_parse__(sys.argv, usage) = __args_parse__(sys.argv, usage)
if debug: if debug:
@ -795,6 +803,11 @@ if __name__ == "__main__":
logging.basicConfig(format='[colout] %(levelname)s: %(message)s', level=lvl) logging.basicConfig(format='[colout] %(levelname)s: %(message)s', level=lvl)
if autocolor and not sys.stdout.isatty():
pass_through = True
else:
pass_through = False
################## ##################
# Load resources # # Load resources #
@ -880,8 +893,13 @@ if __name__ == "__main__":
color = "colormap" # use the keyword to switch to colormap instead of list of colors color = "colormap" # use the keyword to switch to colormap instead of list of colors
logging.debug("used-defined colormap: %s" % ",".join(colormap) ) logging.debug("used-defined colormap: %s" % ",".join(colormap) )
# if noop
if pass_through:
logging.debug( "passing input through without modification" )
write_all( as_all, sys.stdin, sys.stdout, no_op )
# if theme # if theme
if as_theme: elif as_theme:
logging.debug( "asked for theme: %s" % pattern ) logging.debug( "asked for theme: %s" % pattern )
assert(pattern in themes.keys()) assert(pattern in themes.keys())
write_all( as_all, sys.stdin, sys.stdout, colortheme, themes[pattern].theme() ) write_all( as_all, sys.stdin, sys.stdout, colortheme, themes[pattern].theme() )