Index: pym/output.py =================================================================== --- pym/output.py (revision 4905) +++ pym/output.py (revision 4906) @@ -210,15 +210,18 @@ def nocolor(): "turn off colorization" + global havecolor havecolor=0 - for x in codes.keys(): - codes[x]="" def resetColor(): return codes["reset"] def colorize(color_key, text): - return codes[color_key] + text + codes["reset"] + global havecolor + if havecolor: + return codes[color_key] + text + codes["reset"] + else: + return text compat_functions_colors = ["bold","white","teal","turquoise","darkteal", "fuscia","fuchsia","purple","blue","darkblue","green","darkgreen","yellow", Index: bin/emerge =================================================================== --- bin/emerge (revision 4905) +++ bin/emerge (revision 4906) @@ -32,6 +32,7 @@ from portage import digraph import emergehelp, xpak, commands, errno, re, socket, string, time, types +import output from output import blue, bold, colorize, darkblue, darkgreen, darkred, green, \ havecolor, nc_len, nocolor, red, teal, turquoise, white, xtermTitle, \ xtermTitleReset, yellow @@ -4063,6 +4064,11 @@ longopt_aliases = {"--cols":"--columns", "--skip-first":"--skipfirst"} argument_options = { + "--color": { + "help":"enable or disable color output", + "type":"choice", + "choices":("y", "n") + }, "--with-bdeps": { "help":"include unnecessary build time dependencies", "type":"choice", @@ -4123,6 +4129,11 @@ else: myfiles.append(x) + if "--nocolor" in myopts: + print "*** Deprecated use of '--nocolor', use '--color=n' instead." + del myopts["--nocolor"] + myopts["--color"] = "n" + return myaction, myopts, myfiles def validate_ebuild_environment(trees): @@ -4222,8 +4233,18 @@ settings["PORTAGE_DEBUG"] = str(PORTAGE_DEBUG) settings.backup_changes("PORTAGE_DEBUG") - # Set color output - if "--nocolor" in myopts: + """The explicit --color < y | n > option overrides the NOCOLOR environment + variable and stdout auto-detection.""" + if "--color" in myopts: + if "y" == myopts["--color"]: + output.havecolor = 1 + settings["NOCOLOR"] = "false" + else: + output.havecolor = 0 + settings["NOCOLOR"] = "true" + settings.backup_changes("NOCOLOR") + elif not sys.stdout.isatty(): + output.havecolor = 0 settings["NOCOLOR"] = "true" settings.backup_changes("NOCOLOR") @@ -4241,6 +4262,9 @@ ldpath_mtimes = mtimedb["ldpath"] xterm_titles = "notitles" not in settings.features + """Disable color as early as possible via NOCOLOR and stdout + auto-detection. This initial setting may later be overridden via the + --color < yes | no > option.""" if settings.get("NOCOLOR","").lower() in ("yes","true"): nocolor() elif (not sys.stdout.isatty()) and \ @@ -4380,11 +4404,6 @@ print "* --tree is currently broken with --resume. Disabling..." del myopts["--tree"] - # Set color output - if "--nocolor" in myopts or \ - settings["NOCOLOR"] in ("yes","true"): - nocolor() - if not ("--quiet" in myopts): if not sys.stdout.isatty() or ("--nospinner" in myopts): spinner.update = spinner.update_basic @@ -4395,7 +4414,7 @@ trees[settings["ROOT"]]["vartree"].dbapi) sys.exit(0) elif "--help" in myopts: - emergehelp.help(myaction, myopts, havecolor) + emergehelp.help(myaction, myopts, output.havecolor) sys.exit(0) if portage.wheelgid == portage.portage_gid: @@ -4407,7 +4426,7 @@ print "myopts", myopts if not myaction and not myfiles and "--resume" not in myopts: - emergehelp.help(myaction, myopts, havecolor) + emergehelp.help(myaction, myopts, output.havecolor) sys.exit(1) # check if root user is the current user for the actions where emerge needs this