Fixed a stupid typo in the headers.
[svrt.git] / progress_bar.cc
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with svrt.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <time.h>
26 #include "progress_bar.h"
27
28 ProgressBar::ProgressBar()  : _visible(false), _value_max(-1) { }
29
30 void ProgressBar::set_visible(bool visible) {
31   _visible = visible;
32 }
33
34 void ProgressBar::init(ostream *out, scalar_t value_max) {
35   _value_max = value_max;
36   _last_step = -1;
37   time(&_initial_time);
38   refresh(out, 0);
39 }
40
41 void ProgressBar::refresh(ostream *out, scalar_t value) {
42   if(_visible && _value_max > 0) {
43     int step = int((value * 40) / _value_max);
44
45     if(step > _last_step) {
46       char buffer[width + 1], date_buffer[buffer_size];
47       int i, j;
48       j = sprintf(buffer, "Timer: ");
49
50       for(i = 0; i < step; i++) buffer[j + i] = 'X';
51       for(; i < 40; i++) buffer[j + i] = (i%4 == 0) ? '+' : '-';
52       j += i;
53
54       time_t current_time; time(&current_time);
55       int rt = int(((current_time - _initial_time)/scalar_t(value)) * scalar_t(_value_max - value));
56
57       if(rt > 0) {
58         if(rt > 3600 * 24) {
59           time_t current;
60           time(&current);
61           current += rt;
62           strftime(date_buffer, buffer_size, "%a %b %e %H:%M", localtime(&current));
63           j += snprintf(buffer + j, width - j - 1, " (end ~ %s)", date_buffer);
64         } else {
65           int hours = rt/3600, min = (rt%3600)/60, sec = rt%60;
66           if(hours > 0)
67             j += snprintf(buffer + j, width - j - 1, " (~%dh%dmin left)", hours, min);
68           else if(min > 0)
69             j += snprintf(buffer + j, width - j - 1, " (~%dmin%ds left)", min, sec);
70           else
71             j += snprintf(buffer + j, width - j - 1, " (~%ds left)", sec);
72         }
73       }
74
75       for(; j < width; j++) buffer[j] = ' ';
76       buffer[j] = '\0';
77       (*out) << buffer << "\r";
78       out->flush();
79       _last_step = step;
80     }
81   }
82 }
83
84 void ProgressBar::finish(ostream *out) {
85   if(_visible) {
86     char buffer[width + 1];
87     int j;
88     time_t current_time; time(&current_time);
89     int rt = int(current_time - _initial_time);
90     int min = rt/60, sec = rt%60;
91     j = sprintf(buffer, "Timer: Total %dmin%ds", min, sec);
92     for(; j < width; j++) buffer[j] = ' ';
93     buffer[j] = '\0';
94     (*out) << buffer << endl;
95     out->flush();
96   }
97 }