Skip to content

Commit

Permalink
Add progressbar class, part of solve of #81
Browse files Browse the repository at this point in the history
  • Loading branch information
natir committed Jun 22, 2017
1 parent f0a9f1e commit 3c29441
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 2 deletions.
96 changes: 96 additions & 0 deletions utils/progressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright Copyright 2016-17 Pierre Marijon
This file is part of fastQT.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
@author : Pierre Marijon <pierre@marijon.fr>
Freely inspired by GATB-Core Progress class https://github.com/GATB/gatb-core
*/

#include "progressbar.h"

ProgressBar::ProgressBar(unsigned int ntasks, const std::string& message, std::ostream& output) : message(message), os(output)
{
reset(ntasks);
}

void ProgressBar::reset(unsigned int ntasks)
{
this->todo = ntasks;
this->done = 0;
this->partial = 0;
this->subdiv = 100;
this->steps = static_cast<double>(ntasks > 0 ? ntasks : 1) / static_cast<double>(subdiv);
}

void ProgressBar::init()
{
os << message;
postInit();
}

void ProgressBar::finish()
{
/** We set the total done. */
set (todo);

/** We forward the remaining of the finish to postFinish method. */
postFinish ();

/** We reset all progression variables. */
todo = 0;
done = 0;
partial = 0;
}

void ProgressBar::inc (unsigned int ntasks_done)
{
done += ntasks_done;
partial += ntasks_done;

while (partial >= steps)
{
update();
partial -= steps;
}
}

void ProgressBar::set (unsigned int ntasks_done)
{
if (ntasks_done > done)
{
inc (ntasks_done-done);
}
}

void ProgressBar::postInit ()
{
os << "[";
os.flush();
}

void ProgressBar::postFinish ()
{
os << "]" << std::endl;
os.flush();
}

void ProgressBar::update()
{
os << "-";
os.flush();
}
63 changes: 63 additions & 0 deletions utils/progressbar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright Copyright 2016-17 Pierre Marijon
This file is part of FastQt.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
@author : Pierre Marijon <pierre@marijon.fr>
Freely inspired by GATB-Core Progress class https://github.com/GATB/gatb-core
*/

#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H

#include <iostream>

class ProgressBar
{
public:
ProgressBar(unsigned int ntask, const std::string& message, std::ostream& os=std::cerr);

virtual ~ProgressBar() {}

void init();

void finish();

void inc(unsigned int ntask_done);

void set(unsigned int ntask_done);

void reset(unsigned int ntask);

protected:

protected:

virtual void update ();
virtual void postInit ();
virtual void postFinish ();

std::string message;
u_int64_t done;
u_int64_t todo;
int subdiv ; // progress printed every 1/subdiv of total to do
double partial;
double steps ; //steps = _todo/subidv
std::ostream& os;
char buffer[512];
};

#endif // PROGRESSBAR_H
6 changes: 4 additions & 2 deletions utils/utils.pri
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ HEADERS += \
$$PWD/basegroup.h \
$$PWD/statistic.h \
$$PWD/imageformatdefinition.h \
$$PWD/format_detection.h
$$PWD/format_detection.h \
$$PWD/progressbar.h

SOURCES += \
$$PWD/basegroup.cpp \
$$PWD/format_detection.cpp
$$PWD/format_detection.cpp \
$$PWD/progressbar.cpp

0 comments on commit 3c29441

Please sign in to comment.