Welcome, guest | Sign In | My Account | Store | Cart

A simple DEMO to show how to enhance Python inside a Terminal. This works on Classic AMIGAs, E-UAE, Debian Linux 6.0.0, PCLinuxOS 2009, Windows XP and Vista and WinUAE, from Python 1.4.0 to 3.3A2.

Due to a complaint about my _fun_ upload recently I decided to post this so that the MANY can now find out how to manipulate text inside a Python Terminal window so that it looks much better.

Read the code for more information...

Enjoy finding simple solutions to often very difficult problems...

Bazza...

Python, 84 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Colours.py
#
# This DEMO script prints colours and codes for Linux, Classic AMIGA and Windows Python.
#
# This is Public Domain and you may do with it as you please.
#
# Tested on standard classic AMIGA A1200(HD), E-UAE, Debian Linux, Windows XP and Vista,
# and WinUAE from Python 1.4.0 to 3.3A2.
#
# This shows how to enhance text printouts to the screen for better presentation.
# Windows is limited to a complete switch of the whole window to only foreground
# and background. The AMIGA and derivatives are limited to ONLY the first eight
# WorkBench colours. Linux is unable to do ITALICS reliably on various terminal
# programs using the escape mode method...
#
# Because of a fun program I uploaded that was voted down I decided to upload this
# because although some may know about it, MANY won't! I will say no more about the fun
# program. This does NOT do anything to your personal Terminal setups except display
# various modes and colours at the flick of a simple escape sequence.
#
# Copy/drag this file to the Lib(rary) directory/folder/drawer, rename to Colours.py
# and run from the Python Prompt using:-
#
# >>> import Colours<RETURN/ENTER>
#
# And away you go...
#
# $VER: Colours.py_Version_0.00.10_(C)2012_B.Walker_G0LCU.
#
# Enjoy finding simple solutions to often very difficult problems...

# The only, (standard), imports required for this DEMO...
import sys
import os
import time

print("\nColours inside a Linux Terminal, Classic AMIGA CLI")
print("or Windows Command Prompt using Python.")

# The code is self explanatory...
if sys.platform=="linux2" or sys.platform=="darwin":
	print("\n\033[0mThis line is your startup defaults...")
	print("\n      \033[0;37;40mNormal Colors.\033[0m       \033[1;37;40mBright, Bold, Foregrond Colors.\033[0m\n")
	print("  \033[0;30;47m Black      \033[0m 0;30;47m      \033[1;30;40m Dark Gray      \033[0m 1;30;40m")
	print("  \033[0;31;47m Red        \033[0m 0;31;47m      \033[1;31;40m Bright Red     \033[0m 1;31;40m")
	print("  \033[0;32;47m Green      \033[0m 0;32;47m      \033[1;32;40m Bright Green   \033[0m 1;32;40m")
	print("  \033[0;33;47m Brown      \033[0m 0;33;47m      \033[1;33;40m Yellow         \033[0m 1;33;40m")
	print("  \033[0;34;47m Blue       \033[0m 0;34;47m      \033[1;34;40m Bright Blue    \033[0m 1;34;40m")
	print("  \033[0;35;47m Magenta    \033[0m 0;35;47m      \033[1;35;40m Bright Magenta \033[0m 1;35;40m")
	print("  \033[0;36;47m Cyan       \033[0m 0;36;47m      \033[1;36;40m Bright Cyan    \033[0m 1;36;40m")
	print("  \033[0;37;40m Light Grey \033[0m 0;37;40m      \033[1;37;40m White          \033[0m 1;37;40m")
	print("\n\033[0;4;37;40mUnderlined text...\033[0m")
	print("\n\033[1;4;37;40mBright, bold, underlined text...\033[0m")
	print("\n\033[0mFinally reset the colours back to your startup defaults...\nPress Ctrl-C to Quit:- ")

if sys.platform=="amiga":
	print("\n\033[0mThis line is your startup defaults...")
	print("\n\033[0mThe first eight WorkBench colours only! (Assume default bootup colours.)\n")
	print("  \033[0;30;41m White on black 0;30;41m, \033[0;32;41mbright white on black, 0;32;41m... \033[0m")
	print("  \033[1;30;43m Bold white on user background, 1;30;43m... \033[0m")
	print("  \033[0;3;32;44m Normal, italic, bright, white on user background, 0;3;32;44m... \033[0m")
	print("  \033[1;3;32;45m Bold, italic, bright, white on user background, 1;3;32;45m... \033[0m")
	print("  \033[0;4;31;46m Normal, underlined, black on user background, 0;4;31;46m... \033[0m")
	print("  \033[1;3;4;31;47m Bold, italics, underlined, black on user background, 1;3;4;31;47m... \033[0m")
	print("\n\033[0mFinally reset the colours back to your startup defaults...\nPress Ctrl-C to Quit:- ")

if sys.platform=="win32":
	# Normal colours for a Command Prompt from CMD.EXE is white on black.
	os.system("COLOR 07")
	print("\nNormal Command Prompt default colours, white on black...\n")
	# Hold for about 2 seconds...
	time.sleep(2)
	# This sets the whole page to green on black.
	os.system("COLOR 0A")
	print("Refer to the COLOR command for choice of colours.\nThis is green on black for about four seconds...\n")
	# Hold for about 4 seconds before bringing back to standard colours...
	time.sleep(4)
	# These are the default foreground and background colours.
	os.system("COLOR 07")
	print("Back to the default foreground and background colours...\nPress Ctrl-C to Quit:- ")	

while 1: pass
# End of Colours.py code.
# Enjoy finding simple solutions to often very difficult problems...

This code was prompted by a complaint about my fun demo upload. I expect professionals to know about this but amateurs not so, so this is the result.

Apologies if there are any typos in the comments. The code is self explanatory...

Enjoy finding simple solutions to often very difficult problems...

Bazza...

7 comments

Barry Walker (author) 12 years ago  # | flag

To the person who voted this up on 13-04-2012, you know who you are, many thanks from this mere amateur...

Again many thanks...

Bazza...

Arthur Lee 11 years, 11 months ago  # | flag

So that's it. What am I supposed to do with it?

Do I need to add \033[0;34;47m Blue \033[0m 0;34;47m around the text I want it to be blue?

If you can wrap them up, let people just add \blue text you want it to be blue here so they can get colorful result.

That would be so damn good.

Barry Walker (author) 11 years, 11 months ago  # | flag

colours == Esc[attribute;forground;backgroundm reset == Esc[0m

colours, normal green on black:-

c="\033[0;40;32m"

reset to default Terminal mode:-

r="\033[om"

Test text for line below...

x="green"

For all versions of Python in Linux...

print("The word "+c+x+r+" will be green on black..."

;o)

Bazza...

Barry Walker (author) 11 years, 11 months ago  # | flag

Dunno how the text came out like that but "r" is wrong...

r="\033[0m"

Bazza...

Barry Walker (author) 11 years, 11 months ago  # | flag

Also the print routine is wrong... :/

print("The word "+c+x+r+" will be green on black...")

is correct...

Maybe I ought to go back to bed and get out of the other side... ;oO

Charles Howe 11 years, 11 months ago  # | flag

The following code is a functional example of a blue user prompt that captures a normal text response, on the same line. This was tested in Python 2.7.3 on Fedora 16, and with Python 2.7.1 on OS X Lion (10.7.4).

bluestr  = "\033[1;34m"
normstr = "\033[1;m"

print bluestr + "Enter Principal Amount: " + normstr,
principal = float(raw_input())
print principal
Barry Walker (author) 11 years, 8 months ago  # | flag

21-08-2012.

Changed line 41 to add a MacBook Pro OSX 10.7.3, Python version 2.7.1...