sapling/eden/scm/tests/test-ui-color.py
Mark Thomas 513a4f8426 color: don't disable colors if HGPLAINEXCEPT=color
Summary:
With `HGPLAINEXCEPT=color`, colors should still be enabled if the terminal is
capable of supporting them and output is to the terminal.

Currently this doesn't work if `--color=auto` (the default), as
`color._modesetup` uses `ui.formatted` to check if the output is a terminal,
and thus has colors available, but this is `False` for all `HGPLAIN` modes.

Instead, add a new method to `ui` that checks whether  `fout` is a terminal, and
use that for color autodetection.

This function also allows us to add `HGPLAINEXCEPT=pager` as we can use
that in the same way.

Reviewed By: farnz

Differential Revision: D21617170

fbshipit-source-id: 7ee4eaa8963f3d6eb7ed8044a678a4804b9a98f0
2020-05-19 06:13:54 -07:00

39 lines
947 B
Python

from __future__ import absolute_import, print_function
import os
from edenscm.mercurial import dispatch, ui as uimod
from hghave import require
# ensure errors aren't buffered
testui = uimod.ui()
testui.pushbuffer()
testui.write(("buffered\n"))
testui.warn(("warning\n"))
testui.write_err("error\n")
print(repr(testui.popbuffer()))
# test dispatch.dispatch with the same ui object
hgrc = open(os.environ["HGRCPATH"], "w")
hgrc.write("[extensions]\n")
hgrc.write("color=\n")
hgrc.close()
ui_ = uimod.ui.load()
ui_.setconfig("ui", "assume-tty", "True")
# we're not interested in the output, so write that to devnull
ui_.fout = open(os.devnull, "wb")
# call some arbitrary command just so we go through
# color's wrapped _runcommand twice.
def runcmd():
dispatch.dispatch(dispatch.request(["version", "-q"], ui_))
runcmd()
print("colored? %s" % (ui_._colormode is not None))
runcmd()
print("colored? %s" % (ui_._colormode is not None))