Make progress bar adapt to terminal width

This commit is contained in:
Sude 2014-11-27 23:20:22 +02:00
parent 24105b07fe
commit d04cfbae1f
3 changed files with 24 additions and 4 deletions

View File

@ -36,6 +36,7 @@ namespace Util
int replaceString(std::string& str, const std::string& to_replace, const std::string& replace_with); 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 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); void setFilePermissions(const boost::filesystem::path& path, const boost::filesystem::perms& permissions);
int getTerminalWidth();
} }
#endif // UTIL_H #endif // UTIL_H

View File

@ -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) // 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) // 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); Downloader* downloader = static_cast<Downloader*>(clientp);
double rate; // average download speed in B/s 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) if (downloader->timer.getTimeBetweenUpdates()>=100 || dlnow == dltotal)
{ {
downloader->timer.reset(); downloader->timer.reset();
int iTermWidth = Util::getTerminalWidth();
bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate))); bptime::time_duration eta(bptime::seconds((long)((dltotal - dlnow) / rate)));
std::stringstream eta_ss; std::stringstream eta_ss;
if (eta.hours() > 23) if (eta.hours() > 23)
@ -1690,7 +1692,6 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
// assuming that config is provided. // assuming that config is provided.
printf("\033[0K\r%3.0f%% ", fraction * 100); printf("\033[0K\r%3.0f%% ", fraction * 100);
downloader->progressbar->draw(bar_length, fraction);
// Download rate unit conversion // Download rate unit conversion
std::string rate_unit; std::string rate_unit;
@ -1704,8 +1705,18 @@ int Downloader::progressCallback(void *clientp, double dltotal, double dlnow, do
rate /= 1024; rate /= 1024;
rate_unit = "kB/s"; 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()); 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
fflush(stdout); 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; return 0;

View File

@ -11,6 +11,7 @@
#include <tinyxml.h> #include <tinyxml.h>
#include <jsoncpp/json/json.h> #include <jsoncpp/json/json.h>
#include <fstream> #include <fstream>
#include <sys/ioctl.h>
/* /*
Create filepath from specified directory and path 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);
}