Skip to main content

Posts

Showing posts with the label carriage return

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 unti...