Explorar o código

2010-01-17 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added --human-readable option.  This option, when true is given,
	prints sizes and speed in human readable format(e.g., 1.2Ki,
	3.4Mi) in the console readout. The default value is true and it
	looks exactly the same as aria2-1.8.0.  So the 'new feature'
	appears when false is given.  In this case, sizes and speed are
	printed without in bytes. No Ki, Mi units conversion is used.
	This may be useful for a program to parse the output of aria2.
	* doc/aria2c.1.txt
	* src/ConsoleStatCalc.cc
	* src/ConsoleStatCalc.h
	* src/OptionHandlerFactory.cc
	* src/main.cc
	* src/prefs.cc
	* src/prefs.h
	* src/usage_text.h
Tatsuhiro Tsujikawa %!s(int64=16) %!d(string=hai) anos
pai
achega
c0308e1ea4
Modificáronse 11 ficheiros con 125 adicións e 25 borrados
  1. 18 0
      ChangeLog
  2. 8 2
      doc/aria2c.1
  3. 10 1
      doc/aria2c.1.html
  4. 5 0
      doc/aria2c.1.txt
  5. 34 20
      src/ConsoleStatCalc.cc
  6. 32 1
      src/ConsoleStatCalc.h
  7. 9 0
      src/OptionHandlerFactory.cc
  8. 2 1
      src/main.cc
  9. 2 0
      src/prefs.cc
  10. 2 0
      src/prefs.h
  11. 3 0
      src/usage_text.h

+ 18 - 0
ChangeLog

@@ -1,3 +1,21 @@
+2010-01-17  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added --human-readable option.  This option, when true is given,
+	prints sizes and speed in human readable format(e.g., 1.2Ki,
+	3.4Mi) in the console readout. The default value is true and it
+	looks exactly the same as aria2-1.8.0.  So the 'new feature'
+	appears when false is given.  In this case, sizes and speed are
+	printed without in bytes. No Ki, Mi units conversion is used.
+	This may be useful for a program to parse the output of aria2.
+	* doc/aria2c.1.txt
+	* src/ConsoleStatCalc.cc
+	* src/ConsoleStatCalc.h
+	* src/OptionHandlerFactory.cc
+	* src/main.cc
+	* src/prefs.cc
+	* src/prefs.h
+	* src/usage_text.h
+
 2010-01-15  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 2010-01-15  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 
 	Now --all-proxy, --http-proxy, --https-proxy and --ftp-proxy
 	Now --all-proxy, --http-proxy, --https-proxy and --ftp-proxy

+ 8 - 2
doc/aria2c.1

@@ -2,12 +2,12 @@
 .\"     Title: aria2c
 .\"     Title: aria2c
 .\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
 .\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
 .\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
 .\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
-.\"      Date: 01/15/2010
+.\"      Date: 01/17/2010
 .\"    Manual: Aria2 Manual
 .\"    Manual: Aria2 Manual
 .\"    Source: Aria2
 .\"    Source: Aria2
 .\"  Language: English
 .\"  Language: English
 .\"
 .\"
-.TH "ARIA2C" "1" "01/15/2010" "Aria2" "Aria2 Manual"
+.TH "ARIA2C" "1" "01/17/2010" "Aria2" "Aria2 Manual"
 .\" -----------------------------------------------------------------
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
 .\" -----------------------------------------------------------------
@@ -967,6 +967,12 @@ Default:
 \fIprealloc\fR
 \fIprealloc\fR
 .RE
 .RE
 .PP
 .PP
+\fB\-\-human\-readable\fR[=\fItrue\fR|\fIfalse\fR]
+.RS 4
+Print sizes and speed in human readable format (e\&.g\&., 1\&.2Ki, 3\&.4Mi) in the console readout\&. Default:
+\fItrue\fR
+.RE
+.PP
 \fB\-\-interface\fR=INTERFACE
 \fB\-\-interface\fR=INTERFACE
 .RS 4
 .RS 4
 Bind sockets to given interface\&. You can specify interface name, IP address and hostname\&. Possible Values: interface, IP address, hostname
 Bind sockets to given interface\&. You can specify interface name, IP address and hostname\&. Possible Values: interface, IP address, hostname

+ 10 - 1
doc/aria2c.1.html

@@ -1773,6 +1773,15 @@ writes the piece to the appropriate files.</td>
 </p>
 </p>
 </dd>
 </dd>
 <dt class="hdlist1">
 <dt class="hdlist1">
+<strong>--human-readable</strong>[=<em>true</em>|<em>false</em>]
+</dt>
+<dd>
+<p>
+  Print sizes and speed in human readable format (e.g., 1.2Ki, 3.4Mi)
+  in the console readout. Default: <em>true</em>
+</p>
+</dd>
+<dt class="hdlist1">
 <strong>--interface</strong>=INTERFACE
 <strong>--interface</strong>=INTERFACE
 </dt>
 </dt>
 <dd>
 <dd>
@@ -3578,7 +3587,7 @@ files in the program, then also delete it here.</p></div>
 <div id="footnotes"><hr /></div>
 <div id="footnotes"><hr /></div>
 <div id="footer">
 <div id="footer">
 <div id="footer-text">
 <div id="footer-text">
-Last updated 2010-01-15 17:58:00 JST
+Last updated 2010-01-17 16:21:53 JST
 </div>
 </div>
 </div>
 </div>
 </body>
 </body>

+ 5 - 0
doc/aria2c.1.txt

@@ -673,6 +673,11 @@ Advanced Options
   Possible Values: 'none', 'prealloc', 'falloc'
   Possible Values: 'none', 'prealloc', 'falloc'
   Default: 'prealloc'
   Default: 'prealloc'
 
 
+*--human-readable*[='true'|'false']::
+
+  Print sizes and speed in human readable format (e.g., 1.2Ki, 3.4Mi)
+  in the console readout. Default: 'true'
+
 *--interface*=INTERFACE::
 *--interface*=INTERFACE::
 
 
   Bind sockets to given interface. You can specify interface name, IP
   Bind sockets to given interface. You can specify interface name, IP

+ 34 - 20
src/ConsoleStatCalc.cc

@@ -72,7 +72,8 @@
 namespace aria2 {
 namespace aria2 {
 
 
 static void printProgress
 static void printProgress
-(std::ostream& o, const SharedHandle<RequestGroup>& rg, const DownloadEngine* e)
+(std::ostream& o, const SharedHandle<RequestGroup>& rg, const DownloadEngine* e,
+ const SizeFormatter& sizeFormatter)
 {
 {
   TransferStat stat = rg->calculateStat();
   TransferStat stat = rg->calculateStat();
   unsigned int eta = 0;
   unsigned int eta = 0;
@@ -100,10 +101,10 @@ static void printProgress
 #endif // ENABLE_BITTORRENT
 #endif // ENABLE_BITTORRENT
     {
     {
       o << "SIZE:"
       o << "SIZE:"
-        << util::abbrevSize(rg->getCompletedLength())
+        << sizeFormatter(rg->getCompletedLength())
         << "B"
         << "B"
         << "/"
         << "/"
-        << util::abbrevSize(rg->getTotalLength())
+        << sizeFormatter(rg->getTotalLength())
         << "B";
         << "B";
       if(rg->getTotalLength() > 0) {
       if(rg->getTotalLength() > 0) {
         o << "("
         o << "("
@@ -128,13 +129,13 @@ static void printProgress
   if(!rg->downloadFinished()) {
   if(!rg->downloadFinished()) {
     o << " "
     o << " "
       << "SPD:"
       << "SPD:"
-      << util::abbrevSize(stat.getDownloadSpeed()) << "Bs";
+      << sizeFormatter(stat.getDownloadSpeed()) << "Bs";
   }
   }
   if(stat.getSessionUploadLength() > 0) {
   if(stat.getSessionUploadLength() > 0) {
     o << " "
     o << " "
       << "UP:"
       << "UP:"
-      << util::abbrevSize(stat.getUploadSpeed()) << "Bs"
-      << "(" << util::abbrevSize(stat.getAllTimeUploadLength()) << "B)";
+      << sizeFormatter(stat.getUploadSpeed()) << "Bs"
+      << "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)";
   }
   }
   if(eta > 0) {
   if(eta > 0) {
     o << " "
     o << " "
@@ -149,13 +150,17 @@ class PrintSummary
 private:
 private:
   size_t _cols;
   size_t _cols;
   const DownloadEngine* _e;
   const DownloadEngine* _e;
+  const SizeFormatter& _sizeFormatter;
 public:
 public:
-  PrintSummary(size_t cols, const DownloadEngine* e):_cols(cols), _e(e) {}
+  PrintSummary
+  (size_t cols, const DownloadEngine* e,
+   const SizeFormatter& sizeFormatter):
+    _cols(cols), _e(e), _sizeFormatter(sizeFormatter) {}
 
 
   void operator()(const SharedHandle<RequestGroup>& rg)
   void operator()(const SharedHandle<RequestGroup>& rg)
   {
   {
     const char SEP_CHAR = '-';
     const char SEP_CHAR = '-';
-    printProgress(std::cout, rg, _e);
+    printProgress(std::cout, rg, _e, _sizeFormatter);
     const std::vector<SharedHandle<FileEntry> >& fileEntries =
     const std::vector<SharedHandle<FileEntry> >& fileEntries =
       rg->getDownloadContext()->getFileEntries();
       rg->getDownloadContext()->getFileEntries();
     std::cout << "\n"
     std::cout << "\n"
@@ -169,7 +174,8 @@ public:
 
 
 static void printProgressSummary
 static void printProgressSummary
 (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
 (const std::deque<SharedHandle<RequestGroup> >& groups, size_t cols,
- const DownloadEngine* e)
+ const DownloadEngine* e,
+ const SizeFormatter& sizeFormatter)
 {
 {
   const char SEP_CHAR = '=';
   const char SEP_CHAR = '=';
   time_t now;
   time_t now;
@@ -190,12 +196,19 @@ static void printProgressSummary
   }
   }
   std::cout << " *** " << "\n"
   std::cout << " *** " << "\n"
             << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
             << std::setfill(SEP_CHAR) << std::setw(cols) << SEP_CHAR << "\n";
-  std::for_each(groups.begin(), groups.end(), PrintSummary(cols, e));
+  std::for_each(groups.begin(), groups.end(),
+                PrintSummary(cols, e, sizeFormatter));
 }
 }
 
 
-ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval):
+ConsoleStatCalc::ConsoleStatCalc(time_t summaryInterval, bool humanReadable):
   _summaryInterval(summaryInterval)
   _summaryInterval(summaryInterval)
-{}
+{
+  if(humanReadable) {
+    _sizeFormatter.reset(new AbbrevSizeFormatter());
+  } else {
+    _sizeFormatter.reset(new PlainSizeFormatter());
+  }
+}
 
 
 void
 void
 ConsoleStatCalc::calculateStat(const DownloadEngine* e)
 ConsoleStatCalc::calculateStat(const DownloadEngine* e)
@@ -204,7 +217,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
     return;
     return;
   }
   }
   _cp.reset();
   _cp.reset();
-
+  const SizeFormatter& sizeFormatter = *_sizeFormatter.get();
   bool isTTY = isatty(STDOUT_FILENO) == 1;
   bool isTTY = isatty(STDOUT_FILENO) == 1;
   unsigned short int cols = 80;
   unsigned short int cols = 80;
 #ifdef __MINGW32__
 #ifdef __MINGW32__
@@ -231,13 +244,14 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
     if((_summaryInterval > 0) &&
     if((_summaryInterval > 0) &&
        _lastSummaryNotified.elapsed(_summaryInterval)) {
        _lastSummaryNotified.elapsed(_summaryInterval)) {
       _lastSummaryNotified.reset();
       _lastSummaryNotified.reset();
-      printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e);
+      printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e,
+                           sizeFormatter);
       std::cout << "\n";
       std::cout << "\n";
     }
     }
 
 
     RequestGroupHandle firstRequestGroup = e->_requestGroupMan->getRequestGroup(0);
     RequestGroupHandle firstRequestGroup = e->_requestGroupMan->getRequestGroup(0);
 
 
-    printProgress(o, firstRequestGroup, e);
+    printProgress(o, firstRequestGroup, e, sizeFormatter);
 
 
     if(e->_requestGroupMan->countRequestGroup() > 1) {
     if(e->_requestGroupMan->countRequestGroup() > 1) {
       o << "("
       o << "("
@@ -251,7 +265,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
     TransferStat stat = e->_requestGroupMan->calculateStat();
     TransferStat stat = e->_requestGroupMan->calculateStat();
     o << " "
     o << " "
       << "[TOTAL SPD:"
       << "[TOTAL SPD:"
-      << util::abbrevSize(stat.getDownloadSpeed()) << "Bs" << "]";
+      << sizeFormatter(stat.getDownloadSpeed()) << "Bs" << "]";
   }
   }
 
 
   {
   {
@@ -260,10 +274,10 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
       o << " "
       o << " "
         << "[FileAlloc:"
         << "[FileAlloc:"
         << "#" << entry->getRequestGroup()->getGID() << " "
         << "#" << entry->getRequestGroup()->getGID() << " "
-        << util::abbrevSize(entry->getCurrentLength())
+        << sizeFormatter(entry->getCurrentLength())
         << "B"
         << "B"
         << "/"
         << "/"
-        << util::abbrevSize(entry->getTotalLength())
+        << sizeFormatter(entry->getTotalLength())
         << "B"
         << "B"
         << "(";
         << "(";
       if(entry->getTotalLength() > 0) {
       if(entry->getTotalLength() > 0) {
@@ -287,10 +301,10 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
       o << " "
       o << " "
         << "[Checksum:"
         << "[Checksum:"
         << "#" << entry->getRequestGroup()->getGID() << " "
         << "#" << entry->getRequestGroup()->getGID() << " "
-        << util::abbrevSize(entry->getCurrentLength())
+        << sizeFormatter(entry->getCurrentLength())
         << "B"
         << "B"
         << "/"
         << "/"
-        << util::abbrevSize(entry->getTotalLength())
+        << sizeFormatter(entry->getTotalLength())
         << "B"
         << "B"
         << "("
         << "("
         << 100*entry->getCurrentLength()/entry->getTotalLength()
         << 100*entry->getCurrentLength()/entry->getTotalLength()

+ 32 - 1
src/ConsoleStatCalc.h

@@ -37,9 +37,38 @@
 
 
 #include "StatCalc.h"
 #include "StatCalc.h"
 #include "TimeA2.h"
 #include "TimeA2.h"
+#include "util.h"
 
 
 namespace aria2 {
 namespace aria2 {
 
 
+class SizeFormatter:public std::unary_function<int64_t, std::string> {
+protected:
+  virtual std::string format(int64_t size) const = 0;
+public:
+  virtual ~SizeFormatter() {}
+
+  std::string operator()(int64_t size) const
+  {
+    return format(size);
+  }
+};
+
+class AbbrevSizeFormatter:public SizeFormatter {
+protected:
+  virtual std::string format(int64_t size) const
+  {
+    return util::abbrevSize(size);
+  }
+};
+
+class PlainSizeFormatter:public SizeFormatter {
+protected:
+  virtual std::string format(int64_t size) const
+  {
+    return util::itos(size);
+  }
+};
+
 class ConsoleStatCalc:public StatCalc
 class ConsoleStatCalc:public StatCalc
 {
 {
 private:
 private:
@@ -48,8 +77,10 @@ private:
   Time _lastSummaryNotified;
   Time _lastSummaryNotified;
 
 
   time_t _summaryInterval;
   time_t _summaryInterval;
+
+  SharedHandle<SizeFormatter> _sizeFormatter;
 public:
 public:
-  ConsoleStatCalc(time_t summaryInterval);
+  ConsoleStatCalc(time_t summaryInterval, bool humanReadable = true);
 
 
   virtual ~ConsoleStatCalc() {}
   virtual ~ConsoleStatCalc() {}
 
 

+ 9 - 0
src/OptionHandlerFactory.cc

@@ -238,6 +238,15 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
     op->addTag(TAG_BASIC);
     op->addTag(TAG_BASIC);
     handlers.push_back(op);
     handlers.push_back(op);
   }
   }
+  {
+    SharedHandle<OptionHandler> op(new BooleanOptionHandler
+                                   (PREF_HUMAN_READABLE,
+                                    TEXT_HUMAN_READABLE,
+                                    V_TRUE,
+                                    OptionHandler::OPT_ARG));
+    op->addTag(TAG_ADVANCED);
+    handlers.push_back(op);
+  }
   {
   {
     SharedHandle<OptionHandler> op(new DefaultOptionHandler
     SharedHandle<OptionHandler> op(new DefaultOptionHandler
                                    (PREF_INPUT_FILE,
                                    (PREF_INPUT_FILE,

+ 2 - 1
src/main.cc

@@ -94,7 +94,8 @@ SharedHandle<StatCalc> getStatCalc(const SharedHandle<Option>& op)
   if(op->getAsBool(PREF_QUIET)) {
   if(op->getAsBool(PREF_QUIET)) {
     statCalc.reset(new NullStatCalc());
     statCalc.reset(new NullStatCalc());
   } else {
   } else {
-    statCalc.reset(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL)));
+    statCalc.reset(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL),
+                                       op->getAsBool(PREF_HUMAN_READABLE)));
   }
   }
   return statCalc;
   return statCalc;
 }
 }

+ 2 - 0
src/prefs.cc

@@ -176,6 +176,8 @@ const std::string PREF_XML_RPC_LISTEN_ALL("xml-rpc-listen-all");
 const std::string PREF_INTERFACE("interface");
 const std::string PREF_INTERFACE("interface");
 // value: true | false
 // value: true | false
 const std::string PREF_DISABLE_IPV6("disable-ipv6");
 const std::string PREF_DISABLE_IPV6("disable-ipv6");
+// value: true | false
+const std::string PREF_HUMAN_READABLE("human-readable");
 
 
 /**
 /**
  * FTP related preferences
  * FTP related preferences

+ 2 - 0
src/prefs.h

@@ -180,6 +180,8 @@ extern const std::string PREF_XML_RPC_LISTEN_ALL;
 extern const std::string PREF_INTERFACE;
 extern const std::string PREF_INTERFACE;
 // value: true | false
 // value: true | false
 extern const std::string PREF_DISABLE_IPV6;
 extern const std::string PREF_DISABLE_IPV6;
+// value: true | false
+extern const std::string PREF_HUMAN_READABLE;
 
 
 /**
 /**
  * FTP related preferences
  * FTP related preferences

+ 3 - 0
src/usage_text.h

@@ -606,3 +606,6 @@
     "                              in metadata will not be downloaded. This option\n" \
     "                              in metadata will not be downloaded. This option\n" \
     "                              has effect only when BitTorrent Magnet URI is\n" \
     "                              has effect only when BitTorrent Magnet URI is\n" \
     "                              used. See also --bt-save-metadata option.")
     "                              used. See also --bt-save-metadata option.")
+#define TEXT_HUMAN_READABLE                     \
+  _(" --human-readable[=true|false] Print sizes and speed in human readable format\n" \
+    "                              (e.g., 1.2Ki, 3.4Mi) in the console readout.")