From 34df2cc41684c696f28d1f83413ac270385f53ff Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 11 Mar 2008 16:18:33 +0000 Subject: [PATCH] 2008-03-12 Tatsuhiro Tsujikawa Now download line is printed nicely with no garbage at the end of line. I use ioctl to get the columns of terminal. If stdout is redirected to another device, instead of carriage return, end of line '\n' character is used. This is feature request#1909659 * src/ConsoleStatCalc.cc (calculateStat) --- ChangeLog | 8 ++++++++ src/ConsoleStatCalc.cc | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 63031ac1..64347b13 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-03-12 Tatsuhiro Tsujikawa + + Now download line is printed nicely with no garbage at the end of + line. I use ioctl to get the columns of terminal. + If stdout is redirected to another device, instead of carriage return, + end of line '\n' character is used. This is feature request#1909659 + * src/ConsoleStatCalc.cc (calculateStat) + 2008-03-11 Tatsuhiro Tsujikawa Included a2time.h diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index 39a8c5b9..51b0a7f3 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -43,6 +43,9 @@ #ifdef ENABLE_BITTORRENT # include "BtContext.h" #endif // ENABLE_BITTORRENT +#include +#include +#include #include #include @@ -57,9 +60,14 @@ ConsoleStatCalc::calculateStat(const RequestGroupManHandle& requestGroupMan, return; } _cp.reset(); - - std::cout << "\r "; - std::cout << "\r"; + bool isTTY = isatty(STDOUT_FILENO) == 1; + if(isTTY) { + struct winsize size; + if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == -1) { + size.ws_col = 80; + } + std::cout << '\r' << std::setw(size.ws_col) << ' ' << '\r'; + } if(requestGroupMan->countRequestGroup() > 0) { RequestGroupHandle firstRequestGroup = requestGroupMan->getRequestGroup(0); TransferStat stat = firstRequestGroup->calculateStat(); @@ -177,7 +185,11 @@ ConsoleStatCalc::calculateStat(const RequestGroupManHandle& requestGroupMan, } } #endif // ENABLE_MESSAGE_DIGEST - std::cout << std::flush; + if(isTTY) { + std::cout << std::flush; + } else { + std::cout << std::endl; + } } } // namespace aria2