summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2022-11-14 09:55:09 +0100
committerJoel Rosdahl <joel@rosdahl.net>2022-11-27 21:33:50 +0100
commitcf78bc995e765767b42942a834fcd7e81b392fd7 (patch)
treee5cd7c1d8a623d2b174dea145fbc1d1e7a471657 /src
parent6e2ffd10df8b71c775925b4bd54838a9181fc328 (diff)
downloadccache-cf78bc995e765767b42942a834fcd7e81b392fd7.tar.gz
enhance: Add Util::format_human_readable_diff
Diffstat (limited to 'src')
-rw-r--r--src/Util.cpp7
-rw-r--r--src/Util.hpp3
2 files changed, 10 insertions, 0 deletions
diff --git a/src/Util.cpp b/src/Util.cpp
index 3aecbaa1..844e5497 100644
--- a/src/Util.cpp
+++ b/src/Util.cpp
@@ -556,6 +556,13 @@ format_base32hex(const uint8_t* data, size_t size)
}
std::string
+format_human_readable_diff(int64_t diff)
+{
+ const char* sign = diff == 0 ? "" : (diff > 0 ? "+" : "-");
+ return FMT("{}{}", sign, format_human_readable_size(std::abs(diff)));
+}
+
+std::string
format_human_readable_size(uint64_t size)
{
if (size >= 1000 * 1000 * 1000) {
diff --git a/src/Util.hpp b/src/Util.hpp
index e29aff3d..1bd57b0d 100644
--- a/src/Util.hpp
+++ b/src/Util.hpp
@@ -143,6 +143,9 @@ std::string format_base16(const uint8_t* data, size_t size);
// padding characters will be added.
std::string format_base32hex(const uint8_t* data, size_t size);
+// Format `diff` as a human-readable string.
+std::string format_human_readable_diff(int64_t diff);
+
// Format `size` as a human-readable string.
std::string format_human_readable_size(uint64_t size);