diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index d780d9bd..47dfa4dc 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -95,6 +95,69 @@ protected: }; } // namespace +namespace { +void printSeedRatio(std::ostream& o, const SharedHandle& rg, + const TransferStat& stat) +{ + if(rg->getCompletedLength() > 0) { + std::streamsize oldprec = o.precision(); + o << std::fixed << std::setprecision(1) + << ((stat.allTimeUploadLength*10)/rg->getCompletedLength())/10.0 + << std::setprecision(oldprec) + << std::resetiosflags(std::ios::fixed); + } else { + o << "--"; + } +} +} // namespace + +namespace { +void printProgressCompact(std::ostream& o, const DownloadEngine* e, + const SizeFormatter& sizeFormatter) +{ + if(!e->getRequestGroupMan()->downloadFinished()) { + NetStat& netstat = e->getRequestGroupMan()->getNetStat(); + int dl = netstat.calculateDownloadSpeed(); + int ul = netstat.calculateUploadSpeed(); + o << "[DL:" << sizeFormatter(dl) << "B UL:" << sizeFormatter(ul) << "B]"; + } + + const std::deque >& groups = + e->getRequestGroupMan()->getRequestGroups(); + size_t cnt = 0; + const size_t MAX_ITEM = 5; + for(std::deque >::const_iterator + i = groups.begin(), eoi = groups.end(); i != eoi && cnt < MAX_ITEM; + ++i, ++cnt) { + TransferStat stat = (*i)->calculateStat(); + o << "[#" << (*i)->getGID() << " "; +#ifdef ENABLE_BITTORRENT + if((*i)->getDownloadContext()->hasAttribute(CTX_ATTR_BT) && + !bittorrent::getTorrentAttrs((*i)->getDownloadContext()) + ->metadata.empty() && (*i)->downloadFinished()) { + o << "SEED("; + printSeedRatio(o, *i, stat); + o << ")"; + } else +#endif // ENABLE_BITTORRENT + { + o << sizeFormatter((*i)->getCompletedLength()) << "B" + << "/" + << sizeFormatter((*i)->getTotalLength()) << "B"; + if((*i)->getTotalLength() > 0) { + o << "(" + << 100*(*i)->getCompletedLength()/(*i)->getTotalLength() + << "%)"; + } + } + o << "]"; + } + if(cnt < groups.size()) { + o << "(" << groups.size()-cnt << "more)"; + } +} +} // namespace + namespace { void printProgress (std::ostream& o, const SharedHandle& rg, const DownloadEngine* e, @@ -113,15 +176,7 @@ void printProgress !bittorrent::getTorrentAttrs(rg->getDownloadContext())->metadata.empty() && rg->downloadFinished()) { o << "SEEDING(ratio:"; - if(rg->getCompletedLength() > 0) { - std::streamsize oldprec = o.precision(); - o << std::fixed << std::setprecision(1) - << ((stat.allTimeUploadLength*10)/rg->getCompletedLength())/10.0 - << std::setprecision(oldprec) - << std::resetiosflags(std::ios::fixed); - } else { - o << "--"; - } + printSeedRatio(o, rg, stat); o << ")"; } else #endif // ENABLE_BITTORRENT @@ -289,7 +344,8 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) if(!readoutVisibility_) { return; } - if(e->getRequestGroupMan()->countRequestGroup() > 0) { + size_t numGroup = e->getRequestGroupMan()->countRequestGroup(); + if(numGroup == 1) { SharedHandle firstRequestGroup = e->getRequestGroupMan()->getRequestGroup(0); @@ -300,12 +356,9 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) << e->getRequestGroupMan()->countRequestGroup()-1 << "more...)"; } - } - - if(e->getRequestGroupMan()->countRequestGroup() > 1 && - !e->getRequestGroupMan()->downloadFinished()) { - int spd = e->getRequestGroupMan()->getNetStat().calculateDownloadSpeed(); - o << " [TOTAL SPD:" << sizeFormatter(spd) << "Bs]"; + } else if(numGroup > 1) { + // For more than 2 RequestGroups, use compact readout form + printProgressCompact(o, e, sizeFormatter); } { diff --git a/src/util.cc b/src/util.cc index 354d1853..0f9083b2 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1292,20 +1292,29 @@ int64_t getRealSize(const std::string& sizeWithUnit) std::string abbrevSize(int64_t size) { - if(size < 1024) { - return itos(size, true); + static const char* UNITS[] = { "", "Ki", "Mi", "Gi" }; + int64_t t = size; + size_t uidx = 0; + int r = 0; + while(t >= 1024 && uidx+1 < sizeof(UNITS)/sizeof(UNITS[0])) { + lldiv_t d = lldiv(t, 1024); + t = d.quot; + r = d.rem; + ++uidx; } - static const char units[] = { 'K', 'M' }; - size_t i = 0; - int r = size&0x3ffu; - size >>= 10; - for(; i < sizeof(units)-1 && size >= 1024; ++i) { - r = size&0x3ffu; - size >>= 10; + if(uidx+1 < sizeof(UNITS)/sizeof(UNITS[0]) && t >= 512) { + ++uidx; + r = t; + t = 0; } - std::string result = itos(size, true); - result += fmt(".%d%ci", r*10/1024, units[i]); - return result; + std::string res; + res += itos(t, true); + if(t < 10 && uidx > 0) { + res += "."; + res += itos(r*10/1024); + } + res += UNITS[uidx]; + return res; } void sleep(long seconds) { diff --git a/test/UtilTest.cc b/test/UtilTest.cc index e356bfba..80555055 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -1550,13 +1550,16 @@ void UtilTest::testGetRealSize() void UtilTest::testAbbrevSize() { - CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL)); + CPPUNIT_ASSERT_EQUAL(std::string("8,589,934,591Gi"), + util::abbrevSize(9223372036854775807LL)); + CPPUNIT_ASSERT_EQUAL(std::string("4.0Gi"), util::abbrevSize(4294967296LL)); CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024)); - CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023)); + CPPUNIT_ASSERT_EQUAL(std::string("0.9Ki"), util::abbrevSize(1023)); + CPPUNIT_ASSERT_EQUAL(std::string("0.5Ki"), util::abbrevSize(512)); + CPPUNIT_ASSERT_EQUAL(std::string("511"), util::abbrevSize(511)); CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0)); CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127)); CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864)); - } void UtilTest::testToStream() @@ -1576,10 +1579,10 @@ void UtilTest::testToStream() "idx|path/length\n" "===+===========================================================================\n" " 1|aria2.tar.bz2\n" - " |12.0KiB (12,300)\n" + " |12KiB (12,300)\n" "---+---------------------------------------------------------------------------\n" " 2|aria2.txt\n" - " |556B (556)\n" + " |0.5KiB (556)\n" "---+---------------------------------------------------------------------------\n"), readFile(filename)); }