mirror of
https://github.com/Sude-/lgogdownloader.git
synced 2024-11-20 11:49:17 +01:00
Make progress bar adapt to terminal width
This commit is contained in:
parent
24105b07fe
commit
d04cfbae1f
@ -36,6 +36,7 @@ namespace Util
|
||||
int replaceString(std::string& str, const std::string& to_replace, const std::string& replace_with);
|
||||
void filepathReplaceReservedStrings(std::string& str, const std::string& gamename, const unsigned int& platformId = 0, const std::string& dlcname = "");
|
||||
void setFilePermissions(const boost::filesystem::path& path, const boost::filesystem::perms& permissions);
|
||||
int getTerminalWidth();
|
||||
}
|
||||
|
||||
#endif // UTIL_H
|
||||
|
@ -1624,7 +1624,8 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
|
||||
{
|
||||
// on entry: dltotal - how much remains to download till the end of the file (bytes)
|
||||
// dlnow - how much was downloaded from the start of the program (bytes)
|
||||
unsigned int bar_length = 26;
|
||||
int bar_length = 26;
|
||||
int min_bar_length = 5;
|
||||
Downloader* downloader = static_cast<Downloader*>(clientp);
|
||||
|
||||
double rate; // average download speed in B/s
|
||||
@ -1660,6 +1661,7 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
|
||||
if (downloader->timer.getTimeBetweenUpdates()>=100 || dlnow == dltotal)
|
||||
{
|
||||
downloader->timer.reset();
|
||||
int iTermWidth = Util::getTerminalWidth();
|
||||
bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate)));
|
||||
std::stringstream eta_ss;
|
||||
if (eta.hours() > 23)
|
||||
@ -1690,7 +1692,6 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
|
||||
|
||||
// assuming that config is provided.
|
||||
printf("\033[0K\r%3.0f%% ", fraction * 100);
|
||||
downloader->progressbar->draw(bar_length, fraction);
|
||||
|
||||
// Download rate unit conversion
|
||||
std::string rate_unit;
|
||||
@ -1704,8 +1705,18 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
|
||||
rate /= 1024;
|
||||
rate_unit = "kB/s";
|
||||
}
|
||||
printf(" %0.2f/%0.2fMB @ %0.2f%s ETA: %s\r", dlnow/1024/1024, dltotal/1024/1024, rate, rate_unit.c_str(), eta_ss.str().c_str());
|
||||
fflush(stdout);
|
||||
char status_text[200]; // We're probably never going to go as high as 200 characters but it's better to use too big number here than too small
|
||||
sprintf(status_text, " %0.2f/%0.2fMB @ %0.2f%s ETA: %s\r", dlnow/1024/1024, dltotal/1024/1024, rate, rate_unit.c_str(), eta_ss.str().c_str());
|
||||
int status_text_length = strlen(status_text) + 6;
|
||||
|
||||
if ((status_text_length + bar_length) > iTermWidth)
|
||||
bar_length -= (status_text_length + bar_length) - iTermWidth;
|
||||
|
||||
// Don't draw progressbar if length is less than min_bar_length
|
||||
if (bar_length >= min_bar_length)
|
||||
downloader->progressbar->draw(bar_length, fraction);
|
||||
|
||||
std::cout << status_text << std::flush;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <tinyxml.h>
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <fstream>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
/*
|
||||
Create filepath from specified directory and path
|
||||
@ -334,3 +335,10 @@ void Util::setFilePermissions(const boost::filesystem::path& path, const boost::
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Util::getTerminalWidth()
|
||||
{
|
||||
struct winsize w;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
return static_cast<int>(w.ws_col);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user