Procházet zdrojové kódy

2008-03-12 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	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)
Tatsuhiro Tsujikawa před 17 roky
rodič
revize
34df2cc416
2 změnil soubory, kde provedl 24 přidání a 4 odebrání
  1. 8 0
      ChangeLog
  2. 16 4
      src/ConsoleStatCalc.cc

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2008-03-12  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	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  <tujikawa at rednoah dot com>
 
 	Included a2time.h

+ 16 - 4
src/ConsoleStatCalc.cc

@@ -43,6 +43,9 @@
 #ifdef ENABLE_BITTORRENT
 # include "BtContext.h"
 #endif // ENABLE_BITTORRENT
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
 #include <iomanip>
 #include <iostream>
 
@@ -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