diff --git a/include/PAF/utils/ProgressMonitor.h b/include/PAF/utils/ProgressMonitor.h index 0213a89..8ba3962 100644 --- a/include/PAF/utils/ProgressMonitor.h +++ b/include/PAF/utils/ProgressMonitor.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright 2022,2024 Arm Limited and/or its + * SPDX-FileCopyrightText: Copyright 2022,2024,2025 Arm Limited and/or its * affiliates * 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 diff --git a/unit-tests/ProgressMonitor.cpp b/unit-tests/ProgressMonitor.cpp index 4874097..820487f 100644 --- a/unit-tests/ProgressMonitor.cpp +++ b/unit-tests/ProgressMonitor.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its + * SPDX-FileCopyrightText: Copyright 2022-2025 Arm Limited and/or its * affiliates * 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); +}