From c0308e1ea4c6be54ff9c07c33613140e8a3ea4ec Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Jan 2010 07:23:53 +0000 Subject: [PATCH] 2010-01-17 Tatsuhiro Tsujikawa 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 --- ChangeLog | 18 +++++++++++++ doc/aria2c.1 | 10 +++++-- doc/aria2c.1.html | 11 +++++++- doc/aria2c.1.txt | 5 ++++ src/ConsoleStatCalc.cc | 54 +++++++++++++++++++++++-------------- src/ConsoleStatCalc.h | 33 ++++++++++++++++++++++- src/OptionHandlerFactory.cc | 9 +++++++ src/main.cc | 3 ++- src/prefs.cc | 2 ++ src/prefs.h | 2 ++ src/usage_text.h | 3 +++ 11 files changed, 125 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 87f10435..11477d50 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2010-01-17 Tatsuhiro Tsujikawa + + 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 Now --all-proxy, --http-proxy, --https-proxy and --ftp-proxy diff --git a/doc/aria2c.1 b/doc/aria2c.1 index ba39d9b2..41054f20 100644 --- a/doc/aria2c.1 +++ b/doc/aria2c.1 @@ -2,12 +2,12 @@ .\" Title: aria2c .\" Author: [FIXME: author] [see http://docbook.sf.net/el/author] .\" Generator: DocBook XSL Stylesheets v1.75.2 -.\" Date: 01/15/2010 +.\" Date: 01/17/2010 .\" Manual: Aria2 Manual .\" Source: Aria2 .\" Language: English .\" -.TH "ARIA2C" "1" "01/15/2010" "Aria2" "Aria2 Manual" +.TH "ARIA2C" "1" "01/17/2010" "Aria2" "Aria2 Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- @@ -967,6 +967,12 @@ Default: \fIprealloc\fR .RE .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 .RS 4 Bind sockets to given interface\&. You can specify interface name, IP address and hostname\&. Possible Values: interface, IP address, hostname diff --git a/doc/aria2c.1.html b/doc/aria2c.1.html index 11d530b5..3785fd4f 100644 --- a/doc/aria2c.1.html +++ b/doc/aria2c.1.html @@ -1773,6 +1773,15 @@ writes the piece to the appropriate files.

+--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
@@ -3578,7 +3587,7 @@ files in the program, then also delete it here.


diff --git a/doc/aria2c.1.txt b/doc/aria2c.1.txt index cd1e96d8..8c992b87 100644 --- a/doc/aria2c.1.txt +++ b/doc/aria2c.1.txt @@ -673,6 +673,11 @@ Advanced Options Possible Values: 'none', 'prealloc', 'falloc' 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:: Bind sockets to given interface. You can specify interface name, IP diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index b46a21f1..99d9d9ab 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -72,7 +72,8 @@ namespace aria2 { static void printProgress -(std::ostream& o, const SharedHandle& rg, const DownloadEngine* e) +(std::ostream& o, const SharedHandle& rg, const DownloadEngine* e, + const SizeFormatter& sizeFormatter) { TransferStat stat = rg->calculateStat(); unsigned int eta = 0; @@ -100,10 +101,10 @@ static void printProgress #endif // ENABLE_BITTORRENT { o << "SIZE:" - << util::abbrevSize(rg->getCompletedLength()) + << sizeFormatter(rg->getCompletedLength()) << "B" << "/" - << util::abbrevSize(rg->getTotalLength()) + << sizeFormatter(rg->getTotalLength()) << "B"; if(rg->getTotalLength() > 0) { o << "(" @@ -128,13 +129,13 @@ static void printProgress if(!rg->downloadFinished()) { o << " " << "SPD:" - << util::abbrevSize(stat.getDownloadSpeed()) << "Bs"; + << sizeFormatter(stat.getDownloadSpeed()) << "Bs"; } if(stat.getSessionUploadLength() > 0) { o << " " << "UP:" - << util::abbrevSize(stat.getUploadSpeed()) << "Bs" - << "(" << util::abbrevSize(stat.getAllTimeUploadLength()) << "B)"; + << sizeFormatter(stat.getUploadSpeed()) << "Bs" + << "(" << sizeFormatter(stat.getAllTimeUploadLength()) << "B)"; } if(eta > 0) { o << " " @@ -149,13 +150,17 @@ class PrintSummary private: size_t _cols; const DownloadEngine* _e; + const SizeFormatter& _sizeFormatter; 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& rg) { const char SEP_CHAR = '-'; - printProgress(std::cout, rg, _e); + printProgress(std::cout, rg, _e, _sizeFormatter); const std::vector >& fileEntries = rg->getDownloadContext()->getFileEntries(); std::cout << "\n" @@ -169,7 +174,8 @@ public: static void printProgressSummary (const std::deque >& groups, size_t cols, - const DownloadEngine* e) + const DownloadEngine* e, + const SizeFormatter& sizeFormatter) { const char SEP_CHAR = '='; time_t now; @@ -190,12 +196,19 @@ static void printProgressSummary } std::cout << " *** " << "\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) -{} +{ + if(humanReadable) { + _sizeFormatter.reset(new AbbrevSizeFormatter()); + } else { + _sizeFormatter.reset(new PlainSizeFormatter()); + } +} void ConsoleStatCalc::calculateStat(const DownloadEngine* e) @@ -204,7 +217,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) return; } _cp.reset(); - + const SizeFormatter& sizeFormatter = *_sizeFormatter.get(); bool isTTY = isatty(STDOUT_FILENO) == 1; unsigned short int cols = 80; #ifdef __MINGW32__ @@ -231,13 +244,14 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) if((_summaryInterval > 0) && _lastSummaryNotified.elapsed(_summaryInterval)) { _lastSummaryNotified.reset(); - printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e); + printProgressSummary(e->_requestGroupMan->getRequestGroups(), cols, e, + sizeFormatter); std::cout << "\n"; } RequestGroupHandle firstRequestGroup = e->_requestGroupMan->getRequestGroup(0); - printProgress(o, firstRequestGroup, e); + printProgress(o, firstRequestGroup, e, sizeFormatter); if(e->_requestGroupMan->countRequestGroup() > 1) { o << "(" @@ -251,7 +265,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) TransferStat stat = e->_requestGroupMan->calculateStat(); o << " " << "[TOTAL SPD:" - << util::abbrevSize(stat.getDownloadSpeed()) << "Bs" << "]"; + << sizeFormatter(stat.getDownloadSpeed()) << "Bs" << "]"; } { @@ -260,10 +274,10 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) o << " " << "[FileAlloc:" << "#" << entry->getRequestGroup()->getGID() << " " - << util::abbrevSize(entry->getCurrentLength()) + << sizeFormatter(entry->getCurrentLength()) << "B" << "/" - << util::abbrevSize(entry->getTotalLength()) + << sizeFormatter(entry->getTotalLength()) << "B" << "("; if(entry->getTotalLength() > 0) { @@ -287,10 +301,10 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) o << " " << "[Checksum:" << "#" << entry->getRequestGroup()->getGID() << " " - << util::abbrevSize(entry->getCurrentLength()) + << sizeFormatter(entry->getCurrentLength()) << "B" << "/" - << util::abbrevSize(entry->getTotalLength()) + << sizeFormatter(entry->getTotalLength()) << "B" << "(" << 100*entry->getCurrentLength()/entry->getTotalLength() diff --git a/src/ConsoleStatCalc.h b/src/ConsoleStatCalc.h index fe4134a3..0501c3c0 100644 --- a/src/ConsoleStatCalc.h +++ b/src/ConsoleStatCalc.h @@ -37,9 +37,38 @@ #include "StatCalc.h" #include "TimeA2.h" +#include "util.h" namespace aria2 { +class SizeFormatter:public std::unary_function { +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 { private: @@ -48,8 +77,10 @@ private: Time _lastSummaryNotified; time_t _summaryInterval; + + SharedHandle _sizeFormatter; public: - ConsoleStatCalc(time_t summaryInterval); + ConsoleStatCalc(time_t summaryInterval, bool humanReadable = true); virtual ~ConsoleStatCalc() {} diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index 1ef10d0f..520ff42c 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -238,6 +238,15 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() op->addTag(TAG_BASIC); handlers.push_back(op); } + { + SharedHandle op(new BooleanOptionHandler + (PREF_HUMAN_READABLE, + TEXT_HUMAN_READABLE, + V_TRUE, + OptionHandler::OPT_ARG)); + op->addTag(TAG_ADVANCED); + handlers.push_back(op); + } { SharedHandle op(new DefaultOptionHandler (PREF_INPUT_FILE, diff --git a/src/main.cc b/src/main.cc index c657ff43..a968dce1 100644 --- a/src/main.cc +++ b/src/main.cc @@ -94,7 +94,8 @@ SharedHandle getStatCalc(const SharedHandle