aboutsummaryrefslogtreecommitdiff
path: root/src/nix/progress-bar.cc
blob: 659d6572ad932938e5c70c9c4fd9b2cea83ffa35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "progress-bar.hh"
#include "util.hh"
#include "sync.hh"

#include <map>

namespace nix {

class ProgressBar : public Logger
{
private:

    struct ActInfo
    {
        Activity * activity;
        Verbosity lvl;
        std::string s;
    };

    struct Progress
    {
        uint64_t expected = 0, progress = 0;
    };

    struct State
    {
        std::list<ActInfo> activities;
        std::map<Activity *, std::list<ActInfo>::iterator> its;
        std::map<std::string, Progress> progress;
    };

    Sync<State> state_;

public:

    ~ProgressBar()
    {
        auto state(state_.lock());
        assert(state->activities.empty());
        writeToStderr("\r\e[K");
    }

    void log(Verbosity lvl, const FormatOrString & fs) override
    {
        auto state(state_.lock());
        log(*state, lvl, fs.s);
    }

    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;
    }
};

StartProgressBar::StartProgressBar()
{
    if (isatty(STDERR_FILENO)) {
        prev = logger;
        logger = new ProgressBar();
    }
}

StartProgressBar::~StartProgressBar()
{
    if (prev) {
        auto bar = logger;
        logger = prev;
        delete bar;
    }
}

}