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

View File

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