diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-08-07 13:47:59 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-08-07 13:47:59 +0000 |
commit | b7cf8523cee861d02cd369f55567d88253f92eb1 (patch) | |
tree | a3735bb741f32512585fe22e2415186022efb643 /lib/common | |
parent | 2cd7050a54b6db73f375c4215075b290b675bd0c (diff) |
Add machine-readable output mode (with -m option) to bbstoreaccounts info.
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/Utils.cpp | 58 | ||||
-rw-r--r-- | lib/common/Utils.h | 6 |
2 files changed, 43 insertions, 21 deletions
diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index b2a16d5b..3f06ac48 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -252,36 +252,56 @@ std::string HumanReadableSize(int64_t Bytes) return result.str(); } -std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, + bool MachineReadable) { std::ostringstream result; - // Bar graph - char bar[17]; - unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); - if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} - for(unsigned int l = 0; l < b; l++) + + if (MachineReadable) { - bar[l] = '*'; + result << (Bytes >> 10) << " kB, " << + std::setprecision(0) << ((Bytes*100)/Max) << "%"; } - for(unsigned int l = b; l < sizeof(bar) - 1; l++) + else { - bar[l] = ' '; + // Bar graph + char bar[17]; + unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); + if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} + for(unsigned int l = 0; l < b; l++) + { + bar[l] = '*'; + } + for(unsigned int l = b; l < sizeof(bar) - 1; l++) + { + bar[l] = ' '; + } + bar[sizeof(bar)-1] = '\0'; + + result << std::fixed << + std::setw(10) << Blocks << " blocks, " << + std::setw(10) << HumanReadableSize(Bytes) << ", " << + std::setw(3) << std::setprecision(0) << + ((Bytes*100)/Max) << "% |" << bar << "|"; } - bar[sizeof(bar)-1] = '\0'; - - result << std::fixed << - std::setw(10) << Blocks << " blocks, " << - std::setw(10) << HumanReadableSize(Bytes) << ", " << - std::setw(3) << std::setprecision(0) << - ((Bytes*100)/Max) << "% |" << bar << "|"; return result.str(); } -std::string FormatUsageLineStart(const std::string& rName) +std::string FormatUsageLineStart(const std::string& rName, + bool MachineReadable) { - std::ostringstream result; - result << std::setw(20) << std::right << rName << ": "; + std::ostringstream result; + + if (MachineReadable) + { + result << rName << ": "; + } + else + { + result << std::setw(20) << std::right << rName << ": "; + } + return result.str(); } diff --git a/lib/common/Utils.h b/lib/common/Utils.h index 78bcbd6b..d6792077 100644 --- a/lib/common/Utils.h +++ b/lib/common/Utils.h @@ -31,8 +31,10 @@ enum }; int ObjectExists(const std::string& rFilename); std::string HumanReadableSize(int64_t Bytes); -std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max); -std::string FormatUsageLineStart(const std::string& rName); +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, + bool MachineReadable); +std::string FormatUsageLineStart(const std::string& rName, + bool MachineReadable); #include "MemLeakFindOff.h" |