extract ProgressBar declaration into its header file

Change-Id: Ica9e2ec41d99eaa196a0d535501edf45c589b2b6
This commit is contained in:
Qyriad 2024-06-19 20:56:28 -06:00
parent 3a4c21fc9e
commit f9594b592b
2 changed files with 503 additions and 447 deletions

View file

@ -36,86 +36,14 @@ static std::string_view storePathToName(std::string_view path)
return i == std::string::npos ? base.substr(0, 0) : base.substr(i + 1);
}
// 100 years ought to be enough for anyone (yet sufficiently smaller than max() to not cause signed integer overflow).
constexpr const auto A_LONG_TIME = std::chrono::duration_cast<std::chrono::milliseconds>(100 * 365 * 86400s);
class ProgressBar : public Logger
{
private:
struct ActInfo
{
std::string s, lastLine, phase;
ActivityType type = actUnknown;
uint64_t done = 0;
uint64_t expected = 0;
uint64_t running = 0;
uint64_t failed = 0;
std::map<ActivityType, uint64_t> expectedByType;
bool visible = true;
ActivityId parent;
std::optional<std::string> name;
std::chrono::time_point<std::chrono::steady_clock> startTime;
};
struct ActivitiesByType
{
std::map<ActivityId, std::list<ActInfo>::iterator> its;
uint64_t done = 0;
uint64_t expected = 0;
uint64_t failed = 0;
};
struct State
{
std::list<ActInfo> activities;
std::map<ActivityId, std::list<ActInfo>::iterator> its;
std::map<ActivityType, ActivitiesByType> activitiesByType;
uint64_t filesLinked = 0, bytesLinked = 0;
uint64_t corruptedPaths = 0, untrustedPaths = 0;
bool active = true;
bool paused = false;
bool haveUpdate = true;
};
Sync<State> state_;
std::thread updateThread;
std::condition_variable quitCV, updateCV;
bool printBuildLogs = false;
bool isTTY;
public:
ProgressBar(bool isTTY)
: isTTY(isTTY)
{
state_.lock()->active = isTTY;
updateThread = std::thread([&]() {
auto state(state_.lock());
auto nextWakeup = A_LONG_TIME;
while (state->active) {
if (!state->haveUpdate)
state.wait_for(updateCV, nextWakeup);
nextWakeup = draw(*state);
state.wait_for(quitCV, std::chrono::milliseconds(50));
}
});
}
~ProgressBar()
ProgressBar::~ProgressBar()
{
stop();
}
/* Called by destructor, can't be overridden */
void stop() override final
void ProgressBar::stop()
{
{
auto state(state_.lock());
@ -128,31 +56,33 @@ public:
updateThread.join();
}
void pause() override {
void ProgressBar::pause()
{
state_.lock()->paused = true;
writeToStderr("\r\e[K");
}
void resume() override {
void ProgressBar::resume()
{
state_.lock()->paused = false;
writeToStderr("\r\e[K");
state_.lock()->haveUpdate = true;
updateCV.notify_one();
}
bool isVerbose() override
bool ProgressBar::isVerbose()
{
return printBuildLogs;
}
void log(Verbosity lvl, std::string_view s) override
void ProgressBar::log(Verbosity lvl, std::string_view s)
{
if (lvl > verbosity) return;
auto state(state_.lock());
log(*state, lvl, s);
}
void logEI(const ErrorInfo & ei) override
void ProgressBar::logEI(const ErrorInfo & ei)
{
auto state(state_.lock());
@ -162,7 +92,7 @@ public:
log(*state, ei.level, oss.str());
}
void log(State & state, Verbosity lvl, std::string_view s)
void ProgressBar::log(State & state, Verbosity lvl, std::string_view s)
{
if (state.active) {
writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n");
@ -174,8 +104,14 @@ public:
}
}
void startActivity(ActivityId act, Verbosity lvl, ActivityType type,
const std::string & s, const Fields & fields, ActivityId parent) override
void ProgressBar::startActivity(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent
)
{
auto state(state_.lock());
@ -242,7 +178,7 @@ public:
/* Check whether an activity has an ancestore with the specified
type. */
bool hasAncestor(State & state, ActivityType type, ActivityId act)
bool ProgressBar::hasAncestor(State & state, ActivityType type, ActivityId act)
{
while (act != 0) {
auto i = state.its.find(act);
@ -253,7 +189,7 @@ public:
return false;
}
void stopActivity(ActivityId act) override
void ProgressBar::stopActivity(ActivityId act)
{
auto state(state_.lock());
@ -275,7 +211,7 @@ public:
update(*state);
}
void result(ActivityId act, ResultType type, const std::vector<Field> & fields) override
void ProgressBar::result(ActivityId act, ResultType type, const std::vector<Field> & fields)
{
auto state(state_.lock());
@ -348,13 +284,13 @@ public:
}
}
void update(State & state)
void ProgressBar::update(State & state)
{
state.haveUpdate = true;
updateCV.notify_one();
}
std::chrono::milliseconds draw(State & state)
std::chrono::milliseconds ProgressBar::draw(State & state)
{
auto nextWakeup = A_LONG_TIME;
@ -412,7 +348,7 @@ public:
return nextWakeup;
}
std::string getStatus(State & state)
std::string ProgressBar::getStatus(State & state)
{
constexpr auto MiB = 1024.0 * 1024.0;
@ -502,7 +438,7 @@ public:
return res;
}
void writeToStdout(std::string_view s) override
void ProgressBar::writeToStdout(std::string_view s)
{
auto state(state_.lock());
if (state->active) {
@ -514,7 +450,7 @@ public:
}
}
std::optional<char> ask(std::string_view msg) override
std::optional<char> ProgressBar::ask(std::string_view msg)
{
auto state(state_.lock());
if (!state->active || !isatty(STDIN_FILENO)) return {};
@ -525,11 +461,10 @@ public:
return s[0];
}
void setPrintBuildLogs(bool printBuildLogs) override
void ProgressBar::setPrintBuildLogs(bool printBuildLogs)
{
this->printBuildLogs = printBuildLogs;
}
};
Logger * makeProgressBar()
{

View file

@ -1,10 +1,131 @@
#pragma once
///@file
#include <chrono>
#include "logging.hh"
#include "sync.hh"
namespace nix {
// 100 years ought to be enough for anyone (yet sufficiently smaller than max() to not cause signed integer overflow).
constexpr const auto A_LONG_TIME = std::chrono::duration_cast<std::chrono::milliseconds>(
100 * 365 * std::chrono::seconds(86400)
);
class ProgressBar : public Logger
{
private:
struct ActInfo
{
std::string s, lastLine, phase;
ActivityType type = actUnknown;
uint64_t done = 0;
uint64_t expected = 0;
uint64_t running = 0;
uint64_t failed = 0;
std::map<ActivityType, uint64_t> expectedByType;
bool visible = true;
ActivityId parent;
std::optional<std::string> name;
std::chrono::time_point<std::chrono::steady_clock> startTime;
};
struct ActivitiesByType
{
std::map<ActivityId, std::list<ActInfo>::iterator> its;
uint64_t done = 0;
uint64_t expected = 0;
uint64_t failed = 0;
};
struct State
{
std::list<ActInfo> activities;
std::map<ActivityId, std::list<ActInfo>::iterator> its;
std::map<ActivityType, ActivitiesByType> activitiesByType;
uint64_t filesLinked = 0, bytesLinked = 0;
uint64_t corruptedPaths = 0, untrustedPaths = 0;
bool active = true;
bool paused = false;
bool haveUpdate = true;
};
Sync<State> state_;
std::thread updateThread;
std::condition_variable quitCV, updateCV;
bool printBuildLogs = false;
bool isTTY;
public:
ProgressBar(bool isTTY)
: isTTY(isTTY)
{
state_.lock()->active = isTTY;
updateThread = std::thread([&]() {
auto state(state_.lock());
auto nextWakeup = A_LONG_TIME;
while (state->active) {
if (!state->haveUpdate)
state.wait_for(updateCV, nextWakeup);
nextWakeup = draw(*state);
state.wait_for(quitCV, std::chrono::milliseconds(50));
}
});
}
~ProgressBar();
void stop() override final;
void pause() override;
void resume() override;
bool isVerbose() override;
void log(Verbosity lvl, std::string_view s) override;
void logEI(const ErrorInfo & ei) override;
void log(State & state, Verbosity lvl, std::string_view s);
void startActivity(
ActivityId act,
Verbosity lvl,
ActivityType type,
const std::string & s,
const Fields & fields,
ActivityId parent
) override;
bool hasAncestor(State & state, ActivityType type, ActivityId act);
void stopActivity(ActivityId act) override;
void result(ActivityId act, ResultType type, const std::vector<Field> & fields) override;
void update(State & state);
std::chrono::milliseconds draw(State & state);
std::string getStatus(State & state);
void writeToStdout(std::string_view s) override;
std::optional<char> ask(std::string_view msg) override;
void setPrintBuildLogs(bool printBuildLogs) override;
};
Logger * makeProgressBar();
void startProgressBar();