2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation. //
7 // This program is distributed in the hope that it will be useful, but //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
10 // General Public License for more details. //
12 // You should have received a copy of the GNU General Public License //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
15 // Written by Francois Fleuret, (C) IDIAP //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
17 ///////////////////////////////////////////////////////////////////////////
20 #include "progress_bar.h"
22 const int ProgressBar::_width = 80;
24 ProgressBar::ProgressBar() : _visible(false), _value_max(-1) { }
26 void ProgressBar::set_visible(bool visible) {
30 void ProgressBar::init(ostream *out, scalar_t value_max) {
31 _value_max = value_max;
37 void ProgressBar::refresh(ostream *out, scalar_t value) {
38 if(_visible && _value_max > 0) {
39 int step = int((value * 40) / _value_max);
41 if(1 || step > _last_step) {
42 char buffer[_width + 1], date_buffer[buffer_size];
44 j = sprintf(buffer, "Timer: ");
46 for(i = 0; i < step; i++) buffer[j + i] = 'X';
47 for(; i < 40; i++) buffer[j + i] = (i%4 == 0) ? '+' : '-';
50 time_t current_time; time(¤t_time);
51 int rt = int(((current_time - _initial_time)/scalar_t(value)) * scalar_t(_value_max - value));
58 strftime(date_buffer, buffer_size, "%a %b %e %H:%M", localtime(¤t));
59 j += snprintf(buffer + j, _width - j - 1, " (end ~ %s)", date_buffer);
61 int hours = rt/3600, min = (rt%3600)/60, sec = rt%60;
63 j += snprintf(buffer + j, _width - j - 1, " (~%dh%dmin left)", hours, min);
65 j += snprintf(buffer + j, _width - j - 1, " (~%dmin%ds left)", min, sec);
67 j += snprintf(buffer + j, _width - j - 1, " (~%ds left)", sec);
71 for(; j < _width; j++) buffer[j] = ' ';
73 (*out) << buffer << "\r";
80 void ProgressBar::finish(ostream *out) {
82 char buffer[_width + 1];
84 time_t current_time; time(¤t_time);
85 int rt = int(current_time - _initial_time);
86 int min = rt/60, sec = rt%60;
87 j = sprintf(buffer, "Timer: Total %dmin%ds", min, sec);
88 for(; j < _width; j++) buffer[j] = ' ';
90 (*out) << buffer << endl;