Learn how to use Ruby-progessbar gem: The library displays progress bars in Ruby

Tram Ho

Recently I often have to run the super huge rake task, sitting looking at the log screen out the words are also boring. I thought if there was any library to help me show the status of the process running on the screen, so I discovered this good library. Ruby-progressbar is a great option for displaying the running progress of a function, job, or task in Ruby. It has several advantages as follows:

  • is a long-standing gem and still use well from 2008 to the present
  • There is a full test suite
  • No dependencies
  • Used by lots of open source projects
  • good experience
  • There are many contributors and are maintained regularly

Let’s learn and experience it!

Setting

You can install it directly on the gem

then in the script put more require 'ruby-progressbar'

or put in Gemfile

How to use it?

Simple initialization only.

This command creates a simple progress bar starting at 0 with a maximum measurement of 100 , which will display on the screen like this:

Alternatively you may have multiple options to create a progress bar, for example:

This will output the following:

Below is a detailed description of the options you can use:

——-Option ——————–DefaultDescription
:titleProgressThe title of the progress bar
:total100The total amount of work to be completed
:starting_at0The starting point for the progress bar. When calling #reset it will return this value
:progress_mark=The notation marks the amount of progress completed
:remainder_markspaceThe notation marks the remaining progress that must be completed
:format%t: |%B|The format string is used to format the display of the progress bar
:lengthoccupies the full width possible otherwise equals 80The width of the progress bar displayed on the screen
:output$stdoutAll output will be passed to this object (standard output). Can be any object that has 4 modes .print .flush .tty? .puts

Update progress

Knowing how to create it, so how to update the progress of the bar? Here are a few methods that the library provides:

——-Method ——Description
#incrementIncrease progress by 1 unit. This is the most common way
#decrementReduced progress to 1 unit
#progress+=Lets increase progress a certain amount
#progress-=Lets reduce progress by a certain volume
#progress=Set progress to a specified value. Usually we just use this to test it.
#total=Change the total amount of completed progress bar

This can be any value (including nil ) but not less than the current progress

Can be illustrated by the following image:

Changing Progress Animation

Stopping

You can stop the progress bar in one of the following four ways:

——-Method——–description
#finishStop the progress bar immediately. The position of progress will be equal to the end position
#stopStop the progress bar immediately. The position of progress remains the same
#pauseWill stop the progress bar the same as stop but allow it to return to running by calling #resume .

Note: Elapsed Time and Estimated Time will stop when the progress bar is in paused state.

#resetWill stop the progress bar by resetting all of its information to the original time

Observe the illustration:

Finishing

By default, you will see the display bar ends when progress reaches the value equal to total . If you don’t want the progress bar to end automatically, pass the autofinish: false option when initializing.

Refreshing

If you need to show the progress bar again for a more real-time user experience, use #refresh . #refresh does not affect the current progress of the progress, but will update the Elapsed time and Estimated time log.

The process has no end points

Sometimes you will come across a task that does not know what the total weight of it is. This can happen when you are downloading a bunch of files or when you are processing a set of jobs that have not yet fully loaded.

At such times you can set the value of total nil and continue to increase the bar’s progress as usual. At this point, the bar will show you the progress that is running but it is not clear how much and when it will complete. For example

The output will look like the image above.

At any time when you know what the total value is, you can set it by the method

The progress bar will now change to a state like this:

Logging

When using the progress bar, you may want to log a specific output to the user. If you try to use a puts statement, you will notice that it overwrites the display bar. For example, when you puts "hello" after a progress is run, the screen might look like this:

This happens because the progressbar must always redraw it each time the progress changes. This is the limit of the output on the terminal. To avoid this, use the #log method.

The #Log method will automatically clear the progress bar, print the text, and then redraw the bar on the next line. So if you use this method, you will always see only a single display bar on the screen.

Conclude

Above is quite a bit that people can use to apply Ruby-progressbar gem to create a cool script in programming with Ruby. For more information, refer to the Ruby-progressbar Wiki . Happy coding!

Share the news now

Source : Viblo