| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "ProgressUpdater.h" |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
#include <signal.h> |
| 23 |
|
| 24 |
static void moveCursorUp(size_t n) { |
| 25 |
std::cout << "\033[" << n << "F"; |
| 26 |
} |
| 27 |
|
| 28 |
static void showCursor() { |
| 29 |
signal(SIGINT, SIG_DFL); |
| 30 |
std::cout << "\033[?25h" << std::flush; |
| 31 |
} |
| 32 |
|
| 33 |
static void showCursorFromSignal(int) { |
| 34 |
showCursor(); |
| 35 |
raise(SIGINT); |
| 36 |
} |
| 37 |
|
| 38 |
static void hideCursor() { |
| 39 |
signal(SIGINT, showCursorFromSignal); |
| 40 |
std::cout << "\033[?25l" << std::flush; |
| 41 |
} |
| 42 |
|
| 43 |
static inline void printProgress(float v, size_t width) { |
| 44 |
size_t c = (size_t) (v * width); |
| 45 |
|
| 46 |
std::cout << std::setw(3) << (int) (v * 100) << "% "; |
| 47 |
for (size_t i = 0; i < c; i++) std::cout << "\u2588"; |
| 48 |
std::cout << std::string(width - c, ' '); |
| 49 |
std::cout << "\u25C0" << std::flush; |
| 50 |
} |
| 51 |
|
| 52 |
void ProgressUpdater::printUpdates() { |
| 53 |
std::vector<Update> updates; |
| 54 |
|
| 55 |
std::unique_lock<std::mutex> lock(mMutex); |
| 56 |
std::swap(updates, mUpdates); |
| 57 |
lock.unlock(); |
| 58 |
|
| 59 |
printUpdates(updates); |
| 60 |
} |
| 61 |
|
| 62 |
void ProgressUpdater::printUpdates(const std::vector<Update>& updates) { |
| 63 |
for (const auto update : updates) { |
| 64 |
mProgress[update.index] = std::max(mProgress[update.index], update.value); |
| 65 |
} |
| 66 |
|
| 67 |
moveCursorUp(mNumBars); |
| 68 |
printProgressBars(); |
| 69 |
} |
| 70 |
|
| 71 |
void ProgressUpdater::start() { |
| 72 |
auto threadMain = [this]() -> int { |
| 73 |
bool exitRequested = false; |
| 74 |
while (!exitRequested) { |
| 75 |
std::unique_lock<std::mutex> lock(mMutex); |
| 76 |
while (!mExitRequested && mUpdates.empty()) { |
| 77 |
mCondition.wait(lock); |
| 78 |
} |
| 79 |
exitRequested = mExitRequested; |
| 80 |
lock.unlock(); |
| 81 |
if (!exitRequested) { |
| 82 |
printUpdates(); |
| 83 |
} |
| 84 |
}; |
| 85 |
printUpdates(); |
| 86 |
return 0; |
| 87 |
}; |
| 88 |
|
| 89 |
mThread = std::thread(threadMain); |
| 90 |
|
| 91 |
initProgressValues(); |
| 92 |
printProgressBars(); |
| 93 |
|
| 94 |
hideCursor(); |
| 95 |
} |
| 96 |
|
| 97 |
void ProgressUpdater::stop() { |
| 98 |
std::unique_lock<std::mutex> lock(mMutex); |
| 99 |
mExitRequested = true; |
| 100 |
lock.unlock(); |
| 101 |
mCondition.notify_all(); |
| 102 |
mThread.join(); |
| 103 |
showCursor(); |
| 104 |
} |
| 105 |
|
| 106 |
void ProgressUpdater::update(size_t progressBarIndex, float value) { |
| 107 |
if (progressBarIndex >= mNumBars) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
value = std::max(0.0f, std::min(value, 1.0f)); |
| 112 |
std::unique_lock<std::mutex> lock(mMutex); |
| 113 |
mUpdates.push_back({ progressBarIndex, value }); |
| 114 |
lock.unlock(); |
| 115 |
mCondition.notify_one(); |
| 116 |
} |
| 117 |
|
| 118 |
void ProgressUpdater::update(size_t progressBarIndex, size_t value, size_t max) { |
| 119 |
if ((value != max) && (value % (max / 100 + 1) != 0)) return; |
| 120 |
update(progressBarIndex, value / (float) max); |
| 121 |
} |
| 122 |
|
| 123 |
void ProgressUpdater::initProgressValues() { |
| 124 |
mProgress.resize(mNumBars); |
| 125 |
std::fill(mProgress.begin(), mProgress.end(), 0.0f); |
| 126 |
} |
| 127 |
|
| 128 |
void ProgressUpdater::printProgressBars() { |
| 129 |
for (size_t i = 0; i < mNumBars; i++) { |
| 130 |
printProgress(mProgress[i], 32); |
| 131 |
std::cout << std::endl; |
| 132 |
} |
| 133 |
} |