*** empty log message ***
[folded-ctf.git] / progress_bar.cc
1
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.                         //
6 //                                                                       //
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.                              //
11 //                                                                       //
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/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret, (C) IDIAP                                //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include <time.h>
20 #include "progress_bar.h"
21
22 const int ProgressBar::_width = 80;
23
24 ProgressBar::ProgressBar()  : _visible(false), _value_max(-1) { }
25
26 void ProgressBar::set_visible(bool visible) {
27   _visible = visible;
28 }
29
30 void ProgressBar::init(ostream *out, scalar_t value_max) {
31   _value_max = value_max;
32   _last_step = -1;
33   time(&_initial_time);
34   refresh(out, 0);
35 }
36
37 void ProgressBar::refresh(ostream *out, scalar_t value) {
38   if(_visible && _value_max > 0) {
39     int step = int((value * 40) / _value_max);
40
41     if(1 || step > _last_step) {
42       char buffer[_width + 1], date_buffer[buffer_size];
43       int i, j;
44       j = sprintf(buffer, "Timer: ");
45
46       for(i = 0; i < step; i++) buffer[j + i] = 'X';
47       for(; i < 40; i++) buffer[j + i] = (i%4 == 0) ? '+' : '-';
48       j += i;
49
50       time_t current_time; time(&current_time);
51       int rt = int(((current_time - _initial_time)/scalar_t(value)) * scalar_t(_value_max - value));
52
53       if(rt > 0) {
54         if(rt > 3600 * 24) {
55           time_t current;
56           time(&current);
57           current += rt;
58           strftime(date_buffer, buffer_size, "%a %b %e %H:%M", localtime(&current));
59           j += snprintf(buffer + j, _width - j - 1, " (end ~ %s)", date_buffer);
60         } else {
61           int hours = rt/3600, min = (rt%3600)/60, sec = rt%60;
62           if(hours > 0)
63             j += snprintf(buffer + j, _width - j - 1, " (~%dh%dmin left)", hours, min);
64           else if(min > 0)
65             j += snprintf(buffer + j, _width - j - 1, " (~%dmin%ds left)", min, sec);
66           else
67             j += snprintf(buffer + j, _width - j - 1, " (~%ds left)", sec);
68         }
69       }
70
71       for(; j < _width; j++) buffer[j] = ' ';
72       buffer[j] = '\0';
73       (*out) << buffer << "\r";
74       out->flush();
75       _last_step = step;
76     }
77   }
78 }
79
80 void ProgressBar::finish(ostream *out) {
81   if(_visible) {
82     char buffer[_width + 1];
83     int j;
84     time_t current_time; time(&current_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] = ' ';
89     buffer[j] = '\0';
90     (*out) << buffer << endl;
91     out->flush();
92   }
93 }