From f488eb2bb34666cc3cdb5acd6b7eca8f60ee6455 Mon Sep 17 00:00:00 2001 From: poire-z Date: Tue, 17 Dec 2019 13:00:35 +0100 Subject: [PATCH] util.getFriendlySize(): add option to right align Left align by default, but allow right alignment by padding left with spaces. --- frontend/util.lua | 13 ++++++++----- spec/unit/util_spec.lua | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/frontend/util.lua b/frontend/util.lua index 17c874679..ce093670b 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -663,24 +663,27 @@ end --- Gets human friendly size as string ---- @int size (bytes) +---- @bool right_align (by padding with spaces on the left) ---- @treturn string -function util.getFriendlySize(size) +function util.getFriendlySize(size, right_align) + local frac_format = right_align and "%6.1f" or "%.1f" + local deci_format = right_align and "%6d" or "%d" size = tonumber(size) if not size or type(size) ~= "number" then return end if size > 1024*1024*1024 then -- @translators This is an abbreviation for the gigabyte, a unit of computer memory or data storage capacity. - return T(_("%1 GB"), string.format("%4.1f", size/1024/1024/1024)) + return T(_("%1 GB"), string.format(frac_format, size/1024/1024/1024)) end if size > 1024*1024 then -- @translators This is an abbreviation for the megabyte, a unit of computer memory or data storage capacity. - return T(_("%1 MB"), string.format("%4.1f", size/1024/1024)) + return T(_("%1 MB"), string.format(frac_format, size/1024/1024)) end if size > 1024 then -- @translators This is an abbreviation for the kilobyte, a unit of computer memory or data storage capacity. - return T(_("%1 KB"), string.format("%4.1f", size/1024)) + return T(_("%1 KB"), string.format(frac_format, size/1024)) else -- @translators This is an abbreviation for the byte, a unit of computer memory or data storage capacity. - return T(_("%1 B"), string.format("%d", size)) + return T(_("%1 B"), string.format(deci_format, size)) end end diff --git a/spec/unit/util_spec.lua b/spec/unit/util_spec.lua index c1d1ac27f..734a813a1 100644 --- a/spec/unit/util_spec.lua +++ b/spec/unit/util_spec.lua @@ -334,21 +334,41 @@ describe("util module", function() assert.is_equal("100.0 GB", util.getFriendlySize(100*1024*1024*1024)) end) - it("to 1.0 GB with minimum field width alignment", function() - assert.is_equal(" 1.0 GB", + it("to 1.0 GB", function() + assert.is_equal("1.0 GB", util.getFriendlySize(1024*1024*1024+1)) end) - it("to 1.0 MB with minimum field width alignment", function() - assert.is_equal(" 1.0 MB", + it("to 1.0 MB", function() + assert.is_equal("1.0 MB", util.getFriendlySize(1024*1024+1)) end) - it("to 1.0 KB with minimum field width alignment", function() - assert.is_equal(" 1.0 KB", + it("to 1.0 KB", function() + assert.is_equal("1.0 KB", util.getFriendlySize(1024+1)) end) it("to B", function() - assert.is_equal("100 B", - util.getFriendlySize(100)) + assert.is_equal("10 B", + util.getFriendlySize(10)) + end) + it("to 100.0 GB with minimum field width alignment", function() + assert.is_equal(" 100.0 GB", + util.getFriendlySize(100*1024*1024*1024, true)) + end) + it("to 1.0 GB with minimum field width alignment", function() + assert.is_equal(" 1.0 GB", + util.getFriendlySize(1024*1024*1024+1, true)) + end) + it("to 1.0 MB with minimum field width alignment", function() + assert.is_equal(" 1.0 MB", + util.getFriendlySize(1024*1024+1, true)) + end) + it("to 1.0 KB with minimum field width alignment", function() + assert.is_equal(" 1.0 KB", + util.getFriendlySize(1024+1, true)) + end) + it("to B with minimum field width alignment", function() + assert.is_equal(" 10 B", + util.getFriendlySize(10, true)) end) end) it("should return nil when input is nil or false", function()