tartley / colorama

Simple cross-platform colored terminal text in Python
BSD 3-Clause "New" or "Revised" License
3.52k stars 250 forks source link

Add support for UNDERLINED, BLINK, REVERSED (with patch!) #38

Open tartley opened 9 years ago

tartley commented 9 years ago

Migrated from https://code.google.com/p/colorama/issues/detail?id=39 Reported by irmendejong, Dec 7, 2012

I wrote some code to add a few more ansi-styles to colorama. Unfortunately the win32 style for underlined and reversevideo don't seem to work at all (even though there's constants defined for them). I am simulating the reversevideo one but the underlined one is just not working.

tartley commented 9 years ago
diff -r 4ccdb5498413 colorama/ansi.py
--- a/colorama/ansi.py  Sun May 27 10:09:37 2012 +0100
+++ b/colorama/ansi.py  Tue Dec 11 01:23:44 2012 +0100
@@ -38,10 +38,13 @@
     RESET   = 49

 class AnsiStyle:
-    BRIGHT    = 1
-    DIM       = 2
-    NORMAL    = 22
-    RESET_ALL = 0
+    BRIGHT     = 1
+    DIM        = 2
+    UNDERLINED = 4
+    BLINK      = 5
+    REVERSEVID = 7
+    NORMAL     = 22
+    RESET_ALL  = 0

 Fore = AnsiCodes( AnsiFore )
 Back = AnsiCodes( AnsiBack )
diff -r 4ccdb5498413 colorama/ansitowin32.py
--- a/colorama/ansitowin32.py   Sun May 27 10:09:37 2012 +0100
+++ b/colorama/ansitowin32.py   Tue Dec 11 01:23:44 2012 +0100
@@ -89,6 +89,7 @@
                 AnsiStyle.BRIGHT: (winterm.style, WinStyle.BRIGHT),
                 AnsiStyle.DIM: (winterm.style, WinStyle.NORMAL),
                 AnsiStyle.NORMAL: (winterm.style, WinStyle.NORMAL),
+                AnsiStyle.REVERSEVID: (winterm.style_reverse_vid, ),
                 AnsiFore.BLACK: (winterm.fore, WinColor.BLACK),
                 AnsiFore.RED: (winterm.fore, WinColor.RED),
                 AnsiFore.GREEN: (winterm.fore, WinColor.GREEN),
diff -r 4ccdb5498413 colorama/tests/ansi_test.py
--- a/colorama/tests/ansi_test.py   Sun May 27 10:09:37 2012 +0100
+++ b/colorama/tests/ansi_test.py   Tue Dec 11 01:23:44 2012 +0100
@@ -54,6 +54,10 @@
         self.assertEquals(Style.DIM, '\033[2m')
         self.assertEquals(Style.NORMAL, '\033[22m')
         self.assertEquals(Style.BRIGHT, '\033[1m')
+        self.assertEquals(Style.RESET_ALL, '\033[0m')
+        self.assertEquals(Style.UNDERLINED, '\033[4m')
+        self.assertEquals(Style.BLINK, '\033[5m')
+        self.assertEquals(Style.REVERSEVID, '\033[7m')

 if __name__ == '__main__':
diff -r 4ccdb5498413 colorama/tests/winterm_test.py
--- a/colorama/tests/winterm_test.py    Sun May 27 10:09:37 2012 +0100
+++ b/colorama/tests/winterm_test.py    Tue Dec 11 01:23:44 2012 +0100
@@ -91,6 +91,23 @@
         self.assertEquals(term._style, 22)
         self.assertEquals(term.set_console.called, True)

+    def testReverseVideo(self):
+        term = WinTerm()
+        term.set_console = Mock()
+        term._style = 0
+        term.fore(4)
+        term.back(5)
+        self.assertEquals(term._fore, 4)
+        self.assertEquals(term._back, 5)
+        term.style_reverse_vid()
+        self.assertEquals(term._fore, 5)
+        self.assertEquals(term._back, 4)
+        self.assertEquals(term._style, 0)
+        term.style_reverse_vid()
+        self.assertEquals(term._fore, 4)
+        self.assertEquals(term._back, 5)
+
+
     @patch('colorama.winterm.win32')
     def testSetConsole(self, mockWin32):
         mockAttr = Mock()
diff -r 4ccdb5498413 colorama/winterm.py
--- a/colorama/winterm.py   Sun May 27 10:09:37 2012 +0100
+++ b/colorama/winterm.py   Tue Dec 11 01:23:44 2012 +0100
@@ -58,6 +58,12 @@
         self._style = style
         self.set_console(on_stderr=on_stderr)

+    def style_reverse_vid(self, style=None, on_stderr=False):
+        # Reverse-video doesn't seem to work on windows, but we can simulate it:
+        # simply flip the current foreground and background colors.
+        self._fore, self._back = self._back, self._fore
+        self.set_console(on_stderr=on_stderr)
+
     def set_console(self, attrs=None, on_stderr=False):
         if attrs is None:
             attrs = self.get_attrs()
urosjarc commented 9 years ago

I really want to ask why UNDERLINE is not implemented yet?

wiggin15 commented 9 years ago

This patch isn't merged because Windows doesn't support these modes.

urosjarc commented 9 years ago

Hm... I suggest to add something like Fore.linux.UNDERLINE and Fore.win.BLINK, if there is something that is not supported on WIN, this doesn't mean that doesn't exist on UNIX. Wouldn't you agree?

tartley commented 9 years ago

Colorama's purpose is to make Windows terminals behave like a UNIXy one. If you just want to emit ANSI codes to control your UNIXy terminal, I'd recommend using something like termcolor instead of Colorama.

mxmerz commented 8 years ago

I would like to see support for ANSI style 7 (REVERSEVID in diff above) as well.

luzpaz commented 8 years ago

+1 to please make exceptions for platforms that support underline

MinchinWeb commented 8 years ago

The 1511 update to Windows 10 now supports underline and reverse text.

reverse-text

kapsh commented 7 years ago

Hello, I would like to use underlined style too.

LitoMore commented 7 years ago

Different results will be obtained in different terminal emulators.

FYI below,

angelof-exe commented 1 month ago

In the end, these styles didn't come?