| 152 | } |
| 153 | |
| 154 | inline int |
| 155 | ProgressBar::update(std::string message_ = "") |
| 156 | { |
| 157 | std::wstring message = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(message_); |
| 158 | |
| 159 | if (n_cycles == 0) { |
| 160 | std::cerr << "ProgressBar::update: number of cycles not set"; |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | for (int _ = 0; _ < msg_width; _++) |
| 165 | output << '\b'; |
| 166 | |
| 167 | if (!update_is_called) { |
| 168 | if (do_show_bar == true) { |
| 169 | output << opening_char; |
| 170 | for (int _ = 0; _ < bar_width; _++) |
| 171 | output << todo_char; |
| 172 | output << closing_char << " 0%"; |
| 173 | } else |
| 174 | output << "0%"; |
| 175 | } |
| 176 | update_is_called = true; |
| 177 | |
| 178 | int perc = 0; |
| 179 | |
| 180 | // compute percentage, if did not change, do nothing and return |
| 181 | perc = progress * 100. / (n_cycles - 1); |
| 182 | if (perc < last_perc) |
| 183 | return 1; |
| 184 | |
| 185 | // update percentage each unit |
| 186 | if (perc == last_perc + 1) { |
| 187 | // erase the correct number of characters |
| 188 | if (perc <= 10) |
| 189 | output << "\b\b" << perc << '%'; |
| 190 | else if (perc > 10 && perc < 100) |
| 191 | output << "\b\b\b" << perc << '%'; |
| 192 | else if (perc == 100) |
| 193 | output << "\b\b\b" << perc << '%'; |
| 194 | } |
| 195 | if (do_show_bar == true) { |
| 196 | // update bar every ten units |
| 197 | if (perc % 2 == 0) { |
| 198 | // erase closing bracket |
| 199 | output << std::wstring(closing_char.size(), '\b'); |
| 200 | // erase trailing percentage characters |
| 201 | if (perc < 10) |
| 202 | output << "\b\b\b"; |
| 203 | else if (perc >= 10 && perc < 100) |
| 204 | output << "\b\b\b\b"; |
| 205 | else if (perc == 100) |
| 206 | output << "\b\b\b\b\b"; |
| 207 | |
| 208 | // erase 'todo_char' |
| 209 | for (int j = 0; j < bar_width - (perc - 1) / 2; ++j) { |
| 210 | output << std::wstring(todo_char.size(), '\b'); |
| 211 | } |