Skip to content
Nilo Menezes edited this page Jul 18, 2022 · 2 revisions

Introduction

Colorconsole was built to provide colors on text-mode consoles on Windows, Linux and Mac OS X.

It keeps a simple interface across all platforms, allowing the same code to run unchanged.

It also supports extended xterm colors and attributes like underline and blink were supported. For Windows ANSI terminal, install conEmu and initialize with get_terminal(conEmu=True).

Standard Windows console only supports 16 colors.

For maximum compatibility, only use common functions.

Details

  • Install the package using easy_install or downloading from Google code.

  • You can use Python 2.7 (<=0.7.2) and Python 3.8 (>=0.8.0) to run this package. It should work with any Python version, but I only tested with these two.

Documentation

Color table

Number Color
0 Black
1 Blue
2 Green
3 Cyan
4 Red
5 Purple
6 Brown
7 Light gray
8 Dark gray
9 Light blue
10 Light Green
11 Light Cyan
12 Light Red
13 Light Purple
14 Yellow
15 White

The standard ANSI terminal has only 8 background colors (0 to 7). On Windows, background and foreground colors can use the full table.

xterm supports a 256 color and 24 bit colors mode.

Terminal object

A Terminal object is returned by get_terminal(). Only Windows, ConEmu and ANSI terminals are supported. Terminal type is automatically selected based on operating system. ConEmu is not used, unless running on Windows and calling get_terminal(conEmu=True).

set_color(fg = None, bk = None)

Changes the foreground (fg) and background (bg) colors.

set_title(title)

Changes the title of the console window.

cprint(fg, bg, text)

Prints a text message with the specified foreground (fg) and background (bg) colors. Used as a shortcut to set_color and print.

print_at(x,y, text)

Prints the text message starting at coordinates x,y.

clear()

Clears the screen. It uses the current background color to erase the screen. It does not reset the cursor position to 0,0.

gotoXY(x,y)

Sets the cursor position to column x, line y.

reset()

Resets all color attributes.

columns()

Returns the number of columns of the current console window. On Linux and Mac OS X, it uses COLUMNS environment variable to detect this value. If you don't export COLUMNS, this function will always return 80.

lines()

Returns the number of lines of the current console window. On Linux and Mac OS X, it uses LINES environment variable to detect this value. If you don't export COLUMNS, this function will always return 24.

move_left()

Moves the cursor one position to the left.

move_right()

Moves the cursor one position to the right.

move_up()

Moves the cursor one line up.

move_down()

Moves the cursor one line down.

putch(ch)

Writes a single char to the screen. On Linux/Mac OS, you must be using unbuffered mode. See enable_unbuffered_input_mode().

getch()

Return a single char from the terminal. Your program you wait for a single key press. This method returns the char of the key pressed. On Linux/Mac OS, you must be using unbuffered mode. See enable_unbuffered_input_mode().

getche()

As getch, but displays the pressed key char on the screen. On Linux/Mac OS, you must be using unbuffered mode. See enable_unbuffered_input_mode().

kbhit()

Returns True if any key was pressed, False otherwise. The key can be read with a call to getch. This function does not block. On Linux/Mac OS, you must be using unbuffered mode. See enable_unbuffered_input_mode().

enable_unbuffered_input_mode()

On Linux and Mac OS X, sets the terminal to unbuffered input mode. This mode is char oriented. You must call this method before trying to use single key press methods, like: getch(), getche(), kbhit()

This call is ignored on Windows.

restore_buffered_mode()

On Linux and Mac OS X, sets the terminal to buffered input mode. The buffered input mode is the default one and it is line oriented. Your program will not receive any input on stdin until enter is pressed. If you called enable_unbuffered_input_mode(), call restore_buffered_mode() in the end of your code.

This call is ignored on Windows.

xterm24bit_set_fg_color(r,g,b)

Only works with xterm. Sets the foreground color to RGB value passed on r,g,b. r,g and b can be an integer number from 0 up to 255.

xterm24bit_set_bk_color(r,g,b)

Only works with xterm. Sets the background color to RGB value passed on r,g,b. r,g and b can be an integer number from 0 up to 255.

xterm256_set_fg_color(color)

Only works on xterm and programs that support xterm256 color mode. On Windows, you can use conEmu. Sets the foreground color to one of 256 possible values from xterm palette.

xterm256_set_bk_color(color)

Only works on xterm and programs that support xterm256 color mode. On Windows, you can use conEmu. Sets the background color to one of 256 possible values from xterm palette.

reverse()

Only works on ANSI terminals. Set video to reverse mode. Background and Foreground colors are inversed.

invisible()

Only works on ANSI terminals. Enable invisible mode.

blink()

Only works on some ANSI terminals. Following text will blink.

underline()

Only works on some ANSI terminals. Following text will be underlined.

reset_colors()

Sets colors to its normal background and foreground values. On Windows, it is the same as reset().

default_foreground()

Only on ANSI terminals, sets the foreground color to its default value.

default_background()

Only on ANSI terminals, sets the background color to its default value.

Example 1

Windows console:

On Linux:

alt text

import math
from colorconsole import terminal

screen = terminal.get_terminal(conEmu=False)
screen.set_title("Example 1")
screen.set_color(terminal.colors["WHITE"],terminal.colors["BLUE"])
screen.clear()

PI = math.pi
step = (2.0*PI)/screen.columns()

for x in range(screen.columns()):
        screen.set_color(terminal.colors["YELLOW"], terminal.colors['RED'])
        screen.print_at(x,11, "-")
        screen.set_color(2,7)
        screen.print_at(x, 11+math.sin(x*step)*10, "S"  )
        screen.set_color(1,2)
        screen.print_at(x, 11+math.cos(x*step)*10, "C"  )

screen.reset_colors()
# Waits for a single key touch before ending.
screen.getch()

Example 2

Windows console:

On Linux:

alt text

from __future__ import print_function
from colorconsole import terminal

screen = terminal.get_terminal(conEmu=False)
screen.clear()
screen.set_title("Example 2")

screen.print_at(0,0,"Color table")

if screen.type == "WIN":
    for x in range(16):
        screen.gotoXY(0,x+1)
        screen.cprint(15,0, "%2d"%x)
        screen.gotoXY(3,x+1)
        screen.cprint(x,x, " " * 20)
else:
    screen.gotoXY(0,3)
    for x in range(256):
        screen.xterm256_set_fg_color(x)
        print("%02x" % x, end="" if (x+1)%32!=0 else "\n")

    screen.gotoXY(0,12)
    for x in range(256):
        screen.xterm256_set_bk_color(x)
        print("%02x" % x, end="" if (x+1)%32!=0 else "\n")
    print()
screen.reset_colors()

# Waits for a single key touch before ending.
screen.getch()

= Example 3 For XTerm only =

On Linux xterm (required for 24 bit color support): alt text

from __future__ import print_function
from colorconsole import terminal

screen = terminal.get_terminal()
screen.clear()
screen.set_title("Example 3")

if screen.type == "WIN":
    raise RuntimeError("24 bit mode not supported on Windows.")

screen.underline()
screen.blink()
screen.print_at(0,0,"Color table 24 bits")
screen.reset()

c=0
screen.gotoXY(0,3)
for r in range(256):
    for g in range(256):
        for b in range(256):
            screen.xterm24bit_set_bk_color(r,g,b)
            print(" ", end="")
            c+=1
            if (c+1)%64==0:
                print()
            if (c+1)%1024==0:
                screen.gotoXY(0,3)
print()
screen.reset_colors()
# Waits for a single key touch before ending.
screen.getch()