1 #ifndef PROGRESS_H 2 #define PROGRESS_H 3 4 #include "time.h" 5 6 using namespace std; 7 8 const int BAR_BUF_SIZE = 28; 9 const char SYMBOL = '*'; 10 11 class ProgressBar 12 { 13 char _bar_buf[BAR_BUF_SIZE]; 14 string _process; 15 long double _num_complete; 16 long double _tot_num; 17 int _num_updates; 18 int _num_remaining; 19 20 public: ProgressBar()21 ProgressBar() {} 22 ProgressBar(string process,double tot_num)23 ProgressBar(string process, double tot_num) 24 { 25 _tot_num = tot_num; 26 _process = process; 27 _num_complete = -1.0; 28 _num_remaining = -1.0; 29 _num_updates = 0; 30 31 for(int i=0; i < BAR_BUF_SIZE; ++i) _bar_buf[i] = ' '; 32 _bar_buf[0] = '['; 33 _bar_buf[BAR_BUF_SIZE-2] = ']'; 34 _bar_buf[BAR_BUF_SIZE-1] = '\0'; 35 36 37 time_t rawtime; 38 struct tm * timeinfo; 39 char time_buf [80]; 40 41 time ( &rawtime ); 42 timeinfo = localtime ( &rawtime ); 43 44 strftime (time_buf,80,"%H:%M:%S",timeinfo); 45 46 fprintf(stderr, "[%s] %s\n", time_buf, _process.c_str()); 47 } 48 update(const char * bundle_label_buf,double inc_amt)49 void update(const char* bundle_label_buf, double inc_amt) 50 { 51 52 _num_complete += inc_amt; 53 _num_updates ++; 54 55 if (cuff_verbose||cuff_quiet||_tot_num==0) return; 56 57 char bundle_buf[28]; 58 bundle_buf[27] = '\0'; 59 strncpy(bundle_buf, bundle_label_buf, 27); 60 61 int percent = (_num_complete * 100)/_tot_num; 62 63 percent = min(percent, 99); 64 65 int last_bar = percent/(100/(BAR_BUF_SIZE-3)); 66 for (int i=1; i <= last_bar; ++i) 67 _bar_buf[i] = SYMBOL; 68 69 char line_buf[82]; 70 snprintf(line_buf, 82, "\r> Processing Locus %-27s %s %3d%%", bundle_buf, _bar_buf, percent); 71 72 fprintf(stderr,"%s",line_buf); 73 } 74 remaining(int num_remaining)75 void remaining(int num_remaining) 76 { 77 if (cuff_verbose||cuff_quiet||_tot_num==0||num_remaining==_num_remaining) return; 78 79 _num_remaining = num_remaining; 80 81 int percent = (_num_complete * 100)/_tot_num; 82 percent = min(percent, 99); 83 84 char msg_buff[45]; 85 sprintf(msg_buff, "Waiting for %d threads to complete.", num_remaining); 86 87 int last_bar = percent/(100/(BAR_BUF_SIZE-3)); 88 for (int i=1; i <= last_bar; ++i) 89 _bar_buf[i] = SYMBOL; 90 91 char line_buf[82]; 92 snprintf(line_buf, 81, "\r> %-44s %s %3d%%", msg_buff, _bar_buf, percent); 93 94 fprintf(stderr,"%s",line_buf); 95 } 96 complete()97 void complete() 98 { 99 for (int i=1; i < BAR_BUF_SIZE-2; ++i) 100 _bar_buf[i] = SYMBOL; 101 char complete_buf[45]; 102 snprintf(complete_buf, 44, "Processed %d loci.", _num_updates); 103 if (cuff_verbose||cuff_quiet) 104 fprintf(stderr, "%-44s\n", complete_buf); 105 else 106 fprintf(stderr, "\r> %-44s %s %3d%%\n", complete_buf, _bar_buf, 100); 107 } 108 }; 109 110 #endif 111