nghung270192 / colorama

Automatically exported from code.google.com/p/colorama
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Code for some extra styles: u #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I wrote some monkeypatching 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.

You can use my code as follows:

import monkeypatched_colorama as colorama

and then work as usual.

Maybe it is useful.

import colorama
import colorama.ansi
import colorama.winterm
import colorama.ansitowin32

# patch in extra ansi styles
colorama.ansi.AnsiStyle.ITALIC = 3
colorama.ansi.AnsiStyle.UNDERLINED = 4
colorama.ansi.AnsiStyle.BLINK = 5
colorama.ansi.AnsiStyle.REVERSEVID = 7
colorama.ansi.Style = colorama.ansi.AnsiCodes(colorama.ansi.AnsiStyle)
colorama.Style = colorama.ansi.Style

# patch windows term
if hasattr(colorama.ansitowin32, "winterm"):
    colorama.winterm.WinStyle.REVERSEVID = 0x4000
    colorama.winterm.WinStyle.UNDERLINED = 0x8000

    class MonkeypatchedAnsiToWin32(colorama.ansitowin32.AnsiToWin32):
        def get_win32_calls(self):
            result = super(MonkeypatchedAnsiToWin32, self).get_win32_calls() or {}
            result[colorama.ansi.AnsiStyle.UNDERLINED] = (colorama.ansitowin32.winterm.style, colorama.winterm.WinStyle.UNDERLINED)
            #result[colorama.ansi.AnsiStyle.REVERSEVID] = (colorama.ansitowin32.winterm.style, colorama.winterm.WinStyle.REVERSEVID)
            result[colorama.ansi.AnsiStyle.REVERSEVID] = (self.style_reverse_vid, )
            return result
        def style_reverse_vid(self, style=None, on_stderr=False):
            # REVERSEVID console style doesn't seem to work on windows, so simulate it:
            # Get the current style settings, flip foreground and background colors.
            term = colorama.ansitowin32.winterm
            term._fore, term._back = term._back, term._fore
            term.set_console(on_stderr=on_stderr)

    import colorama.initialise
    colorama.ansitowin32.AnsiToWin32 = MonkeypatchedAnsiToWin32
    colorama.initialise.AnsiToWin32 = colorama.ansitowin32.AnsiToWin32
    colorama.AnsiToWin32 = colorama.ansitowin32.AnsiToWin32

from colorama import *

Original issue reported on code.google.com by irmendejong on 7 Dec 2012 at 9:00

GoogleCodeExporter commented 9 years ago
Sorry about the typo in the title field.

Original comment by irmendejong on 7 Dec 2012 at 9:03

GoogleCodeExporter commented 9 years ago
Hey,

Thanks really brilliant, thanks for that.

Instead of sending your code though, would you be interested in checking out 
colorama, making the changes to colorama itself, and submitting a patch or pull 
request? Then we could integrate your code right into the project!

If underline doesn't work then I'd probably not integrate that into the master 
branch, so you might want to just do reverse, or at the very least do reverse 
and underline as separate commits (or groups of commits.) Then we could merge 
each feature into the project (or not) separately.

Don't forget to update the tests to cover your new code.

No worries if you don't have time, but much appreciated if you do.

Original comment by tart...@gmail.com on 10 Dec 2012 at 11:05

GoogleCodeExporter commented 9 years ago
The underlined style *does* work on linux and mac os though.
The blink style works on mac os but not on linux (may depend on the terminal 
that is used).

Underlined and blink both don't work on windows console. Reversevid only works 
using the emulation.

Will have a look on how much work it is to provide a true patch instead of 
above monkeypatching code

Original comment by irmendejong on 10 Dec 2012 at 11:48

GoogleCodeExporter commented 9 years ago
Right, that was pretty easy. Please find a patch attached. 

(I've removed the ITALIC style because that one seems to be supported nowhere)

Original comment by irmendejong on 11 Dec 2012 at 12:25

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks. Remember that Colorama does precisely nothing on platforms other than 
Windows - all effects are purely up to the terminal.

I'll check it out today.

Original comment by tart...@gmail.com on 11 Dec 2012 at 7:55

GoogleCodeExporter commented 9 years ago

Original comment by tart...@gmail.com on 20 Apr 2014 at 9:50

GoogleCodeExporter commented 9 years ago
Migrated to https://github.com/tartley/colorama/issues/39
Closing here as 'duplicate'

Original comment by tart...@gmail.com on 16 Feb 2015 at 12:11