From f2164d68cbe551f692b142392cde88c97c01c9a1 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Wed, 30 Dec 2015 13:32:06 +0000 Subject: Add format.formatfilesize and formattime to create consistent user-friendly displays of file size and time --- format.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/format.lua b/format.lua index ec3e48d..b4250f8 100644 --- a/format.lua +++ b/format.lua @@ -23,6 +23,27 @@ function mymodule.escapespecialcharacters ( str ) return (string.gsub(str or "", "[~`#%$&%*%(%)\\|%[%]{};\'\"<>/\n\r]", "\\%1")) end +function mymodule.formatfilesize ( size ) + size = tonumber(size) or 0 + if ( size > 1073741824 ) then + return ((size/1073741824) - (size/1073741824%0.1)) .. "G" + elseif ( size > 1048576 ) then + return ((size/1048576) - (size/1048576%0.1)) .. "M" + elseif ( size > 1024 ) then + return ((size/1024) - (size/1024%0.1)) .. "k" + else + return tostring(size) + end +end + +function mymodule.formattime ( time ) + if tonumber(time) then + return os.date("%c", time) + else + return "---" + end +end + -- search and remove all blank and commented lines from a string or table of lines -- returns a table to iterate over without the blank or commented lines -- cgit v1.2.3