How can we get legacy behavior for printing line-feed `\n` and carriage-return `\r` to the console?

Viewed 24

Command-line interfaces, Linux shells, etc... can all be modeled as a table of ASCII characters having many rows and many columns.

+---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|   | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
+---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 0 |    | f  | o  | r  |    | k  |    | i  | n  |    | r  | a  | n  | g  | e  | (  |  0 |  , |    |  1 |  0 | )  | :  |
+---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 1 |    |    |    |    | p  | r  | i  | n  | t  | (  | k  | )  |    |    |    |    |    |    |    |    |    |    |    |
+---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 2 |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |    |
+---+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

Some years before I was born, printing \n and \r caused the following to happen:

Character Column/Row Index Updates Left/Right Behavior
\n set column-index to 0 move write-head all-the-way left
\r add 1 to the row-index move write-head down one position

How can we force python to print line-feed \n and carriage \r the same way that these characters were printed more than 30 years ago?

Below I have some code, and also, I have the desired console output.

import sys

sys.stdout.write("---------\n\r")

sys.stdout.write("hello world\n")

# OVER-WRITE "hello world\n"
sys.stdout.write("AAAAA BBBBB\r") 

# begin text in the middle of the screen, instead of at the far-left
sys.stdout.write("*************\n\r")

sys.stdout.write("$$$$$$$$$$$$\r\n")

Desired Console Output

---------
AAAAA BBBBB
           *************
$$$$$$$$$$$$

In contemporary times it is often the case any one of the following strings will set the column index to zero and increment the row=index by one:

  • "\r\n"
  • "\n"
  • "\r"
  • "\n\r"

Today, the default console output for the default shell-emulator would look like this:

---------
hello world
AAAAA BBBBB
*************
$$$$$$$$$$$$
0 Answers
Related