2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00

Added util.getFriendlySize() (#3381)

* Added util.getFriendlySize()

* Allow for GB
This commit is contained in:
poire-z 2017-10-20 17:48:32 +02:00 committed by Frans de Jonge
parent 87a86e0dc1
commit c15915a4ee
4 changed files with 21 additions and 25 deletions

View File

@ -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 })

View File

@ -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,

View File

@ -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

View File

@ -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()