feat: add file size in popup (#1049)

This commit is contained in:
Andreas Bissinger
2022-03-06 11:26:35 +01:00
committed by GitHub
parent 5015e7226c
commit ceadf83809
4 changed files with 36 additions and 20 deletions

View File

@@ -245,4 +245,19 @@ function M.table_create_missing(tbl, sub)
return t
end
function M.format_bytes(bytes)
local units = {'B', 'K', 'M', 'G', 'T'}
bytes = math.max(bytes, 0)
local pow = math.floor((bytes and math.log(bytes) or 0) / math.log(1024))
pow = math.min(pow, #units)
local value = bytes / (1024 ^ pow)
value = math.floor((value * 10) + 0.5) / 10
pow = pow + 1
return (units[pow] == nil) and (bytes .. 'B') or (value .. units[pow])
end
return M