From c111811bab645950414ba8fe43107baa91e01cb4 Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Sat, 9 Feb 2019 13:50:47 -0500 Subject: [PATCH] some translations --- xonsh/ansi_colors.py | 22 ++++++++++++++++++++-- xonsh/environ.py | 30 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/xonsh/ansi_colors.py b/xonsh/ansi_colors.py index ba12636b6..c9e7b888b 100644 --- a/xonsh/ansi_colors.py +++ b/xonsh/ansi_colors.py @@ -125,9 +125,17 @@ def ansi_reverse_style(style='default', return_style=False): # add keys to make this more useful updates = { '1': 'BOLD_', + '2': 'FAINT_', '4': 'UNDERLINE_', + '5': 'SLOWBLINK_', '1;4': 'BOLD_UNDERLINE_', '4;1': 'BOLD_UNDERLINE_', + '38': 'SET_FOREGROUND_', + '48': 'SET_BACKGROUND_', + '38;2': 'SET_FOREGROUND_RGB_', + '48;2': 'SET_BACKGROUND_RGB_', + '38;5': 'SET_FOREGROUND_SHORT_', + '48;5': 'SET_BACKGROUND_SHORT_', } for ec, name in reversed_style.items(): no_left_zero = ec.lstrip('0') @@ -148,6 +156,17 @@ def ANSI_ESCAPE_CODE_RE(): return re.compile(r'\001?(\033\[)?([0-9;]+)m?\002?') +@lazyobject +def ANSI_REVERSE_COLOR_NAME_TRANSLATIONS(): + return { + 'UNDERLINE_BOLD_': 'BOLD_UNDERLINE_', + 'SET_FOREGROUND_FAINT_': 'SET_FOREGROUND_RGB_', + 'SET_FOREGROUND_FAINT_': 'SET_FOREGROUND_RGB_', + 'SET_FOREGROUND_SLOWBLINK_': 'SET_FOREGROUND_SHORT_', + 'SET_BACKGROUND_SLOWBLINK_': 'SET_BACKGROUND_SHORT_', + } + + def ansi_color_escape_code_to_name(escape_code, style='default', reversed_style=None): """Converts an ASNI color code escape sequence to a tuple of color names in the provided style ('default', by default). For example, @@ -170,8 +189,7 @@ def ansi_color_escape_code_to_name(escape_code, style='default', reversed_style= # skip '0' entries continue n = n + name if n else name - if n == 'UNDERLINE_BOLD_': - n = 'BOLD_UNDERLINE_' + n = ANSI_REVERSE_COLOR_NAME_TRANSLATIONS.get(n, n) if n.endswith('_'): continue norm_names.append(n) diff --git a/xonsh/environ.py b/xonsh/environ.py index c08975546..7b8f24394 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -1297,22 +1297,19 @@ class Env(cabc.MutableMapping): self._d["PATH"] = list(PATH_DEFAULT) self._detyped = None - @staticmethod - def detypeable(val): - return not (callable(val) or isinstance(val, cabc.MutableMapping)) - def detype(self): if self._detyped is not None: return self._detyped ctx = {} for key, val in self._d.items(): - if key not in self._ensurers and not self.detypeable(val): - continue if not isinstance(key, str): key = str(key) ensurer = self.get_ensurer(key) - val = ensurer.detype(val) - ctx[key] = val + if not ensurer.detype or not callable(ensurer.detype): + # cannot actually detype this var. + continue + deval = ensurer.detype(val) + ctx[key] = deval self._detyped = ctx return ctx @@ -1334,7 +1331,20 @@ class Env(cabc.MutableMapping): os_environ.update(self._orig_env) self._orig_env = None - def get_ensurer(self, key, default=Ensurer(always_true, None, ensure_string)): + @staticmethod + def detypeable(val): + return not (callable(val) or isinstance(val, cabc.MutableMapping)) + + def _get_default_ensurer(self, val, default=None): + if default is not None: + return default + if self.detypeable(val): + default = Ensurer(always_true, None, ensure_string) + else: + default = Ensurer(always_true, None, False) + return default + + def get_ensurer(self, key, val=None, default=None): """Gets an ensurer for the given key.""" if key in self._ensurers: return self._ensurers[key] @@ -1344,7 +1354,7 @@ class Env(cabc.MutableMapping): if k.match(key) is not None: break else: - ensurer = default + ensurer = self._get_default_ensurer(val=val, default=default) self._ensurers[key] = ensurer return ensurer