alias humanSize = stringifiable!( (size, sink) { import std.format : formattedWrite; if (!size) // You would otherwise need to wrap everything in fmtIf: return sink("0"); static immutable prefixChars = " KMGTPEZY"; size_t power = 0; while (size > 1000 && power + 1 < prefixChars.length) size /= 1024, power++; sink.formattedWrite!"%s %sB"(size, prefixChars[power]); }, real); import std.conv : text; assert(humanSize(0).text == "0"); assert(humanSize(8192).text == "8 KB");
Constructs a functor type from a function alias, and wraps it into a stringifiable object. Can be used to create stringifiable widgets which need a sink for more complex behavior.