Skip to main content

Print in terminal with colors using python?


I want to print in the terminal with colors. How can I do that in python?



Another questions what is the best character that when it is printed it look like a box [brick]?



I want to print colored blocks, it is part of game :)


Source: Tips4allCCNA FINAL EXAM

Comments

  1. This somewhat depends on what platform you are on. The most common way to do this is by printing ANSI escape sequences. For a simple example, here's some python code from the blender build scripts:

    class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
    self.HEADER = ''
    self.OKBLUE = ''
    self.OKGREEN = ''
    self.WARNING = ''
    self.FAIL = ''
    self.ENDC = ''


    To use code like this, you can do something like

    print bcolors.WARNING + "Warning: No active frommets remain. Continue?"
    + bcolors.ENDC


    This will work on unix, linux including macOS, and window (provided you enable ansi.sys). There are ansi codes for setting the color, moving the cursor, and more.

    If you are going to get complicated with this (and it sounds like you are if you are writing a game), you should look into the "curses" module, which handles a lot of the complicated parts of this for you. The Python Curses HowTO is a good introduction.

    If you are not using extended ASCII (i.e. not on a PC), you are stuck with the ascii characters below 127, and '#' or '@' is probably your best bet for a block. If you can ensure your terminal is using a IBM extended ascii character set, you have many more options. Characters 176, 177, 178 and 219 are the "block characters".

    Some modern text-based programs, such as "Dwarf Fortress", emulate text mode in a graphical mode, and use images of the classic PC font. You can find some of these bitmaps that you can use on the Dwarf Fortress Wiki see (user-made tilesets).

    The Text Mode Demo Contest has more resources for doing graphics in text mode.

    Hmm.. I think got a little carried away on this answer. I am in the midst of planning an epic text-based adventure game, though. Good luck with your colored text!

    ReplyDelete
  2. I'm surprised no one has mentioned the Python termcolor module. Usage is pretty simple:

    from termcolor import colored

    print colored('hello', 'red'), colored('world', 'green')


    It may not be sophisticated enough, however, for game programming and the "colored blocks" that you want to do...

    ReplyDelete
  3. You want to learn about ANSI escape sequences. Here's a brief example:

    CSI="\x1B["
    reset=CSI+"m"
    print CSI+"31;40m" + "Colored Text" + CSI + "0m"


    For more info see http://en.wikipedia.org/wiki/ANSI_escape_code

    For a block character, try a unicode character like \u2588:

    print u"\u2588"


    Putting it all together:

    print CSI+"31;40m" + u"\u2588" + CSI + "0m"

    ReplyDelete
  4. My favorite way is with the Blessings library (full disclosure: I wrote it). For example:

    from blessings import Terminal

    t = Terminal()
    print t.red('This is red.')
    print t.bold_bright_red_on_black('Bright red on black')


    To print colored bricks, the most reliable way is to print spaces with background colors. I use this technique to draw the progress bar in nose-progressive:

    print t.on_green(' ')


    You can print in specific locations as well:

    with t.location(0, 5):
    print t.on_yellow(' ')


    If you have to muck with other terminal capabilities in the course of your game, you can do that as well. You can use Python's standard string formatting to keep it readable:

    print '{t.clear_eol}You just cleared a {t.bold}whole{t.normal} line!'.format(t=t)


    The nice thing about Blessings is that it does its best to work on all sorts of terminals, not just the (overwhelmingly common) ANSI-color ones. It also keeps unreadable escape sequences out of your code while remaining concise to use. Have fun!

    ReplyDelete
  5. On Windows you can use module 'win32console' (available in some Python distributions) or module 'ctypes' (Python 2.5 and up) to access the Win32 API.

    To see complete code that supports both ways, see the color console reporting code from Testoob.

    ctypes example:

    import ctypes

    # Constants from the Windows API
    STD_OUTPUT_HANDLE = -11
    FOREGROUND_RED = 0x0004 # text color contains red.

    def get_csbi_attributes(handle):
    # Based on IPython's winconsole.py, written by Alexander Belchenko
    import struct
    csbi = ctypes.create_string_buffer(22)
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
    assert res

    (bufx, bufy, curx, cury, wattr,
    left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
    return wattr


    handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    reset = get_csbi_attributes(handle)

    ctypes.windll.kernel32.SetConsoleTextAttribute(handle, FOREGROUND_RED)
    print "Cherry on top"
    ctypes.windll.kernel32.SetConsoleTextAttribute(handle, reset)

    ReplyDelete
  6. For windows you cannot print to console with colors unless your using the win32api.

    For linux its as simple as using print, with the escape sequences outlined here:

    Colors

    For the characther to print like a box, it really depends on what font you are using for the console window. The pound symbol works well, but it depends on the font:

    #

    ReplyDelete
  7. Here's a curses example:

    import curses

    def main(stdscr):
    stdscr.clear()
    if curses.has_colors():
    for i in xrange(1, curses.COLORS):
    curses.init_pair(i, i, curses.COLOR_BLACK)
    stdscr.addstr("COLOR %d! " % i, curses.color_pair(i))
    stdscr.addstr("BOLD! ", curses.color_pair(i) | curses.A_BOLD)
    stdscr.addstr("STANDOUT! ", curses.color_pair(i) | curses.A_STANDOUT)
    stdscr.addstr("UNDERLINE! ", curses.color_pair(i) | curses.A_UNDERLINE)
    stdscr.addstr("BLINK! ", curses.color_pair(i) | curses.A_BLINK)
    stdscr.addstr("DIM! ", curses.color_pair(i) | curses.A_DIM)
    stdscr.addstr("REVERSE! ", curses.color_pair(i) | curses.A_REVERSE)
    stdscr.refresh()
    stdscr.getch()

    if __name__ == '__main__':
    print "init..."
    curses.wrapper(main)

    ReplyDelete
  8. If you are programming a game you may would like to change the background color and use spaces only :)

    you may try something like

    print " "+ "\033[01;41m" + " " +"\033[01;46m" + " " + "\033[01;42m"

    ReplyDelete
  9. For teh Google: now there is CLINT.

    from clint.textui import colored
    print colored.red('some warning message')
    print colored.green('nicely done!')


    Get it from GitHub.

    ReplyDelete
  10. I wrote a simple module, available at:
    http://pypi.python.org/pypi/colorconsole

    It works with Windows, Mac OS X and Linux.
    It uses ANSI for Linux and Mac, but native calls to console functions on Windows.
    You have colors, cursor positioning and keyboard input. It is not a replacement for curses, but can be very useful if you need to use in simple scripts or ASCII games.

    ReplyDelete
  11. For the characters

    Your terminal most probably uses Unicode (typically UTF-8 encoded) characters, so it's only a matter of the appropriate font selection to see your favorite character. Unicode char U+2588, "Full block" is the one I would suggest you use.

    Try the following:

    import unicodedata
    fp= open("character_list", "w")
    for index in xrange(65536):
    char= unichr(index)
    try: its_name= unicodedata.name(char)
    except ValueError: its_name= "N/A"
    fp.write("%05d %04x %s %s\n" % (index, index, char.encode("UTF-8"), its_name)
    fp.close()


    Examine the file later with your favourite viewer.

    For the colors

    curses is the module you want to use. Check this tutorial.

    ReplyDelete
  12. There's also a module called WConIO that does much the same thing. Unfortunately the author will probably not be able to build a Python 2.6 version any time soon.

    ReplyDelete
  13. Go to http://en.wikipedia.org/wiki/List_of_Unicode_characters#Block_elements
    There is a list of block elements(It requires Unicode).
    Or use ascii character 24 or 26.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.