Skip to main content

Use Carriage Return for Simple Progress Bar in Text Applications

Since most systems, including Unix, use LF(\n, 0x0A) as a newline character, using of CR(\r, 0x0D) is quite rare. One of the few cases where CR is used in modern computers is when creating progress bars in text applications. Using CR allows for a simple implementation of progress bars in terminals.

The code above draws a progress bar with # and ' '(space). For convenience, I fixed the progress bar's length at 20 characters, adding one # for every 5% increase in progress. When using CR to draw a progress bar like this, there are three points to consider.

The first point is to draw the progress bar on stderr instead of stdout. One of the significant differences between stdout and stderr is that stdout buffers output rather than immediately displaying it on the screen. Typically, stdout buffers output until it encounters a newline character. Therefore, if you print a progress bar without a newline character on stdout, the screen will not be updated until the progress bar is complete. If you need to use stdout for some reason, you must call the flush(stdout) function explicitly.

The second point is that the length of the string to be printed next should not be shorter than the previously printed string. CR only moves the cursor to the beginning of the line; it does not erase the current line. While this is not a concern for fixed-length progress bars, more complex control sequences may be needed to erase the current line or use space characters to erase the remaining characters when repeatedly outputting various strings on the same line.

The last point is not to output other strings while updating the progress bar. The code above assumes that only the progress bar will be drawn until it is complete. If other strings are printed in the middle, the result will differ entirely from what was intended.

Using control sequences allows for a perfect implementation that repeatedly modifies the same line, like the progress bar. However, using control sequences for simple programs like data processing can be overkill. Using CR alone can provide a simple and efficient implementation for displaying progress in simple programs.

Comments

Popular posts from this blog

Understanding Aspect-Oriented Programming with Python Examples

Object-Oriented Programming (OOP) manages code by grouping it into independent modules known as objects, emphasizing the crucial principle of Separation of Concerns. This means each object should focus on its specific responsibilities. However, real-world applications often feature functionalities that are common across multiple objects or modules, such as logging, security, transaction management, and performance monitoring. These functionalities are called Cross-cutting Concerns. Challenge of Cross-Cutting Concerns Scattering These cross-cutting concerns, when handled solely with OOP, create two major problems. The first problem is Scattering , which is when code for a specific functionality is spread across multiple places through copying and pasting. For instance, imagine adding user permission checks and logging code to every function. The same logging and permission checking code would repeatedly appear within each method. Tangling The other issue is Tangling . This refe...

Cursor Movement with CSI Sequences

Code Abbr Name CSI # A CUU CUrsor Up CSI # B CUD CUrsor Down CSI # C CUF CUrsor Forward CSI # D CUB CUrsor Backward CSI # E CNL CUrsor Next Line CSI # F CPL CUrsor Previous Line CSI # I CHT Cursor Horizontal forward Tabulation CSI # Z CBT Cursor Backward Tabulation CSI # G CHA Cursor Horizontal Absolute CSI # ; # H CUP CUrsor Position Today, we will continue from the  previous article to explore how to move the cursor using CSI sequences. The types of CSI sequences for moving the cursor can be summarized as follows. CUU, CUD, CUF, CUB These are the abbreviations for CUrsor Up, CUrsor Down, CUrsor Forward, and CUrsor Backward; as the names suggest, they move the cursor up, down, forward, and backward. They take a single number as an argument; if the argument is omitted, it is treated as 1. Thus, 0x1b[A is equivalent to 0x1b[1A . In this case, CUF and CUB move only within the same line. In other words, CUB rece...

Iterator Adapters in Rust

An Iterator that takes another iterator and returns a new one is called an iterator adapter . The name "adapter" comes from one of the GoF's design patterns, the adapter pattern . However, in reality, it corresponds more to the decorator pattern , so if you pay too much attention to the name, you might get confused about its purpose. So it's better not to worry too much about the name. Enough complaining about the name, what does an iterator adapter do? An iterator adapter adds a task to be performed when the iterator iterates. This will be easier to understand when you see an example. The map function is one of the famous adapters. The iterator returned by the map function for those who have used functional languages iterates over new values transformed from the original values. Besides, various adapters are already implemented in the standard library. Among them, the most frequently used are those that are convenient to use with loops. Examples include the ...