| 42 |  | #include <string> | 
| 43 |  | #include <sstream> | 
| 44 |  | #include <sys/ioctl.h> | 
| 45 | + | #define _POSIX_SOURCE | 
| 46 | + | #include <unistd.h> | 
| 47 |  | #ifdef IS_MPI | 
| 48 |  | #include <mpi.h> | 
| 49 |  | #endif //is_mpi | 
| 76 |  | #ifdef IS_MPI | 
| 77 |  | if (MPI::COMM_WORLD.Get_rank() == 0) { | 
| 78 |  | #endif | 
| 77 | – | #ifndef IS_MPI | 
| 78 | – | // get the window width: | 
| 79 | – | struct ttysize ts; | 
| 80 | – | ioctl(0, TIOCGWINSZ, &ts); | 
| 81 | – | width = ts.ts_cols; | 
| 82 | – | #endif | 
| 79 |  |  | 
| 80 | < | // We'll use: | 
| 81 | < | // 31 characters for the completion estimate, | 
| 82 | < | //  6 for the % complete, | 
| 83 | < | //  2 characters for the open and closing brackets. | 
| 80 | > | // only do the progress bar if we are actually running in a tty: | 
| 81 | > | if (isatty(fileno(stdout))) { | 
| 82 | > | // get the window width: | 
| 83 | > | struct winsize w; | 
| 84 | > | ioctl(fileno(stdout), TIOCGWINSZ, &w); | 
| 85 | > | width = w.ws_col; | 
| 86 |  |  | 
| 87 | < | int avail = width - 31 - 6 - 2; | 
| 87 | > | // We'll use: | 
| 88 | > | // 31 characters for the completion estimate, | 
| 89 | > | //  6 for the % complete, | 
| 90 | > | //  2 characters for the open and closing brackets. | 
| 91 | > |  | 
| 92 | > | int avail = width - 31 - 6 - 2; | 
| 93 | > |  | 
| 94 | > | ++iteration_; | 
| 95 | > |  | 
| 96 | > | if (maximum_ > 0.0) { | 
| 97 |  |  | 
| 98 | < | ++iteration_; | 
| 99 | < |  | 
| 100 | < | if (maximum_ > 0.0) { | 
| 101 | < | // we know the maximum, so | 
| 102 | < | // draw a progress bar | 
| 103 | < |  | 
| 104 | < | RealType percent = value_ * 100.0 / maximum_; | 
| 105 | < | int hashes = int(percent * avail / 100.0); | 
| 106 | < | std::string progressbar; | 
| 107 | < | progressbar.assign(hashes, '#'); | 
| 108 | < |  | 
| 109 | < | // add the spinner to the end of the progress bar: | 
| 110 | < | progressbar += progressSpinner_[iteration_ & 3]; | 
| 111 | < |  | 
| 112 | < | // compute the best estimate of the ending time: | 
| 113 | < | time_t current_ = time(NULL); | 
| 114 | < | time_t end_ = start_ + (current_ - start_) * (100.0/percent); | 
| 115 | < | struct tm * ender = localtime(&end_); | 
| 116 | < | char buffer[24]; | 
| 117 | < | strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender); | 
| 118 | < |  | 
| 119 | < | std::stringstream fmt; | 
| 120 | < | fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s"; | 
| 121 | < | std::string st = fmt.str(); | 
| 122 | < |  | 
| 123 | < | printf(st.c_str(), int(percent), | 
| 124 | < | progressbar.c_str(), | 
| 125 | < | buffer); | 
| 126 | < |  | 
| 127 | < | } else { | 
| 128 | < | // we don't know the maximum, so we can't draw a progress bar | 
| 129 | < | int center = (iteration_ % 48) + 1; // 50 spaces, minus 2 | 
| 130 | < | std::string before; | 
| 131 | < | std::string after; | 
| 132 | < | before.assign(std::max(center - 2, 0), ' '); | 
| 133 | < | after.assign(std::min(center + 2, 50), ' '); | 
| 134 | < |  | 
| 135 | < | printf("\r[%s###%s]            ", | 
| 129 | < | before.c_str(), after.c_str()); | 
| 130 | < | } | 
| 131 | < | fflush(stdout); | 
| 98 | > | // we know the maximum, so draw a progress bar | 
| 99 | > |  | 
| 100 | > | RealType percent = value_ * 100.0 / maximum_; | 
| 101 | > | int hashes = int(percent * avail / 100.0); | 
| 102 | > | std::string progressbar; | 
| 103 | > | progressbar.assign(hashes, '#'); | 
| 104 | > |  | 
| 105 | > | // add the spinner to the end of the progress bar: | 
| 106 | > | progressbar += progressSpinner_[iteration_ & 3]; | 
| 107 | > |  | 
| 108 | > | // compute the best estimate of the ending time: | 
| 109 | > | time_t current_ = time(NULL); | 
| 110 | > | time_t end_ = start_ + (current_ - start_) * (100.0/percent); | 
| 111 | > | struct tm * ender = localtime(&end_); | 
| 112 | > | char buffer[24]; | 
| 113 | > | strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender); | 
| 114 | > |  | 
| 115 | > | std::stringstream fmt; | 
| 116 | > | fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s"; | 
| 117 | > | std::string st = fmt.str(); | 
| 118 | > |  | 
| 119 | > | printf(st.c_str(), int(percent), | 
| 120 | > | progressbar.c_str(), | 
| 121 | > | buffer); | 
| 122 | > |  | 
| 123 | > | } else { | 
| 124 | > | // we don't know the maximum, so we can't draw a progress bar | 
| 125 | > | int center = (iteration_ % 48) + 1; // 50 spaces, minus 2 | 
| 126 | > | std::string before; | 
| 127 | > | std::string after; | 
| 128 | > | before.assign(std::max(center - 2, 0), ' '); | 
| 129 | > | after.assign(std::min(center + 2, 50), ' '); | 
| 130 | > |  | 
| 131 | > | printf("\r[%s###%s]            ", | 
| 132 | > | before.c_str(), after.c_str()); | 
| 133 | > | } | 
| 134 | > | fflush(stdout); | 
| 135 | > | } | 
| 136 |  | #ifdef IS_MPI | 
| 137 |  | } | 
| 138 |  | #endif |