Skip to content

Commit

Permalink
[utils] Add an option to make the ProgressMonitor visible or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-de-Grandmaison-ARM committed Feb 3, 2025
1 parent d1d0101 commit 115ebab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
15 changes: 10 additions & 5 deletions include/PAF/utils/ProgressMonitor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: <text>Copyright 2022,2024 Arm Limited and/or its
* SPDX-FileCopyrightText: <text>Copyright 2022,2024,2025 Arm Limited and/or its
* affiliates <[email protected]></text>
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -32,9 +32,10 @@ class ProgressMonitor {
/// Construct a ProgressMonitor, to output progress on OS, using Title as
/// the prefix string, expecting Total number of steps to reach completion
/// of the task.
ProgressMonitor(std::ostream &OS, const std::string &Title, size_t Total)
ProgressMonitor(std::ostream &OS, const std::string &Title, size_t Total,
bool Visible = true)
: os(OS), title(Title), totalNumberOfSteps(Total), progress(0),
lastPercentageLogged(-1) {
lastPercentageLogged(-1), visible(Visible) {
display();
}

Expand All @@ -56,8 +57,10 @@ class ProgressMonitor {
void display() {
unsigned percentage = 100 * progress / totalNumberOfSteps;
if (percentage != lastPercentageLogged) {
os << '\r' << title << ": " << percentage << '%';
os.flush();
if (visible) {
os << '\r' << title << ": " << percentage << '%';
os.flush();
}
lastPercentageLogged = percentage;
}
}
Expand All @@ -71,6 +74,8 @@ class ProgressMonitor {
size_t progress;
/// The last percentage that was updated.
unsigned lastPercentageLogged;
/// Display the progress monitor iff true.
bool visible;
};

} // namespace PAF
30 changes: 29 additions & 1 deletion unit-tests/ProgressMonitor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: <text>Copyright 2022-2024 Arm Limited and/or its
* SPDX-FileCopyrightText: <text>Copyright 2022-2025 Arm Limited and/or its
* affiliates <[email protected]></text>
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -60,3 +60,31 @@ TEST(ProgressMonitor, Basic) {
EXPECT_EQ(PM.count(), 4);
EXPECT_EQ(PM.remaining(), 196);
}

TEST(ProgressMonitor, Invisible) {
ostringstream os;
ProgressMonitor PM(os, "MyTitle", 100, /* Visible: */ false);

EXPECT_EQ(os.str(), "");
EXPECT_EQ(PM.total(), 100);
EXPECT_EQ(PM.count(), 0);
EXPECT_EQ(PM.remaining(), 100);

PM.update();
EXPECT_EQ(os.str(), "");
EXPECT_EQ(PM.total(), 100);
EXPECT_EQ(PM.count(), 1);
EXPECT_EQ(PM.remaining(), 99);

PM.update();
EXPECT_EQ(os.str(), "");
EXPECT_EQ(PM.total(), 100);
EXPECT_EQ(PM.count(), 2);
EXPECT_EQ(PM.remaining(), 98);

PM.update(2);
EXPECT_EQ(os.str(), "");
EXPECT_EQ(PM.total(), 100);
EXPECT_EQ(PM.count(), 4);
EXPECT_EQ(PM.remaining(), 96);
}

0 comments on commit 115ebab

Please sign in to comment.