That sounds like a nice idea, try it out and see what you make of it. (It may have been done before but probably not as a standalone module as it doesn't require that much code)<br><br><div class="gmail_quote">On Sat, Apr 17, 2010 at 6:52 AM, Jonathan Hartley <span dir="ltr"><<a href="mailto:tartley@tartley.com">tartley@tartley.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">On Apr 16, 5:59 pm, Lie Ryan <<a href="mailto:lie.1...@gmail.com">lie.1...@gmail.com</a>> wrote:<br>

> On 04/16/10 19:28, Jonathan Hartley wrote:<br>
><br>
> > I'm playing with ideas of what API to expose. My favourite one is to<br>
> > simply embed ANSI codes in the stream to be printed. Then this will<br>
> > work as-is on Mac and *nix. To make it work on Windows, printing could<br>
> > be done to a file0-like object which wraps stdout:<br>
><br>
> The problem with that is you're simply reinventing ANSI.SYS device driver.<br>
><br>
> An alternative API is you could override .__add__(), like so (completely<br>
> untested):<br>
><br>
> class Color(object):<br>
>    def __init__(self, color):<br>
>        self.color =  map_the_color(color)<br>
>        self.string = ""<br>
>    def __add__(self, string):<br>
>        self.string += string<br>
>        return self<br>
>    def __str__(self):<br>
>        if terminal_can_do_ansi_color:<br>
>            return ansicolorescape(self.string, self.color)<br>
>        elif windows:<br>
>            syscalltocolor(self.color)<br>
>            print self.string<br>
>            syscalltocolor(reset the color)<br>
>            return ""<br>
><br>
> GREEN = Color('green')<br>
> print GREEN + "Great" + "Good"<br>
><br>
> you can even go a bit further and allow chained calls (again, completely<br>
> untested, but you get the idea):<br>
><br>
> class Color(object):<br>
>    def __init__(self, color):<br>
>        self.color =  map_the_color(color)<br>
>        self.stack = []<br>
>    def __add__(self, string):<br>
>        if isinstance(string, Color):<br>
>            # not a string, chain the calls<br>
>            self.stack.append((string.color, []]))<br>
>        else:<br>
>            # a string,<br>
>            self.stack[-1][1].append(string)<br>
>        return self<br>
>    def __radd__(self, string):<br>
>        self.stack.append([self.default, string])<br>
>        return self<br>
><br>
>    def __str__(self):<br>
>        if ansi_capable:<br>
>            return colorescape(format, string)<br>
>        elif windows:<br>
>            for format, string in self.stack:<br>
>                syscalltocolor(color)<br>
>                print string<br>
>                return ""<br>
><br>
> GREEN = Color('green')<br>
> RED = Color('red')<br>
><br>
> print "Fairly" + GREEN + "Great" + RED + "Poor"<br>
><br>
> or something like that, and you will have an API that works<br>
> transparently on all platforms. The downside is that you cannot call<br>
> str(GREEN + "foo") on windows.<br>
<br>
<br>
<br>
</div></div>Hey Lie,<br>
<br>
Thanks heaps for the reply!<br>
<div class="im"><br>
>> The problem with that is you're simply reinventing ANSI.SYS device driver.<br>
<br>
</div>I don't see that as a problem - in fact I think it's exactly my<br>
goal! :-)<br>
<br>
The difference is that the ANSI driver requires installation and a<br>
reboot on the end-user's computer, which is a fiddly and intrusive<br>
thing for a Python developer to achieve. Whereas doing the same job in<br>
a Python module is easy to use for the Python developer - they just<br>
import the module, maybe call an 'init()' function, and then the ANSI<br>
functionality works on all platforms.<br>
<br>
Your ideas about generating and chaining the ANSI code strings are<br>
great. I worry though, about intermingling the code that generates<br>
ANSI escape sequences with the code which makes them work on Windows.<br>
The problem is that then, only applications which use your ANSI-<br>
generation library will work on Windows. Whereas if these two things<br>
are kept separate, then applications which use any other ANSI-<br>
generation techniques, such as using 'termcolor', or manaully printing<br>
raw ANSI sequences, these can also all work on Windows too, simply by<br>
adding an import and an 'init()' call to the start of the application.<br>
<br>
Am I making sense? Many thanks for your thoughts.<br>
<font color="#888888"><br>
  Jonathan<br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>