1

I have always thought that there is no possible formatting on the command line, as everything I have read says.

However, I recently discovered that pywikipedia (a python bot framework for automatically editing wikipedia-style wikis) can output text to the command line (the normal windows cmd.exe) in different colours!

This is the python syntax:

import wikipedia
wikipedia.output(u"\03{lightpurple}"+s+"\03{default}")

You have to use wikipedia.output() (or pywikibot.output()) but not just print.

The online pywikipedia repository (around line 7990) gives a short explanation:

text can contain special sequences to create colored output. These
consist of the escape character \03 and the color name in curly braces,
e. g. \03{lightpurple}. \03{default} resets the color.

I think it is probably to do with this line:

ui.output(text, toStdout = toStdout)

But I can't find any reference to a ui class.

So how does Pywikipedia manage it?

the
  • 21,007
  • 11
  • 68
  • 101
ACarter
  • 5,688
  • 9
  • 39
  • 56
  • 1
    Here's a tool that can do it - the source code might be informative: http://pypi.python.org/pypi/clint/ – Thomas K May 28 '12 at 18:30
  • 1
    Where did you read that? Nearly every terminal is capable of color output. – Wooble May 28 '12 at 19:19
  • @Thomas, Thanks for that. Although I haven't found the definate answer, there are some links and stuff in there that will help. – ACarter May 28 '12 at 19:39
  • @Wooble, so I have realised... :) – ACarter May 28 '12 at 19:39
  • possible duplicate of [How can I color Python logging output?](http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output) – Peter O. Nov 07 '12 at 20:37

2 Answers2

1

I don't know if you can use the ANSI Code on Windows.
But in Python you could write make it like that :

 >>> print "\033[0;32m"+ "Green" +"\033[0m"
 Green

I saw it here.

Zulu
  • 8,765
  • 9
  • 49
  • 56
0

I think it is probably to do with this line:

ui.output(text, toStdout = toStdout)

But I can't find any reference to a ui class.

How does Pywikibot manage colors on the command line

Indeed this is tricky.

You have found the color codes already like \03{lightblue} an \03{default}; all of them can be found here. There is also a function color_format() which automatically handles the escape sequence. The UI class has the private _print() method which handles all color stuff and delegates the hardware sequence to the encounter_color() method. There are implementations for Windows and Linux yet.

But you are right, there is no direct reference to terminal interface UI class.

There is a function set_interface() in bot module which assigns the UI class to the ui userinterface used by Pywikibot. The 'terminal' module for userinterfaces_terminal_interface is set inside config.py which is the Pywikibot configuarion file which can be overridden by a user_config.py. There is also a buffer_interface mostly used by tests. Other interfaces from older branch like cgi, tkinter, wxpython aren't supported currently.

xqt
  • 280
  • 1
  • 11