2016-03-29 12:29:50 +00:00
|
|
|
#include "progress-bar.hh"
|
2016-04-25 13:26:07 +00:00
|
|
|
#include "util.hh"
|
|
|
|
#include "sync.hh"
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
#include <map>
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
class ProgressBar : public Logger
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2016-04-25 13:26:07 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
struct ActInfo
|
|
|
|
{
|
|
|
|
Activity * activity;
|
|
|
|
Verbosity lvl;
|
|
|
|
std::string s;
|
2016-03-29 12:29:50 +00:00
|
|
|
};
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
struct Progress
|
|
|
|
{
|
|
|
|
uint64_t expected = 0, progress = 0;
|
|
|
|
};
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
std::list<ActInfo> activities;
|
|
|
|
std::map<Activity *, std::list<ActInfo>::iterator> its;
|
|
|
|
std::map<std::string, Progress> progress;
|
|
|
|
};
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
Sync<State> state_;
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
~ProgressBar()
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
assert(state->activities.empty());
|
|
|
|
writeToStderr("\r\e[K");
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
void log(Verbosity lvl, const FormatOrString & fs) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
log(*state, lvl, fs.s);
|
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
void log(State & state, Verbosity lvl, const std::string & s)
|
|
|
|
{
|
|
|
|
writeToStderr("\r\e[K" + s + "\n");
|
|
|
|
update(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void startActivity(Activity & activity, Verbosity lvl, const FormatOrString & fs) override
|
|
|
|
{
|
|
|
|
if (lvl > verbosity) return;
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->activities.emplace_back(ActInfo{&activity, lvl, fs.s});
|
|
|
|
state->its.emplace(&activity, std::prev(state->activities.end()));
|
|
|
|
update(*state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopActivity(Activity & activity) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
auto i = state->its.find(&activity);
|
|
|
|
if (i == state->its.end()) return;
|
|
|
|
state->activities.erase(i->second);
|
|
|
|
state->its.erase(i);
|
|
|
|
update(*state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setExpected(const std::string & label, uint64_t value) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->progress[label].expected = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setProgress(const std::string & label, uint64_t value) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->progress[label].progress = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void incExpected(const std::string & label, uint64_t value) override
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->progress[label].expected += value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void incProgress(const std::string & label, uint64_t value)
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->progress[label].progress += value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update()
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(State & state)
|
|
|
|
{
|
|
|
|
std::string line = "\r";
|
|
|
|
|
|
|
|
std::string status = getStatus(state);
|
|
|
|
if (!status.empty()) {
|
|
|
|
line += '[';
|
|
|
|
line += status;
|
|
|
|
line += "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.activities.empty()) {
|
|
|
|
if (!status.empty()) line += " ";
|
|
|
|
line += state.activities.rbegin()->s;
|
|
|
|
}
|
|
|
|
|
|
|
|
line += "\e[K";
|
|
|
|
writeToStderr(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getStatus(State & state)
|
|
|
|
{
|
|
|
|
std::string res;
|
|
|
|
for (auto & p : state.progress)
|
|
|
|
if (p.second.expected || p.second.progress) {
|
|
|
|
if (!res.empty()) res += ", ";
|
|
|
|
res += std::to_string(p.second.progress);
|
|
|
|
if (p.second.expected) {
|
|
|
|
res += "/";
|
|
|
|
res += std::to_string(p.second.expected);
|
|
|
|
}
|
|
|
|
res += " "; res += p.first;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
StartProgressBar::StartProgressBar()
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2016-04-25 13:26:07 +00:00
|
|
|
if (isatty(STDERR_FILENO)) {
|
|
|
|
prev = logger;
|
|
|
|
logger = new ProgressBar();
|
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
StartProgressBar::~StartProgressBar()
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2016-04-25 13:26:07 +00:00
|
|
|
if (prev) {
|
|
|
|
auto bar = logger;
|
|
|
|
logger = prev;
|
|
|
|
delete bar;
|
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|