show file size and directory items in filemanager

pull/885/head
chrox 10 years ago
parent 8294a629b8
commit 11a8a1dce6

@ -48,10 +48,6 @@ DSHOWOVERLAP = false
-- default to false -- default to false
DSHOWHIDDENFILES = false DSHOWHIDDENFILES = false
-- show file size in filemanager
-- default to false
DSHOWFILESIZE = false
-- landscape clockwise rotation -- landscape clockwise rotation
-- default to true, set to false for counterclockwise rotation -- default to true, set to false for counterclockwise rotation
DLANDSCAPE_CLOCKWISE_ROTATION = true DLANDSCAPE_CLOCKWISE_ROTATION = true

@ -28,8 +28,6 @@ local FileManager = InputContainer:extend{
} }
function FileManager:init() function FileManager:init()
local exclude_dirs = {"%.sdr$"}
self.show_parent = self.show_parent or self self.show_parent = self.show_parent or self
self.banner = VerticalGroup:new{ self.banner = VerticalGroup:new{
@ -51,12 +49,6 @@ function FileManager:init()
is_popout = false, is_popout = false,
is_borderless = true, is_borderless = true,
has_close_button = true, has_close_button = true,
dir_filter = function(dirname)
for _, pattern in ipairs(exclude_dirs) do
if dirname:match(pattern) then return end
end
return true
end,
file_filter = function(filename) file_filter = function(filename)
if DocumentRegistry:getProvider(filename) then if DocumentRegistry:getProvider(filename) then
return true return true
@ -164,6 +156,11 @@ function FileManager:onClose()
return true return true
end end
function FileManager:onRefresh()
self.file_chooser:refreshPath()
return true
end
function FileManager:getDefaultDir() function FileManager:getDefaultDir()
if Device:isKindle() then if Device:isKindle() then
return "/mnt/us/documents" return "/mnt/us/documents"

@ -5,6 +5,7 @@ local Screen = require("ui/screen")
local Device = require("ui/device") local Device = require("ui/device")
local util = require("ffi/util") local util = require("ffi/util")
local DEBUG = require("dbg") local DEBUG = require("dbg")
local _ = require("gettext")
local ffi = require("ffi") local ffi = require("ffi")
ffi.cdef[[ ffi.cdef[[
int strcoll (char *str1, char *str2); int strcoll (char *str1, char *str2);
@ -22,12 +23,19 @@ local FileChooser = Menu:extend{
path = lfs.currentdir(), path = lfs.currentdir(),
parent = nil, parent = nil,
show_hidden = nil, show_hidden = nil,
show_filesize = DSHOWFILESIZE,
filter = function(filename) return true end, filter = function(filename) return true end,
exclude_dirs = {"%.sdr$"},
collate = strcoll, collate = strcoll,
} }
function FileChooser:init() function FileChooser:init()
-- common dir filter
self.dir_filter = function(dirname)
for _, pattern in ipairs(self.exclude_dirs) do
if dirname:match(pattern) then return end
end
return true
end
-- disable string collating in Kobo devices. See issue koreader/koreader#686 -- disable string collating in Kobo devices. See issue koreader/koreader#686
if Device:isKobo() then self.collate = nil end if Device:isKobo() then self.collate = nil end
self.item_table = self:genItemTableFromPath(self.path) self.item_table = self:genItemTableFromPath(self.path)
@ -62,35 +70,50 @@ function FileChooser:genItemTableFromPath(path)
table.sort(files, self.collate) table.sort(files, self.collate)
local item_table = {} local item_table = {}
for _, dir in ipairs(dirs) do for i, dir in ipairs(dirs) do
table.insert(item_table, { text = dir.."/", path = self.path.."/"..dir }) local path = self.path.."/"..dir
local items = 0
local ok, iter, dir_obj = pcall(lfs.dir, path)
if ok then
for f in iter, dir_obj do
items = items + 1
end
-- exclude "." and ".."
items = items - 2
end
local istr = items .. (items > 1 and _(" items") or _(" item"))
table.insert(item_table, { text = dir.."/", mandatory = istr, path = path})
end end
for _, file in ipairs(files) do for _, file in ipairs(files) do
local full_path = self.path.."/"..file local full_path = self.path.."/"..file
if self.show_filesize then local file_size = lfs.attributes(full_path, "size")
local sstr = string.format("%4.1fM",lfs.attributes(full_path, "size")/1024/1024) local sstr = ""
table.insert(item_table, { text = file, mandatory = sstr, path = full_path }) 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 else
table.insert(item_table, { text = file, path = full_path }) sstr = string.format("%d B", file_size)
end end
table.insert(item_table, { text = file, mandatory = sstr, path = full_path })
end end
return item_table return item_table
end end
function FileChooser:refreshPath()
self:swithItemTable(nil, self:genItemTableFromPath(self.path))
end
function FileChooser:changeToPath(path) function FileChooser:changeToPath(path)
path = util.realpath(path) path = util.realpath(path)
self.path = path self.path = path
self:refreshPath() self:refreshPath()
end end
function FileChooser:refreshPath()
self:swithItemTable(nil, self:genItemTableFromPath(self.path))
end
function FileChooser:toggleHiddenFiles() function FileChooser:toggleHiddenFiles()
self.show_hidden = not self.show_hidden self.show_hidden = not self.show_hidden
self:swithItemTable(nil, self:genItemTableFromPath(self.path)) self:refreshPath()
end end
function FileChooser:onMenuSelect(item) function FileChooser:onMenuSelect(item)

Loading…
Cancel
Save