mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
util.getFriendlySize(): add option to right align
Left align by default, but allow right alignment by padding left with spaces.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user