aboutsummaryrefslogtreecommitdiff
path: root/utils/optimin/src/ProgressBar.h
diff options
context:
space:
mode:
authorAdrian Herrera <adrian.herrera02@gmail.com>2021-07-20 04:23:26 +0000
committerAdrian Herrera <adrian.herrera02@gmail.com>2021-07-21 04:02:52 +0000
commit62f1bfed99b82bc073c138a00ff9a30bb596d09d (patch)
tree2d401f65bae30aba3bbff305e80aeea51992f39a /utils/optimin/src/ProgressBar.h
parent3d7a2fc869a03da4c49a0a7e05d97f01a2846337 (diff)
downloadafl++-62f1bfed99b82bc073c138a00ff9a30bb596d09d.tar.gz
utils: added optimin corpus minimizer
Diffstat (limited to 'utils/optimin/src/ProgressBar.h')
-rw-r--r--utils/optimin/src/ProgressBar.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/utils/optimin/src/ProgressBar.h b/utils/optimin/src/ProgressBar.h
new file mode 100644
index 00000000..2f8d7403
--- /dev/null
+++ b/utils/optimin/src/ProgressBar.h
@@ -0,0 +1,58 @@
+/**
+ * Progress bar.
+ *
+ * Adapted from https://www.bfilipek.com/2020/02/inidicators.html
+ */
+
+#pragma once
+
+#include <llvm/ADT/StringRef.h>
+#include <llvm/Support/raw_ostream.h>
+
+/// Display a progress bar in the terminal
+class ProgressBar {
+ private:
+ const size_t BarWidth;
+ const std::string Fill;
+ const std::string Remainder;
+
+ public:
+ ProgressBar() : ProgressBar(60, "#", " ") {
+ }
+
+ ProgressBar(size_t Width, const llvm::StringRef F, const llvm::StringRef R)
+ : BarWidth(Width), Fill(F), Remainder(R) {
+ }
+
+ void update(float Progress, const llvm::StringRef Status = "",
+ llvm::raw_ostream &OS = llvm::outs()) {
+ // No need to write once progress is 100%
+ if (Progress > 100.0f) return;
+
+ // Move cursor to the first position on the same line and flush
+ OS << '\r';
+ OS.flush();
+
+ // Start bar
+ OS << '[';
+
+ const auto Completed =
+ static_cast<size_t>(Progress * static_cast<float>(BarWidth) / 100.0);
+ for (size_t I = 0; I < BarWidth; ++I) {
+ if (I <= Completed) {
+ OS << Fill;
+ } else {
+ OS << Remainder;
+ }
+ }
+
+ // End bar
+ OS << ']';
+
+ // Write progress percentage
+ OS << ' ' << std::min(static_cast<size_t>(Progress), size_t(100)) << '%';
+
+ // Write status text
+ OS << " " << Status;
+ }
+};