Changed: #1051 Fixed some display problems

This commit is contained in:
kervala 2010-10-10 23:12:37 +02:00
parent ce3552b3d5
commit df1a44c57a

View file

@ -78,40 +78,54 @@ void printCheck(const std::string &str)
void printDownload(const std::string &str) void printDownload(const std::string &str)
{ {
static uint previousLength = 0; static char spaces[80];
static const uint maxLength = 160; uint maxLength = 80;
static char spaces[maxLength];
// if "COLUMNS" environnement variable is defined, use it
if (getenv("COLUMNS"))
{
NLMISC::fromString(std::string(getenv("COLUMNS")), maxLength);
}
// only use 79 columns to not wrap
--maxLength;
// temporary modified string
std::string nstr = str;
uint length = 0; uint length = 0;
if (useUtf8) if (useUtf8)
{ {
ucstring utf8Str; ucstring ucstr;
utf8Str.fromUtf8(str); ucstr.fromUtf8(nstr);
length = (uint)utf8Str.length(); length = (uint)ucstr.length();
if (length > maxLength)
{
ucstr = ucstr.luabind_substr(length - maxLength - 3);
nstr = std::string("...") + ucstr.toUtf8();
length = maxLength;
}
} }
else else
{ {
length = (uint)str.length(); length = (uint)nstr.length();
if (length > maxLength)
{
nstr = std::string("...") + nstr.substr(length - maxLength - 3);
length = maxLength;
}
} }
sint diff = length - previousLength; // add padding with spaces
memset(spaces, ' ', maxLength);
if (diff > 0 && length < maxLength) spaces[maxLength - length] = '\0';
{
memset(spaces, ' ', length);
spaces[length] = '\0';
// "erase" previous line
printf("%s\r", spaces);
fflush(stdout);
}
// display download in purple // display download in purple
if (useEsc) if (useEsc)
{ {
printf("\033[1;35m%s\033[0m\r", str.c_str()); printf("\033[1;35m%s%s\033[0m\r", nstr.c_str(), spaces);
} }
else else
{ {
@ -120,7 +134,7 @@ void printDownload(const std::string &str)
SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY); SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
#endif #endif
printf("%s\r", str.c_str()); printf("%s%s\r", nstr.c_str(), spaces);
#ifdef NL_OS_WINDOWS #ifdef NL_OS_WINDOWS
if (hStdout != INVALID_HANDLE_VALUE && hStdout) if (hStdout != INVALID_HANDLE_VALUE && hStdout)
@ -129,8 +143,6 @@ void printDownload(const std::string &str)
} }
fflush(stdout); fflush(stdout);
previousLength = length;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])