diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-04-03 22:32:46 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-04-03 22:32:46 +0000 |
commit | 1aee1d8842576aa2a2cf370f5f5466aa58f17a55 (patch) | |
tree | e6db0f1e9a8b956ac66568c86aa5a2cc0b23fc4f /lib | |
parent | 947d4f202bc72ceff341ae4401ecc9ceb06759c7 (diff) |
Make usage output (from bbstoreaccounts info and bbackupquery usage)
consistent and easier to read, with blocks, B/kB/MB/GB, % and an ASCII
bar graph.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/common/Utils.cpp | 59 | ||||
-rw-r--r-- | lib/common/Utils.h | 3 |
2 files changed, 62 insertions, 0 deletions
diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index 66fab21f..90421299 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -159,6 +159,65 @@ int ObjectExists(const std::string& rFilename) return ((st.st_mode & S_IFDIR) == 0)?ObjectExists_File:ObjectExists_Dir; } +std::string HumanReadableSize(int64_t Bytes) +{ + double readableValue = Bytes; + std::string units = " B"; + if (readableValue > 1024) + { + readableValue /= 1024; + units = "kB"; + } + + if (readableValue > 1024) + { + readableValue /= 1024; + units = "MB"; + } + + if (readableValue > 1024) + { + readableValue /= 1024; + units = "GB"; + } + + std::ostringstream result; + result << std::fixed << std::setprecision(2) << readableValue << + " " << units; + return result.str(); +} +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) +{ + 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++) + { + 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 << "|"; + + return result.str(); +} +std::string FormatUsageLineStart(const std::string& rName) +{ + std::ostringstream result; + result << std::setw(20) << std::right << rName << ": "; + return result.str(); +} diff --git a/lib/common/Utils.h b/lib/common/Utils.h index d0842b51..78bcbd6b 100644 --- a/lib/common/Utils.h +++ b/lib/common/Utils.h @@ -30,6 +30,9 @@ enum ObjectExists_Dir = 2 }; 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); #include "MemLeakFindOff.h" |