├── .gitignore ├── LICENSE ├── README.md ├── examples ├── Makefile ├── example.cpp ├── example_makefile ├── practical_example.cpp └── practical_makefile ├── gifs ├── practical-example.gif └── progress_bar.gif ├── include └── ProgressBar.hpp └── src └── ProgressBar.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | example 2 | main 3 | main.cpp 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Llawliet0872 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Progress Bar for C++ 2 | 3 | ### This is a simple library for progress bars in c++. 4 | 5 | ## The output of examples/example.cpp: 6 | ![](gifs/progress_bar.gif) 7 | 8 | ## Documentation: 9 | 10 | - Initializing a progress bar - `ProgressBar name(char notDoneChar, char doneChar,unsigned int size)`, notDoneChar is what the bar will be filled with before it starts to do a process, doneChar is what the progress bar will fill up while a process is being done. size is the size of the bar. 11 | - `fillUp()` - Fills the bar by 1 everytime it is called. 12 | - `fillUpCells(int cells)` - Fills the bar upto a given number. 13 | - `displayPercentage()` - Displays the percentage of work done. 14 | - `displayTasksDone()` - Displays number of tasks done out of the number of taks to be done eg.`(1 / 28)` 15 | - `end()` - Call this function before printing anything to the terminal and when the bar is no longer needed. 16 | - `getSize()` - This function returns the size of the progress bar (the number of cells). 17 | 18 | ## Try it yourself: 19 | - example.cpp - You can do it by using `make example` assuming you haven't moved any files. Use `./example` to run it. 20 | - practical_example.cpp - You can do it by using `make practical` assuming you haven't moved any files and you do have the development version of libcurl installed. Use `./practical` to run it. 21 | ### To install libcurl: 22 | #### Debian: `sudo apt install libcurl4-openssl-dev` 23 | #### Arch: `sudo pacman -S curl` 24 | - And finally You can use `make clean` to clean the .o files and the executables 25 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | example: 2 | make -f example_makefile 3 | 4 | practical: 5 | make -f practical_makefile 6 | 7 | clean: 8 | rm *.o practical example 9 | -------------------------------------------------------------------------------- /examples/example.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/ProgressBar.hpp" 2 | #include 3 | 4 | unsigned int micros = 1000000; // microseconds to seconds 5 | 6 | int main() 7 | { 8 | ProgressBar foo('.', '#', 30); 9 | 10 | foo.done = 0; 11 | foo.todo = foo.getSize(); 12 | 13 | std::cout << "Doing a task" << std::endl; 14 | 15 | for(int i = 0; i < foo.getSize(); i++) 16 | { 17 | foo.done++; 18 | usleep(0.1 * micros); // A delay of 0.1 second(s) 19 | foo.fillUp(); 20 | foo.displayPercentage(); 21 | std::cout << " | "; 22 | foo.displayTasksDone(); 23 | } 24 | 25 | foo.end(); 26 | 27 | std::cout << "Done!" << std::endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /examples/example_makefile: -------------------------------------------------------------------------------- 1 | 2 | example: example.o ProgressBar.o 3 | g++ example.o ProgressBar.o -o example 4 | 5 | example.o: example.cpp 6 | g++ -c example.cpp 7 | 8 | ProgressBar.o: ../include/ProgressBar.hpp ../src/ProgressBar.cpp 9 | g++ -c ../src/ProgressBar.cpp 10 | -------------------------------------------------------------------------------- /examples/practical_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../include/ProgressBar.hpp" 4 | 5 | int main() 6 | { 7 | ProgressBar bar('.', '=', 30); 8 | 9 | std::cout << "Downloading onedark.vim from https://raw.githubusercontent.com/joshdick/onedark.vim/master/colors/onedark.vim" << std::endl; 10 | 11 | CURL *downloader = curl_easy_init(); 12 | FILE *file = fopen("onedark.vim", "w"); 13 | CURLcode result; 14 | std::string url = "https://raw.githubusercontent.com/joshdick/onedark.vim/master/colors/onedark.vim"; 15 | const char *URL = url.c_str(); 16 | curl_easy_setopt(downloader, CURLOPT_URL, URL); 17 | bar.fillUpCells(10); 18 | bar.displayPercentage(); 19 | curl_easy_setopt(downloader, CURLOPT_WRITEDATA, file); 20 | bar.fillUpCells(20); 21 | bar.displayPercentage(); 22 | result = curl_easy_perform(downloader); 23 | fclose(file); 24 | bar.fillUpCells(30); 25 | bar.displayPercentage(); 26 | curl_easy_cleanup(downloader); 27 | 28 | bar.end(); 29 | std::cout << "Done" << std::endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /examples/practical_makefile: -------------------------------------------------------------------------------- 1 | 2 | practical: practical_example.o ProgressBar.o 3 | g++ practical_example.o ProgressBar.o -o practical -lcurl 4 | 5 | practical_example.o: practical_example.cpp 6 | g++ -c practical_example.cpp 7 | 8 | ProgressBar.o: ../src/ProgressBar.cpp ../include/ProgressBar.hpp 9 | g++ -c ../src/ProgressBar.cpp 10 | -------------------------------------------------------------------------------- /gifs/practical-example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wwsmiff/progress_bar_cpp/3074877a2fea6562f12d6901e9e9d45d9c91898a/gifs/practical-example.gif -------------------------------------------------------------------------------- /gifs/progress_bar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wwsmiff/progress_bar_cpp/3074877a2fea6562f12d6901e9e9d45d9c91898a/gifs/progress_bar.gif -------------------------------------------------------------------------------- /include/ProgressBar.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | class ProgressBar 8 | { 9 | public: 10 | /* Takes in a char for filling up the bar and the size fo the bar */ 11 | ProgressBar(char notDoneChar, char doneChar, unsigned int size); 12 | void end(); 13 | 14 | unsigned int todo; 15 | unsigned int done; 16 | 17 | /* Fills the bar upto a given number */ 18 | void fillUpCells(unsigned int cells); 19 | 20 | /* Fills the bar one by one */ 21 | void fillUp(); 22 | 23 | /* Displays the percentage beside the bar */ 24 | void displayPercentage(); 25 | 26 | 27 | /* Shows tasks done out of the tasks to be done */ 28 | void displayTasksDone(); 29 | 30 | /* Returns the size of the progress bar */ 31 | unsigned int getSize(); 32 | 33 | private: 34 | unsigned int size = 0; 35 | unsigned int pos = 1; 36 | char c; 37 | char ch; 38 | std::vector bar; 39 | }; 40 | -------------------------------------------------------------------------------- /src/ProgressBar.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/ProgressBar.hpp" 2 | /* Defining the constructor */ 3 | ProgressBar::ProgressBar(char notDoneChar, char doneChar, unsigned int size) 4 | :c(doneChar), ch(notDoneChar), size(size), todo(0), done(0) 5 | { 6 | if(size <= 100) 7 | { 8 | size = size; 9 | } 10 | else{ 11 | size = 100; 12 | } 13 | bar.push_back('['); 14 | for(int i = 1; i < size + 1; i++) 15 | { 16 | bar.push_back(ch); 17 | } 18 | 19 | bar.push_back(']'); 20 | } 21 | 22 | /* Defining fillUpCells */ 23 | void ProgressBar::fillUpCells(unsigned int cells) 24 | { 25 | pos = 0; 26 | for(int i = 1; i < cells; i++) 27 | { 28 | bar[i] = c; 29 | std::cout << '\r'; 30 | for(int j = 0; j < bar.size(); j++) 31 | { 32 | std::cout << bar[j] << std::flush; 33 | } 34 | } 35 | pos += cells; 36 | } 37 | 38 | /* Defining fillUp */ 39 | void ProgressBar::fillUp() 40 | { 41 | bar[pos] = c; 42 | pos++; 43 | 44 | std::cout << '\r'; 45 | 46 | for(int i = 0; i < bar.size(); i++) 47 | { 48 | std::cout << bar[i] << std::flush; 49 | } 50 | } 51 | 52 | /* Displays the percentage beside the bar */ 53 | void ProgressBar::displayPercentage() 54 | { 55 | float percent = ((float)pos / (float)(bar.size() - 1)) * 100; 56 | std::cout << (int)percent << "%"; 57 | } 58 | 59 | /* Shows tasks done out of the tasks to be done */ 60 | void ProgressBar::displayTasksDone() 61 | { 62 | std::cout << '(' << done << '/' << todo << ')' << std::flush; 63 | } 64 | 65 | /* Returns the size of the progress bar */ 66 | 67 | unsigned int ProgressBar::getSize() 68 | { 69 | return bar.size() - 2; 70 | } 71 | 72 | void ProgressBar::end() 73 | { 74 | std::cout << std::endl; 75 | } 76 | --------------------------------------------------------------------------------