Code Monkey home page Code Monkey logo

cpp_progress_bar's Introduction

C++ Progress Bar

C++ class that implements a progress bar in console applications on both Linux and Windows platforms. The progress bar works by referencing an index that keeps updating when processing through a piece of code.

Key features

  1. Control the number of updates the progress bar makes.
  2. The ability to adjust the style of the progress bar.
  3. The length of the progress bar displayed on screen adapts to the the width of the console.

Implementation

To use the progress bar, include the following header file:

#include "progress_bar.hpp"

Creating a progress bar

To create a progress bar, the total number of jobs need to be known.

int n = 10;
ProgressBar progress_bar(n);

Optionally, small descriptions can also be added to the progress bar.

int n = 10;
ProgressBar progress_bar(n, "Example 1");

Updating a progress bar

Updates to the progress bar are made using the increment operators.

Example 1:

int n = 10;
ProgressBar progress_bar(n, "Example 1");

int job_index = 0;

do_some_work();

job_index++;
progress_bar += 1;

do_some_more_work();

job_index++;
progress_bar += 1;

Example 2:

int n = 100;
ProgressBar bar1(n, "Example 1");

for (int i = 0; i <= n; ++i) {
    ++bar1;
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

Setting the frequency of updates

If progressing through a large job list, iterating through every update will slow down the program. Using the method SetFrequencyUpdate(int freq) overcomes this problem by setting how many times the progress bar should be updated.

Example 3:

int n = 100000;
ProgressBar bar2(n, "Example 3");
bar2.SetFrequencyUpdate(10);

for (int i = 0; i <= n; ++i){
    ++bar2;
}

Changing the bar style

The progress bar can be customised using the SetStyle(const char*, const char*) method. This changes the ASCII/Unicode characters used for printing the progress bar.

Example 4

n = 1000;
ProgressBar bar3(n, "Example 4");
bar3.SetFrequencyUpdate(10);
bar3.SetStyle("\u2588", "-");

for (int i = 0; i <= n; ++i) {
    ++bar3;
}

Main Example

#include <iostream>
#include <thread>
#include <chrono>

#include "progress_bar.hpp"


int main() {

    int n;

    /// Example 1 ///

    n = 100;
    ProgressBar bar1(n, "Example 1");

    for (int i = 0; i <= n; ++i) {
        ++bar1;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    /// Example 2 ///

    n = 1000;
    ProgressBar bar2(n, "Example 2");
    bar2.SetFrequencyUpdate(10);
    bar2.SetStyle("|", "-");
    //bar2.SetStyle("\u2588", "-"); for linux

    std::cout << std::endl;
    for (int i = 0; i <= n; ++i) {
        ++bar2;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }

    n = 5;
    ProgressBar bar3(n);
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    // following tests exception error
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
    ++bar3;

    return 0;
}

cpp_progress_bar's People

Contributors

karasikov avatar htailor avatar badshah400 avatar ekg avatar szalata avatar hmusta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.