File: download_progress_bar.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (255 lines) | stat: -rw-r--r-- 9,060 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "libdnf5-cli/progressbar/download_progress_bar.hpp"

#include "../utils/utf8.hpp"

#include "libdnf5-cli/tty.hpp"

#include <algorithm>
#include <iomanip>


namespace libdnf5::cli::progressbar {


// Columns (widgets):
// <numbers><description...................><%%%><progress............><speed.....><size.....><time....>

// Example:
// [  1/999] filename                                                 | 123.4 kB/s | 123.4 kB |  00m01s
// [  2/999] filename                       100% [====================] 123.4 kB/s | 123.4 kB | -00m02s
// ----------------------------------------------------------------------------------------------------
// [  1/999] Total                           10% [===                 ] 543.2 kB/s | 543.2 MB | -12m34s


class DownloadProgressBar::Impl {
public:
    Impl(DownloadProgressBar & download_progress_bar);

    // widgets
    libdnf5::cli::progressbar::NumberWidget number_widget;
    libdnf5::cli::progressbar::DescriptionWidget description_widget;
    libdnf5::cli::progressbar::PercentWidget percent_widget;
    libdnf5::cli::progressbar::ProgressWidget progress_widget;
    libdnf5::cli::progressbar::SpeedWidget speed_widget;
    libdnf5::cli::progressbar::SizeWidget size_widget;
    libdnf5::cli::progressbar::TimeWidget time_widget;

private:
    DownloadProgressBar * download_progress_bar;
};

DownloadProgressBar::Impl::Impl(DownloadProgressBar & download_progress_bar)
    : download_progress_bar(&download_progress_bar) {
    number_widget.set_bar(this->download_progress_bar);
    description_widget.set_bar(this->download_progress_bar);
    percent_widget.set_bar(this->download_progress_bar);
    progress_widget.set_bar(this->download_progress_bar);
    speed_widget.set_bar(this->download_progress_bar);
    size_widget.set_bar(this->download_progress_bar);
    time_widget.set_bar(this->download_progress_bar);
}

DownloadProgressBar::~DownloadProgressBar() = default;
DownloadProgressBar::DownloadProgressBar(const DownloadProgressBar & src) = default;
DownloadProgressBar & DownloadProgressBar::operator=(const DownloadProgressBar & src) = default;
DownloadProgressBar::DownloadProgressBar(DownloadProgressBar && src) noexcept = default;
DownloadProgressBar & DownloadProgressBar::operator=(DownloadProgressBar && src) noexcept = default;

DownloadProgressBar::DownloadProgressBar(int64_t download_size, const std::string & description)
    : ProgressBar(download_size, description),
      p_impl(new Impl(*this)) {}


static std::size_t get_bar_width(const std::vector<Widget *> & widgets) {
    std::size_t result = 0;
    for (Widget * widget : widgets) {
        result += widget->get_total_width();
    }
    return result;
}


std::ostream & operator<<(std::ostream & stream, DownloadProgressBar & bar) {
    bar.to_stream(stream);
    return stream;
}


bool DownloadProgressBar::get_number_widget_visible() const noexcept {
    return p_impl->number_widget.get_visible();
}

void DownloadProgressBar::set_number_widget_visible(bool value) noexcept {
    p_impl->number_widget.set_visible(value);
}

void DownloadProgressBar::to_stream(std::ostream & stream) {
    update();

    if (!is_finished() && !tty::is_interactive()) {
        // don't spam non-interactive terminal with progressbar's progress
        // only print the final form
        return;
    }

    // set the default delimiters
    p_impl->number_widget.set_delimiter_before("");
    p_impl->description_widget.set_delimiter_before(" ");
    p_impl->percent_widget.set_delimiter_before(" ");
    p_impl->progress_widget.set_delimiter_before(" ");
    p_impl->speed_widget.set_delimiter_before(" | ");
    p_impl->size_widget.set_delimiter_before(" | ");
    p_impl->time_widget.set_delimiter_before(" | ");

    // set the default description widget width
    // we'll increase the size if terminal width allows
    p_impl->description_widget.set_width(21);

    // create vector of all widgets
    std::vector<Widget *> widgets = {
        &p_impl->number_widget,
        &p_impl->description_widget,
        &p_impl->percent_widget,
        &p_impl->progress_widget,
        &p_impl->speed_widget,
        &p_impl->size_widget,
        &p_impl->time_widget,
    };

    // remove widgets that are not visible
    for (auto it = widgets.begin(); it != widgets.end();) {
        if ((*it)->get_visible()) {
            it++;
        } else {
            it = widgets.erase(it);
        }
    }

    std::size_t terminal_width = static_cast<std::size_t>(tty::get_width());

    // if bar doesn't fit terminal width, hide progress widget
    std::size_t bar_width = get_bar_width(widgets);
    if (bar_width > terminal_width) {
        widgets.erase(std::remove(widgets.begin(), widgets.end(), &p_impl->progress_widget), widgets.end());
        bar_width = get_bar_width(widgets);
    }

    // if bar doesn't fit terminal width, hide speed widget
    if (bar_width > terminal_width) {
        widgets.erase(std::remove(widgets.begin(), widgets.end(), &p_impl->speed_widget), widgets.end());
        p_impl->speed_widget.set_delimiter_before(" ");
        bar_width = get_bar_width(widgets);
    }

    // if bar doesn't fit terminal width, hide time widget
    if (bar_width > terminal_width) {
        widgets.erase(std::remove(widgets.begin(), widgets.end(), &p_impl->time_widget), widgets.end());
        bar_width = get_bar_width(widgets);
    }

    // if bar is finished, hide the progress widget
    if (get_state() != ProgressBarState::STARTED) {
        widgets.erase(std::remove(widgets.begin(), widgets.end(), &p_impl->progress_widget), widgets.end());
        bar_width = get_bar_width(widgets);
    }

    // if bar doesn't fit terminal width, reduce description width
    if (bar_width > terminal_width) {
        p_impl->description_widget.set_total_width(
            p_impl->description_widget.get_total_width() + terminal_width - bar_width);
        bar_width = get_bar_width(widgets);
    }

    if (bar_width < terminal_width) {
        p_impl->description_widget.set_total_width(
            p_impl->description_widget.get_total_width() + terminal_width - bar_width);
        bar_width = get_bar_width(widgets);
    }

    bool color_used = false;

    if (tty::is_coloring_enabled()) {
        switch (get_state()) {
            case ProgressBarState::READY:
            case ProgressBarState::STARTED:
            case ProgressBarState::SUCCESS:
                break;
            case ProgressBarState::WARNING:
                stream << tty::yellow;
                color_used = true;
                break;
            case ProgressBarState::ERROR:
                stream << tty::red;
                color_used = true;
                break;
        }
    }

    for (Widget * widget : widgets) {
        stream << std::left;
        // if string is shorter, fill the widget's space with spaces
        stream << std::setw(static_cast<int>(widget->get_total_width()));
        // print only the part of the string that fits the widget width
        stream << widget->to_string().substr(0, widget->get_total_width());
    }

    if (color_used) {
        stream << tty::reset;
    }

    for (const auto & [message_type, message] : get_messages()) {
        const auto & prefix = get_message_prefix();

        stream << std::endl << prefix;

        color_used = false;
        if (tty::is_coloring_enabled()) {
            // color the message in interactive terminal
            switch (message_type) {
                case MessageType::INFO:
                    break;
                case MessageType::SUCCESS:
                    stream << tty::green;
                    color_used = true;
                    break;
                case MessageType::WARNING:
                    stream << tty::yellow;
                    color_used = true;
                    break;
                case MessageType::ERROR:
                    stream << tty::red;
                    color_used = true;
                    break;
            }
        }

        stream << message;

        if (color_used) {
            stream << tty::reset;
        }
    }
}


}  // namespace libdnf5::cli::progressbar