From 6c75a9ff64912e4cc337d38b3cc93eec2b68bd62 Mon Sep 17 00:00:00 2001 From: zwhfly Date: Fri, 21 Apr 2017 18:19:09 +0800 Subject: [PATCH] Support foreground/background reversing ESC [ 7 m swap foreground and background --- README.rst | 1 + colorama/ansi.py | 1 + colorama/ansitowin32.py | 1 + colorama/winterm.py | 6 ++++++ 4 files changed, 9 insertions(+) diff --git a/README.rst b/README.rst index 0a76fa6..220b129 100644 --- a/README.rst +++ b/README.rst @@ -238,6 +238,7 @@ The only ANSI sequences that colorama converts into win32 calls are:: ESC [ 0 m # reset all (colors and brightness) ESC [ 1 m # bright ESC [ 2 m # dim (looks same as normal brightness) + ESC [ 7 m # swap foreground and background ESC [ 22 m # normal brightness # FOREGROUND: diff --git a/colorama/ansi.py b/colorama/ansi.py index 7877658..989b055 100644 --- a/colorama/ansi.py +++ b/colorama/ansi.py @@ -95,6 +95,7 @@ class AnsiStyle(AnsiCodes): DIM = 2 NORMAL = 22 RESET_ALL = 0 + REVERSE_FORE_BACK = 7 Fore = AnsiFore() Back = AnsiBack() diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 5bfc01e..16c1696 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -99,6 +99,7 @@ def get_win32_calls(self): if self.convert and winterm: return { AnsiStyle.RESET_ALL: (winterm.reset_all, ), + AnsiStyle.REVERSE_FORE_BACK: (winterm.reverse_fore_back, ), AnsiStyle.BRIGHT: (winterm.style, WinStyle.BRIGHT), AnsiStyle.DIM: (winterm.style, WinStyle.NORMAL), AnsiStyle.NORMAL: (winterm.style, WinStyle.NORMAL), diff --git a/colorama/winterm.py b/colorama/winterm.py index 60309d3..066c8b1 100644 --- a/colorama/winterm.py +++ b/colorama/winterm.py @@ -45,6 +45,12 @@ def reset_all(self, on_stderr=None): self.set_attrs(self._default) self.set_console(attrs=self._default) + def reverse_fore_back(self, on_stderr=None): + self._back, self._fore = self._fore, self._back + self._style = ((self._style & 0xF) << 4) | ((self._style >> 4) & 0xF) + self._light = ((self._light & 0xF) << 4) | ((self._light >> 4) & 0xF) + self.set_console(on_stderr=on_stderr) + def fore(self, fore=None, light=False, on_stderr=False): if fore is None: fore = self._default_fore