progress bar: factor out activity ancestor precedence logic

Change-Id: I2bc736cd176f0147477dfb94dd48a4c8a1f8ec77
This commit is contained in:
Qyriad 2024-06-20 18:18:12 -06:00
parent 5623e759ec
commit e30b15ca93
2 changed files with 41 additions and 12 deletions

View file

@ -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());

View file

@ -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<char> 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();