Check the output file is terminal or not in ctor

pull/36/head
Tatsuhiro Tsujikawa 2012-12-08 18:38:30 +09:00
parent 9d7bb9d01a
commit bf56f3c299
2 changed files with 11 additions and 6 deletions

View File

@ -238,7 +238,12 @@ void printProgressSummary
ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
summaryInterval_(summaryInterval),
readoutVisibility_(true),
truncate_(true)
truncate_(true),
#ifdef __MINGW32__
isTTY_(true)
#else // !__MINGW32__
isTTY_(isatty(STDOUT_FILENO) == 1)
#endif // !__MINGW32__
{
if(humanReadable) {
sizeFormatter_.reset(new AbbrevSizeFormatter());
@ -257,15 +262,14 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
const SizeFormatter& sizeFormatter = *sizeFormatter_.get();
#ifdef __MINGW32__
bool isTTY = true;
// Windows terminal cannot handle at the end of line properly.
// Windows terminal cannot handle at the end of line (80 columns)
// properly.
unsigned short int cols = 79;
#else // !__MINGW32__
bool isTTY = isatty(STDOUT_FILENO) == 1;
unsigned short int cols = 80;
#endif // !__MINGW32__
if(isTTY) {
if(isTTY_) {
#ifndef __MINGW32__
#ifdef HAVE_TERMIOS_H
struct winsize size;
@ -368,7 +372,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
}
#endif // ENABLE_MESSAGE_DIGEST
std::string readout = o.str();
if(isTTY) {
if(isTTY_) {
if(truncate_ && readout.size() > cols) {
readout[cols] = '\0';
}

View File

@ -65,6 +65,7 @@ private:
SharedHandle<SizeFormatter> sizeFormatter_;
bool readoutVisibility_;
bool truncate_;
bool isTTY_;
public:
ConsoleStatCalc(time_t summaryInterval, bool humanReadable = true);