Disk free space reporting:

1. Enhance FileInfo:FileSize() to handle gigabytes and rename it to FileInfo:FormatSize() as it is not just for file sizes.
2. Add "Free space" field to file info because it is useful to see it when deciding whether to remove this file or not.
This commit is contained in:
Tigran Aivazian
2012-09-19 21:46:31 +01:00
parent 69ac04500a
commit 8496ccb073
2 changed files with 24 additions and 7 deletions

View File

@@ -23,10 +23,15 @@ function FileInfo:FileCreated(fname, attr)
return os.date("%d %b %Y, %H:%M:%S", lfs.attributes(fname,attr))
end
function FileInfo:FileSize(size)
if size < 1024 then return size.." Bytes"
elseif size < 2^20 then return string.format("%.2f", size/2^10).."KB ("..size.." Bytes)"
else return string.format("%.2f", size/2^20).."MB ("..size.." Bytes)"
function FileInfo:FormatSize(size)
if size < 1024 then
return size.." Bytes"
elseif size < 2^20 then
return string.format("%.2f", size/2^10).."KB ("..size.." Bytes)"
elseif size < 2^30 then
return string.format("%.2f", size/2^20).."MB ("..size.." Bytes)"
else
return string.format("%.2f", size/2^30).."GB ("..size.." Bytes)"
end
end
@@ -52,12 +57,12 @@ function FileInfo:init(path, fname)
info_entry = {dir = "Path", name = path}
table.insert(self.result, info_entry)
info_entry = {dir = "Size", name = FileInfo:FileSize(lfs.attributes(self.pathfile, "size"))}
info_entry = {dir = "Size", name = FileInfo:FormatSize(lfs.attributes(self.pathfile, "size"))}
table.insert(self.result, info_entry)
-- total size of all unzipped entries for zips
local match = string.match(fname, ".+%.([^.]+)")
if match and string.lower(match) == "zip" then
info_entry = {dir = "Unpacked", name = FileInfo:FileSize(getUnpackedZipSize(self.pathfile))}
info_entry = {dir = "Unpacked", name = FileInfo:FormatSize(getUnpackedZipSize(self.pathfile))}
table.insert(self.result, info_entry)
--[[ TODO: When the fileentry inside zips is encoded as ANSI (codes 128-255)
any attempt to print such fileentry causes crash by drawing!!! When fileentries
@@ -66,6 +71,8 @@ function FileInfo:init(path, fname)
table.insert(self.result, info_entry) ]]
end
info_entry = {dir = "Free space", name = FileInfo:FormatSize(util.df("."))}
table.insert(self.result, info_entry)
info_entry = {dir = "Created", name = FileInfo:FileCreated(self.pathfile, "change")}
table.insert(self.result, info_entry)
info_entry = {dir = "Modified", name = FileInfo:FileCreated(self.pathfile, "modification")}

12
util.c
View File

@@ -17,6 +17,7 @@
*/
#include <sys/time.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include "util.h"
@@ -41,10 +42,18 @@ static int util_usleep(lua_State *L) {
return 0;
}
static int util_df(lua_State *L) {
char *path = luaL_checkstring(L, 1);
struct statvfs vfs;
statvfs(path, &vfs);
lua_pushnumber(L, (double)vfs.f_bfree * (double)vfs.f_bsize);
return 1;
}
/* Turn UTF-8 char code to Unicode */
static int utf8charcode(lua_State *L) {
size_t len;
const char* utf8char = luaL_checklstring(L, 1, &len);
const char *utf8char = luaL_checklstring(L, 1, &len);
int c;
if(len == 1) {
c = utf8char[0] & 0x7F; /* should not be needed */
@@ -75,6 +84,7 @@ static const struct luaL_Reg util_func[] = {
{"usleep", util_usleep},
{"utf8charcode", utf8charcode},
{"isEmulated", isEmulated},
{"df", util_df},
{NULL, NULL}
};