-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[utils] Add an option to make the ProgressMonitor visible or not.
- Loading branch information
1 parent
d1d0101
commit 115ebab
Showing
2 changed files
with
39 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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(); | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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); | ||
} |