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 //
16 // (C) Idiap Research Institute //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports //
19 ///////////////////////////////////////////////////////////////////////////
22 #include "progress_bar.h"
24 const int ProgressBar::_width = 80;
26 ProgressBar::ProgressBar() : _visible(false), _value_max(-1) { }
28 void ProgressBar::set_visible(bool visible) {
32 void ProgressBar::init(ostream *out, scalar_t value_max) {
33 _value_max = value_max;
39 void ProgressBar::refresh(ostream *out, scalar_t value) {
40 if(_visible && _value_max > 0) {
41 int step = int((value * 40) / _value_max);
43 if(1 || step > _last_step) {
44 char buffer[_width + 1], date_buffer[buffer_size];
46 j = sprintf(buffer, "Timer: ");
48 for(i = 0; i < step; i++) buffer[j + i] = 'X';
49 for(; i < 40; i++) buffer[j + i] = (i%4 == 0) ? '+' : '-';
52 time_t current_time; time(¤t_time);
53 int rt = int(((current_time - _initial_time)/scalar_t(value)) * scalar_t(_value_max - value));
60 strftime(date_buffer, buffer_size, "%a %b %e %H:%M", localtime(¤t));
61 j += snprintf(buffer + j, _width - j - 1, " (end ~ %s)", date_buffer);
63 int hours = rt/3600, min = (rt%3600)/60, sec = rt%60;
65 j += snprintf(buffer + j, _width - j - 1, " (~%dh%dmin left)", hours, min);
67 j += snprintf(buffer + j, _width - j - 1, " (~%dmin%ds left)", min, sec);
69 j += snprintf(buffer + j, _width - j - 1, " (~%ds left)", sec);
73 for(; j < _width; j++) buffer[j] = ' ';
75 (*out) << buffer << "\r";
82 void ProgressBar::finish(ostream *out) {
84 char buffer[_width + 1];
86 time_t current_time; time(¤t_time);
87 int rt = int(current_time - _initial_time);
88 int min = rt/60, sec = rt%60;
89 j = sprintf(buffer, "Timer: Total %dmin%ds", min, sec);
90 for(; j < _width; j++) buffer[j] = ' ';
92 (*out) << buffer << endl;