Meseret Kassaye
4 min readMay 4, 2022

--

Customize your command line interface in Python

Command line
Photo by Gabriel Heinzer on Unsplash

Everyone is used to programs printing out output in a terminal that scrolls as new text appears, but that’s not all you can do: your program can color your text, move the cursor up, down, left or right, or clear portions of the screen if you are going to re-print them later.

This is what lets programs like Git implement its dynamic progress indicators, and Vim or Bash implement their editors that let you modify already-displayed text without scrolling the terminal.

There are libraries like Readline, JLine, or the Python Prompt Toolkit that help you do this in various programming languages, but you can also do it yourself. This post will explore the basics of how you can control the terminal from any command-line program, with examples in Python, and how your own code can directly make use of all the special features the terminal has to offer.

The way that most programs interact with the Unix terminal is through ANSI escape codes. These are special codes that your program can print in order to give the terminal instructions. Various terminals support different subsets of these codes, and it’s difficult to find a “authoritative” list of what every code does. Wikipedia has a reasonable listing of them, as do many other sites.

Nevertheless, it’s possible to write programs that make use of ANSI escape codes, and at least will work on common Unix systems like Ubuntu or OS-X (though not Windows, which I won’t cover here and is its own adventure!). This post will explore the basics of what Ansi escape codes exist, and demonstrate how to use them to write your own interactive command-line from first principles

Rich text

The most basic Ansi escape codes are those involved in rendering text. These let you add decorations like Colors, Background Colors or other Decorations to your printed text, but don’t do anything fancy. The text you print will still end up at the bottom of the terminal, and still make your terminal scroll, just now it will be colored text instead of the default black/white color scheme your terminal has

Colors

The most basic thing you can do to your text is to color it. The Ansi colors all look like

  • Red: \u001b[31m
  • Reset: \u001b[0m

This \u001b character is the special character that starts off most Ansi escapes; most languages allow this syntax for representing special characters, e.g. Java, Python and Javascript all allow the \u001b syntax.

For example here is printing the string "Hello World", but red:

Note how we need to prefix the string with u i.e. u"..." in order for this to work in Python 2.7.10. This is not necessary in Python 3 or in other languages.

See how the red color, starting from the printed Hello World, ends up spilling into the >>> prompt. In fact, any code we type into this prompt will also be colored red, as will any subsequent output! That is how Ansi colors work: once you print out the special code enabling a color, the color persists forever until someone else prints out the code for a different color, or prints out the Reset code to disable it.

We can disable it by printing the Reset code above:

And we can see the prompt turns back white. In general, you should always remember to end any colored string you’re printing with a Reset, to make sure you don’t accidentally

To avoid this, we need to make sure we end our colored-string with the Reset code:

8 Colors

We have seen how Red and Reset work. The most basic terminals have a set of 8 different 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

Which we can demonstrate by printing one letter of each color, followed by a Reset:

Next we will see for background color and progress bar

--

--

Meseret Kassaye

Web developer, Front-end, In love with ReactJS/TypeScript/JavaScript/NestJS/ and Python