From e30b15ca93983ea45b7e9fd5cdabed5384128206 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Thu, 20 Jun 2024 18:18:12 -0600 Subject: [PATCH] progress bar: factor out activity ancestor precedence logic Change-Id: I2bc736cd176f0147477dfb94dd48a4c8a1f8ec77 --- src/libmain/progress-bar.cc | 43 ++++++++++++++++++++++++++++--------- src/libmain/progress-bar.hh | 10 +++++++-- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc index d97b54f8b..5e177cb8f 100644 --- a/src/libmain/progress-bar.cc +++ b/src/libmain/progress-bar.cc @@ -168,23 +168,29 @@ void ProgressBar::startActivity( i->s = fmt("querying " ANSI_BOLD "%s" ANSI_NORMAL " on %s", name, getS(fields, 1)); } - if ((type == actFileTransfer && hasAncestor(*state, actCopyPath, parent)) - || (type == actFileTransfer && hasAncestor(*state, actQueryPathInfo, parent)) - || (type == actCopyPath && hasAncestor(*state, actSubstitute, parent))) + if (ancestorTakesPrecedence(*state, type, parent)) { i->visible = false; + } update(*state); } -/* Check whether an activity has an ancestore with the specified - type. */ -bool ProgressBar::hasAncestor(State & state, ActivityType type, ActivityId act) +bool ProgressBar::hasAncestor(State & state, ActivityType type, ActivityId act) const { + // 0 is the sentinel value for a non-existent activity ID. while (act != 0) { - auto i = state.its.find(act); - if (i == state.its.end()) break; - if (i->second->type == type) return true; - act = i->second->parent; + auto const foundActIt = state.its.find(act); + if (foundActIt == state.its.end()) { + break; + } + + ActInfo const & foundActInfo = *foundActIt->second; + + if (foundActInfo.type == type) { + return true; + } + + act = foundActInfo.parent; } return false; } @@ -494,6 +500,23 @@ void ProgressBar::setPrintBuildLogs(bool printBuildLogs) this->printBuildLogs = printBuildLogs; } +bool ProgressBar::ancestorTakesPrecedence(State & state, ActivityType type, ActivityId parent) +{ + if (type == actFileTransfer && hasAncestor(state, actCopyPath, parent)) { + return true; + } + + if (type == actFileTransfer && hasAncestor(state, actQueryPathInfo, parent)) { + return true; + } + + if (type == actCopyPath && hasAncestor(state, actSubstitute, parent)) { + return true; + } + + return false; +} + Logger * makeProgressBar() { return new ProgressBar(shouldANSI()); diff --git a/src/libmain/progress-bar.hh b/src/libmain/progress-bar.hh index 76e2ed4ff..5750ff1bd 100644 --- a/src/libmain/progress-bar.hh +++ b/src/libmain/progress-bar.hh @@ -106,7 +106,8 @@ struct ProgressBar : public Logger ActivityId parent ) override; - bool hasAncestor(State & state, ActivityType type, ActivityId act); + /** Check whether an activity has an ancestor with the specified type. */ + bool hasAncestor(State & state, ActivityType type, ActivityId act) const; void stopActivity(ActivityId act) override; @@ -116,13 +117,18 @@ struct ProgressBar : public Logger std::chrono::milliseconds draw(State & state); - std::string getStatus(State & state); + std::string getStatus(State & state) const; void writeToStdout(std::string_view s) override; std::optional ask(std::string_view msg) override; void setPrintBuildLogs(bool printBuildLogs) override; + + /** Returns true if the given activity should not be rendered, + * in lieu of rendering its parent instead. + */ + bool ancestorTakesPrecedence(State & state, ActivityType type, ActivityId parent); }; Logger * makeProgressBar();