Added BEAUTIFUL progress bar rendering.
This commit is contained in:
parent
ad5eebba94
commit
88289560a1
3 changed files with 117 additions and 3 deletions
32
include/Display.hpp
Normal file
32
include/Display.hpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef H_Display
|
||||
#define H_Display
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
|
||||
#include <unistd.h> // for STDOUT_FILENO
|
||||
|
||||
class Display
|
||||
{
|
||||
public:
|
||||
Display();
|
||||
|
||||
unsigned int Rows() const;
|
||||
unsigned int Cols() const;
|
||||
|
||||
/// Returns a winsize structure with size.ws_row is the number of rows, size.ws_col is the number of columns.
|
||||
static winsize GetTerminalSize();
|
||||
|
||||
/// Clears the whole screen.
|
||||
static void ClearScreen();
|
||||
|
||||
protected:
|
||||
unsigned int screenRows;//<! Number of rows currently displayed in the screen
|
||||
unsigned int screenCols;//<! Number of columns currently displayed in the screen
|
||||
};
|
||||
|
||||
/// Creates a progress bar
|
||||
std::string GenerateProgressBar(unsigned int width, unsigned int current, unsigned int total, bool showPercentage = true, std::string const& fillChar = "\u25A0");
|
||||
|
||||
#endif
|
||||
50
src/Display.cpp
Normal file
50
src/Display.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <Display.hpp>
|
||||
|
||||
using std::cout;
|
||||
|
||||
Display::Display()
|
||||
{
|
||||
winsize size = Display::GetTerminalSize();
|
||||
screenCols = size.ws_col;
|
||||
screenRows = size.ws_row;
|
||||
}
|
||||
|
||||
unsigned int Display::Cols() const { return screenCols; }
|
||||
unsigned int Display::Rows() const { return screenRows; }
|
||||
|
||||
winsize Display::GetTerminalSize()
|
||||
{
|
||||
winsize size;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
|
||||
return size;
|
||||
}
|
||||
|
||||
void Display::ClearScreen() { cout << "\033[2J\033[1;1H"; }
|
||||
|
||||
std::string GenerateProgressBar(unsigned int width, unsigned int current, unsigned int total, bool showPercentage, std::string const& fillChar)
|
||||
{
|
||||
std::string progressBar;
|
||||
unsigned int freeWidth = width-2;
|
||||
char buf[8];
|
||||
|
||||
progressBar = '[';
|
||||
|
||||
if(showPercentage)
|
||||
{
|
||||
double percentCurrent = 100.*((double)current/(double)total);
|
||||
freeWidth -= 7;
|
||||
sprintf(buf, "%5.1f%% ", percentCurrent);
|
||||
progressBar += buf;
|
||||
}
|
||||
|
||||
unsigned int scaledCurrent = (current*freeWidth)/total;
|
||||
|
||||
for(unsigned int i = 0 ; i < scaledCurrent ; i++)
|
||||
progressBar += fillChar;
|
||||
for(unsigned int i = scaledCurrent ; i < freeWidth ; i++)
|
||||
progressBar += ' ';
|
||||
|
||||
progressBar += ']';
|
||||
|
||||
return progressBar;
|
||||
}
|
||||
38
src/main.cpp
38
src/main.cpp
|
|
@ -1,5 +1,6 @@
|
|||
#include <iostream>
|
||||
#include <TreeNodeDiskUsage.hpp>
|
||||
#include <Display.hpp>
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
|
|
@ -24,7 +25,7 @@ int main(int argc, char *argv[])
|
|||
PRINT_VAR(s.st_blocks);
|
||||
PRINT_VAR(s.st_blocks*512);// Size on disk, in bytes
|
||||
}
|
||||
// if(0)
|
||||
if(0)
|
||||
{// test GetNodeName()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/victory.tar.gz").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/some/path/to/a/folder").GetNodeName());
|
||||
|
|
@ -36,7 +37,7 @@ int main(int argc, char *argv[])
|
|||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodeName());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodeName());
|
||||
}
|
||||
// if(0)
|
||||
if(0)
|
||||
{// test GetNodePath()
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a/b/c//////////").GetNodePath());
|
||||
PRINT_VAR(TreeNodeDiskUsage("/a///b//c/").GetNodePath());
|
||||
|
|
@ -47,16 +48,47 @@ int main(int argc, char *argv[])
|
|||
PRINT_VAR(TreeNodeDiskUsage(" \\this\\\\is/sparta/with spaces// ").GetNodePath());
|
||||
}
|
||||
if(0)
|
||||
{
|
||||
{// basic tree building and printing test
|
||||
TreeNodeDiskUsage tree(argv[1]);
|
||||
tree.BuildTree();
|
||||
tree.PrintTree();
|
||||
cout << "\n\n";
|
||||
PRINT_VAR(tree.GetTotalSize());
|
||||
PRINT_VAR(tree.GetTotalSizeOnDisk());
|
||||
cout << "\n\n";
|
||||
tree.PrintTree(0);
|
||||
tree.PrintTree(1);
|
||||
tree.PrintTree(2);
|
||||
}
|
||||
if(0)
|
||||
{// terminal display functions test
|
||||
Display d;
|
||||
PRINT_VAR(d.Rows());
|
||||
PRINT_VAR(d.Cols());
|
||||
|
||||
d.ClearScreen();
|
||||
cout << "coucou !\n";
|
||||
}
|
||||
//if(0)
|
||||
{// progress bar test
|
||||
// unsigned int width, unsigned int current, unsigned int total, char fillChar = 219, bool showPercentage = true
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100,false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, false));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, false));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(20, 0, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 25, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 50, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 75, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(20, 100, 100, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 666, 1000, true));
|
||||
PRINT_VAR(GenerateProgressBar(50, 1666, 1000, true));
|
||||
|
||||
PRINT_VAR(GenerateProgressBar(50, 333, 1000, true, "#"));
|
||||
}
|
||||
}
|
||||
else
|
||||
cerr << "Error : No path given !\n";
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue