# HG changeset patch # User Pierre-Yves David # Date 1482413446 -3600 # Node ID 9021a94a7dbf63f1805cba95d7ffb334aa6b7f65 # Parent 8903f67b9ca89d2bdd6b59abaca40fab71e7e4b1 color: move 'modesetup' into the core module Yet another piece of code moving from the extensions to the module in core! diff -r 8903f67b9ca8 -r 9021a94a7dbf hgext/color.py --- a/hgext/color.py Thu Dec 22 14:17:52 2016 +0100 +++ b/hgext/color.py Thu Dec 22 14:30:46 2016 +0100 @@ -176,12 +176,9 @@ color, commands, dispatch, - encoding, extensions, - pycompat, subrepo, ui as uimod, - util, ) cmdtable = {} @@ -192,76 +189,11 @@ # leave the attribute unspecified. testedwith = 'ships-with-hg-core' -def _modesetup(ui, coloropt): - if coloropt == 'debug': - return 'debug' - - auto = (coloropt == 'auto') - always = not auto and util.parsebool(coloropt) - if not always and not auto: - return None - - formatted = (always or (encoding.environ.get('TERM') != 'dumb' - and ui.formatted())) - - mode = ui.config('color', 'mode', 'auto') - - # If pager is active, color.pagermode overrides color.mode. - if getattr(ui, 'pageractive', False): - mode = ui.config('color', 'pagermode', mode) - - realmode = mode - if mode == 'auto': - if pycompat.osname == 'nt': - term = encoding.environ.get('TERM') - # TERM won't be defined in a vanilla cmd.exe environment. - - # UNIX-like environments on Windows such as Cygwin and MSYS will - # set TERM. They appear to make a best effort attempt at setting it - # to something appropriate. However, not all environments with TERM - # defined support ANSI. Since "ansi" could result in terminal - # gibberish, we error on the side of selecting "win32". However, if - # w32effects is not defined, we almost certainly don't support - # "win32", so don't even try. - if (term and 'xterm' in term) or not color.w32effects: - realmode = 'ansi' - else: - realmode = 'win32' - else: - realmode = 'ansi' - - def modewarn(): - # only warn if color.mode was explicitly set and we're in - # a formatted terminal - if mode == realmode and ui.formatted(): - ui.warn(_('warning: failed to set color mode to %s\n') % mode) - - if realmode == 'win32': - color._terminfo_params.clear() - if not color.w32effects: - modewarn() - return None - color._effects.update(color.w32effects) - elif realmode == 'ansi': - color._terminfo_params.clear() - elif realmode == 'terminfo': - color._terminfosetup(ui, mode) - if not color._terminfo_params: - ## FIXME Shouldn't we return None in this case too? - modewarn() - realmode = 'ansi' - else: - return None - - if always or (auto and formatted): - return realmode - return None - def uisetup(ui): if ui.plain(): return def colorcmd(orig, ui_, opts, cmd, cmdfunc): - mode = _modesetup(ui_, opts['color']) + mode = color._modesetup(ui_, opts['color']) uimod.ui._colormode = mode if mode and mode != 'debug': color.configstyles(ui_) diff -r 8903f67b9ca8 -r 9021a94a7dbf mercurial/color.py --- a/mercurial/color.py Thu Dec 22 14:17:52 2016 +0100 +++ b/mercurial/color.py Thu Dec 22 14:30:46 2016 +0100 @@ -9,7 +9,11 @@ from .i18n import _ -from . import pycompat +from . import ( + encoding, + pycompat, + util +) try: import curses @@ -157,6 +161,71 @@ "ECMA-48 color\n")) _terminfo_params.clear() +def _modesetup(ui, coloropt): + if coloropt == 'debug': + return 'debug' + + auto = (coloropt == 'auto') + always = not auto and util.parsebool(coloropt) + if not always and not auto: + return None + + formatted = (always or (encoding.environ.get('TERM') != 'dumb' + and ui.formatted())) + + mode = ui.config('color', 'mode', 'auto') + + # If pager is active, color.pagermode overrides color.mode. + if getattr(ui, 'pageractive', False): + mode = ui.config('color', 'pagermode', mode) + + realmode = mode + if mode == 'auto': + if pycompat.osname == 'nt': + term = encoding.environ.get('TERM') + # TERM won't be defined in a vanilla cmd.exe environment. + + # UNIX-like environments on Windows such as Cygwin and MSYS will + # set TERM. They appear to make a best effort attempt at setting it + # to something appropriate. However, not all environments with TERM + # defined support ANSI. Since "ansi" could result in terminal + # gibberish, we error on the side of selecting "win32". However, if + # w32effects is not defined, we almost certainly don't support + # "win32", so don't even try. + if (term and 'xterm' in term) or not w32effects: + realmode = 'ansi' + else: + realmode = 'win32' + else: + realmode = 'ansi' + + def modewarn(): + # only warn if color.mode was explicitly set and we're in + # a formatted terminal + if mode == realmode and ui.formatted(): + ui.warn(_('warning: failed to set color mode to %s\n') % mode) + + if realmode == 'win32': + _terminfo_params.clear() + if not w32effects: + modewarn() + return None + _effects.update(w32effects) + elif realmode == 'ansi': + _terminfo_params.clear() + elif realmode == 'terminfo': + _terminfosetup(ui, mode) + if not _terminfo_params: + ## FIXME Shouldn't we return None in this case too? + modewarn() + realmode = 'ansi' + else: + return None + + if always or (auto and formatted): + return realmode + return None + def configstyles(ui): for status, cfgeffects in ui.configitems('color'): if '.' not in status or status.startswith(('color.', 'terminfo.')):