[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

What is CR? Let's create a progress bar with Python and display it on the terminal.

This is Saito from the infrastructure team.

Today, let's learn about carriage returns and write some scripts.
Next, let's create a progress bar using carriage returns in Python.

What is carriage return (CR)?

Carriage return (CR) is a special symbol represented by r. I think many people are familiar with the newline character n.

What is a carriage return?

Originally, when using a typewriter, the action of manually returning the part called the carriage that prints characters is called carriage return (CR).

The action of moving to a new line is called line feed (LF).

All operating systems in use to this day have a history of being compatible with electronic typewriters, and this 'r' appears in some form.

For example, in Windows OS, line feed is rn. This can be interpreted as "return to the original position (CR) and then move to a new line (LF)".

Unix, Mac, Linux, BSD, etc. use r and n separately based on the POSIX standard.

When you use the newline character n, the cursor is set to the beginning of a new line.

In other words, n has the role of both CR and LF. On the other hand, r has the role of CR, which returns the cursor to its original location.

Try using r

I gave examples for each OS, but even in programming languages, r and n are often used separately.

Let's actually use r in python.

$ python -c "import sys; sys.stdout.write('beyondrBn')" Beyond

- "-c" is an option for writing one-liners in python.
- Standard output is performed using sys.stdout.write, and the string is written to the buffer (cache).

Now, how the display results are determined in 'beyondrBn'.

1. Introduction string displays beyond. At this time, the cursor is after d.
2. Since there is r, the cursor moves to the beginning of b.
3. This time, cover B with b and print.
4. There is a new line because there is n.

In this way, r can write characters on the same line many times.

Create a progress bar

You can use the above properties to create a progress bar that runs on the command line. Below is a progress bar written in python.

import sys, time def prog_bar(length=100): for i in range(length): sys.stdout.write('#'*i + 'r') sys.stdout.flush() time.sleep(0.01) for i in range(4): prog_bar()

sys.stdout.write is cached before displaying on the terminal.
(Actually, print() uses sys.stdout internally.)
By using sys.stdout.flush, cached information is deleted.

Try writing an animation

Let's also write a fun animation of the spinning bar. (The following has been confirmed to work with python3)

import time space = '.' bar = '-|/' length = 100 printset = (('{0}{1}r'.format (space*(i-length//2) if i > length//2 else '', bar[i%4]), time.sleep(0.05)) for i in range(length)) for i in printset: print(i[0], end='') print(' '*length , end='r')

It can also be used for various other purposes.
I'm also adding slot-like animation to the password generation script.
Please expand and play with it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
5,147
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author