Added util.getFriendlySize() (#3381)

* Added util.getFriendlySize()

* Allow for GB
pull/3383/head
poire-z 7 years ago committed by Frans de Jonge
parent 87a86e0dc1
commit c15915a4ee

@ -52,14 +52,7 @@ function BookInfo:show(file, book_props)
local directory, filename = util.splitFilePathName(file)
local filename_without_suffix, filetype = util.splitFileNameSuffix(filename) -- luacheck: no unused
local file_size = lfs.attributes(file, "size") or 0
local size
if file_size > 1024*1024 then
size = string.format("%4.1f MB", file_size/1024/1024)
elseif file_size > 1024 then
size = string.format("%4.1f KB", file_size/1024)
else
size = string.format("%d B", file_size)
end
local size = util.getFriendlySize(file_size)
table.insert(kv_pairs, { _("Filename:"), filename })
table.insert(kv_pairs, { _("Format:"), filetype:upper() })
table.insert(kv_pairs, { _("Size:"), size })

@ -9,6 +9,7 @@ local util = require("ffi/util")
local _ = require("gettext")
local Screen = Device.screen
local getFileNameSuffix = require("util").getFileNameSuffix
local getFriendlySize = require("util").getFriendlySize
ffi.cdef[[
int strcoll (const char *str1, const char *str2);
@ -188,14 +189,7 @@ function FileChooser:genItemTableFromPath(path)
for _, file in ipairs(files) do
local full_path = self.path.."/"..file.name
local file_size = lfs.attributes(full_path, "size") or 0
local sstr
if file_size > 1024*1024 then
sstr = string.format("%4.1f MB", file_size/1024/1024)
elseif file_size > 1024 then
sstr = string.format("%4.1f KB", file_size/1024)
else
sstr = string.format("%d B", file_size)
end
local sstr = getFriendlySize(file_size)
local file_item = {
text = file.name,
mandatory = sstr,

@ -352,6 +352,23 @@ function util.getFileNameSuffix(file)
return suffix
end
--- Gets human friendly size as string
---- @int size (bytes)
---- @treturn string
function util.getFriendlySize(size)
local s
if size > 1024*1024*1024 then
s = string.format("%4.1f GB", size/1024/1024/1024)
elseif size > 1024*1024 then
s = string.format("%4.1f MB", size/1024/1024)
elseif size > 1024 then
s = string.format("%4.1f KB", size/1024)
else
s = string.format("%d B", size)
end
return s
end
--- Adds > to touch menu items with a submenu
function util.getMenuText(item)
local text

@ -133,15 +133,7 @@ end
-- DB management
function BookInfoManager:getDbSize()
local file_size = lfs.attributes(self.db_location, "size") or 0
local sstr
if file_size > 1024*1024 then
sstr = string.format("%4.1f MB", file_size/1024/1024)
elseif file_size > 1024 then
sstr = string.format("%4.1f KB", file_size/1024)
else
sstr = string.format("%d B", file_size)
end
return sstr
return require("util").getFriendlySize(file_size)
end
function BookInfoManager:createDB()

Loading…
Cancel
Save