2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "textprogressbar.h"
|
2017-09-27 13:33:25 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QByteArray>
|
|
|
|
|
2017-09-27 13:33:25 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
using namespace std;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void TextProgressBar::clear()
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
value = 0;
|
|
|
|
maximum = -1;
|
2017-09-27 13:33:25 +00:00
|
|
|
iteration = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextProgressBar::update()
|
|
|
|
{
|
|
|
|
++iteration;
|
|
|
|
|
|
|
|
if (maximum > 0) {
|
|
|
|
// we know the maximum
|
|
|
|
// draw a progress bar
|
|
|
|
int percent = value * 100 / maximum;
|
|
|
|
int hashes = percent / 2;
|
|
|
|
|
|
|
|
QByteArray progressbar(hashes, '#');
|
|
|
|
if (percent % 2)
|
|
|
|
progressbar += '>';
|
|
|
|
|
|
|
|
printf("\r[%-50s] %3d%% %s ",
|
|
|
|
progressbar.constData(),
|
|
|
|
percent,
|
|
|
|
qPrintable(message));
|
|
|
|
} else {
|
|
|
|
// we don't know the maximum, so we can't draw a progress bar
|
|
|
|
int center = (iteration % 48) + 1; // 50 spaces, minus 2
|
|
|
|
QByteArray before(qMax(center - 2, 0), ' ');
|
|
|
|
QByteArray after(qMin(center + 2, 50), ' ');
|
|
|
|
|
|
|
|
printf("\r[%s###%s] %s ",
|
|
|
|
before.constData(), after.constData(), qPrintable(message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextProgressBar::setMessage(const QString &m)
|
|
|
|
{
|
|
|
|
message = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextProgressBar::setStatus(qint64 val, qint64 max)
|
|
|
|
{
|
|
|
|
value = val;
|
|
|
|
maximum = max;
|
|
|
|
}
|