From c15915a4eecea133acb3c4c7c19fe70ae092be5d Mon Sep 17 00:00:00 2001 From: poire-z Date: Fri, 20 Oct 2017 17:48:32 +0200 Subject: [PATCH] Added util.getFriendlySize() (#3381) * Added util.getFriendlySize() * Allow for GB --- .../apps/filemanager/filemanagerbookinfo.lua | 9 +-------- frontend/ui/widget/filechooser.lua | 10 ++-------- frontend/util.lua | 17 +++++++++++++++++ .../coverbrowser.koplugin/bookinfomanager.lua | 10 +--------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/frontend/apps/filemanager/filemanagerbookinfo.lua b/frontend/apps/filemanager/filemanagerbookinfo.lua index e60829447..2e56f8685 100644 --- a/frontend/apps/filemanager/filemanagerbookinfo.lua +++ b/frontend/apps/filemanager/filemanagerbookinfo.lua @@ -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 }) diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index 1857f0267..b397ae8b6 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -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, diff --git a/frontend/util.lua b/frontend/util.lua index a36e744c8..9ea133e72 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -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 diff --git a/plugins/coverbrowser.koplugin/bookinfomanager.lua b/plugins/coverbrowser.koplugin/bookinfomanager.lua index a4f929099..11faa81c8 100644 --- a/plugins/coverbrowser.koplugin/bookinfomanager.lua +++ b/plugins/coverbrowser.koplugin/bookinfomanager.lua @@ -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()