Skip to content
Sign upLog in
← Back to Community

Styling your Console Programs to make them Unique

Profile icon
[deleted]

Styling Your Console Programs to Make Them Unique

Making console games are tough. There are a limited number of things that you can do. Visuals are tough, RPGs are really hard and require a ton of effort, and simple tool programs aren't very popular. The world requests a lot out of a developer. Recently I developed a Python Program that you can add to your project that makes writing colorful text easier. Someone suggested that I write a tutorial on formatting the console on your own and making it personal. To standardize this, my examples will be in Python, but

What Are ANSI Escape Codes?

ANSI escape codes are a way of formatting and interating with text through the console, as well as deleting printed lines and moving the cursor. It is supported by Repl.it as well as Unix systems like MacOS and Ubuntu, but not Windows sadly. Most escape codes versions start off with\u001b, including Python, Java, and Javascript. It is the specific content after this sequence at defines what is actually happening. Here are some examples.

Colors

Printing colors is quite simple. You simply type the following:

print("\u001b[36mHello World") #Prints "Hello World" in Cyan

\u001b represents an escape code, and [36m represents the color cyan.

List of Basic Colors

  • Black: \u001b[30m
  • Red: \u001b[31m
  • Green: \u001b[32m
  • Yellow: \u001b[33m
  • Blue: \u001b[34m
  • Magenta: \u001b[35m
  • Cyan: \u001b[36m
  • White: \u001b[37m
  • Reset: \u001b[0m

Because of the nature of these color codes, you can simply print it alone, and any of the text printed afterwards will be that color. This is a bit difficult to explain in words, so here is an example.

print("\u001b[32m I'm green.") print("And I'm also green!") print("\u001b[0m I just reset the formatting") print("So now I'm not green anymore.")

Make sense? The code applies until a new one is passed, for instance the reset code.
As well as there being the basic 8 colors, there are also brighter, bolder versions of each. The command prompt counts these as bolded colors, even though the width and height are still the same.
List of Brighter Colors

  • Bright Black: \u001b[30;1m
  • Bright Red: \u001b[31;1m
  • Bright Green: \u001b[32;1m
  • Bright Yellow: \u001b[33;1m
  • Bright Blue: \u001b[34;1m
  • Bright Magenta: \u001b[35;1m
  • Bright Cyan: \u001b[36;1m
  • Bright White: \u001b[37;1m
  • Reset: \u001b[0m

Note that the reset is still the same, and the difference between the normal and the bright colors is the ##;1m at the end. "M" marks the end of an escape code.
You can also do highlights.

  • Background Black: \u001b[40m
  • Background Red: \u001b[41m
  • Background Green: \u001b[42m
  • Background Yellow: \u001b[43m
  • Background Blue: \u001b[44m
  • Background Magenta: \u001b[45m
  • Background Cyan: \u001b[46m
  • Background White: \u001b[47m

Add a ;1 after the number to make it brighter. There are also ways to use the 256 color range, but that won't be covered in this tutorial.

Text Decoration

ANSI also allows for text decorations, like the following:

  • Bold: \u001b[1m
  • Italic: \u001b[3m
  • Underline: \u001b[4m
  • Reversed: \u001b[7m

These effects as well as the color and highlights can be stacked, so writing \u001b[4m\u001b[44mHi will type "Hi" underlined and highlighted in blue.

Clearing the Screen

Having a long line of text and information is quite annoying, so having the ability to clear the entire screen is extremely handy. Constantly updating visuals and animations, combined with color codes, can make an amazing game. There are two types of clearing codes.

Clear Line

\u001b[2K
Typing the code above allows you to clear a line, so you can have a constantly updating progress bar, or updating variables.

Clear Screen

\u001b[2J
Typing the code above will clear the whole screen. All errors, printed text, and overall clutter will be removed.

List of Movement Codes

  • Up: \u001b[{n}A moves cursor up by n
  • Down: \u001b[{n}B moves cursor down by n
  • Right: \u001b[{n}C moves cursor right by n
  • Left: \u001b[{n}D moves cursor left by n

  • Next Line: \u001b[{n}E moves cursor to beginning of line n lines down
  • Prev Line: \u001b[{n}F moves cursor to beginning of line n lines down

  • Set Column: \u001b[{n}G moves cursor to column n
  • Set Position: \u001b[{n};{m}H moves cursor to row n column m

  • Clear Screen: \u001b[{n}J clears the screen
    • n=0 clears from cursor until end of screen,
    • n=1 clears from cursor to beginning of screen
    • n=2 clears entire screen
  • Clear Line: \u001b[{n}K clears the current line
    • n=0 clears from cursor to end of line
    • n=1 clears from cursor to start of line
    • n=2 clears entire line

  • Save Position: \u001b[{s} saves the current cursor position
  • Save Position: \u001b[{u} restores the cursor to the last saved position

Tips and Tricks

Here are some tips and tricks for coding with escape codes. This is all going to be in Python, but it is reasonably easy to convert to other languages. Enjoy!

#Using Variables Instead of Codes #Making the Variables blue = "\u001b[34m" magenta = "\u001b[35m" cyan = "\u001b[36m" reset = "\u001b[0m" # Use print(blue + "I'm blue.") print(cyan + "Da ba de ba da by.") print(reset + "Da ba da be ba da by.") print(blue + "Da ba de ba da byyyyyyyyy") print(reset)

#How to Make a Loading Number import time, sys def loading(): print("Loading...") for i in range(0, 100): time.sleep(0.1) sys.stdout.write("\u001b[1000D" + str(i + 1) + "%") sys.stdout.flush() print() loading()

Using the Backslash \

On a completely unrelated note, you can also type \ to use quotes " inside quotes. This also works if you wish to type \ regularly.

More Info

I really recommend that you go to Wikipedia and check out their information. It is quite confusing, but if you need a specific function, this may be the place for you.
But, if I were to be completely honest, I would just play around with typing \u001b[36m and replacing "36" with various numbers.
Also, most of this information I got from Haoyi's Programming Blog. Please check it out.
Thank you

@Bookie0
for help on code blocks and the italic escape codes. You can go to his page here.
Last but not least, please upvote this tutorial. I spent a lot of time on it, and I hope you enjoy it!
[email protected]

Voters
Profile icon
KelbyRitchison
Profile icon
Bookie0
Comments
hotnewtop
Profile icon
Bookie0

italic = "\033[3m" for italic.

Also add synntax highlighting to your code blocks by adding py after the 3 backslashes `

print("yay colors") def hello(): thing = "syntax" print(thing+" highlighting is great") # comments too greeeeen
Profile icon
[deleted]

Thank you

@Bookie0

Profile icon
Bookie0

np! :)

@TEACH3R