filebrowse.lua add sort by type, add .bmp, .mod to known filetypes

also extends date and size examples to show the size and timestamp
respectively

Change-Id: Ife2b74a8a60a6da097f1f714ebaf68a60d504287
This commit is contained in:
William Wilgus 2026-08-02 23:27:58 -04:00
parent f44bf5c66d
commit 49600dd77c
4 changed files with 98 additions and 20 deletions

View file

@ -29,9 +29,10 @@ local tmploader = require("temploader")
--------------------------------------------------------------------------------
-- uses print_table and get_files to display simple file browser
-- sort_by "date" "name" "size"
-- sort_by "date" "name" "size" "type"
-- descending true/false
function file_choose(dir, title, sort_by, descending)
-- returns selected file and attribs string if get_attr == true
function file_choose(dir, title, sort_by, descending, get_attr)
local dstr, hstr = ""
if not title then
dstr = "%d items found in %0d.%02d seconds"
@ -58,6 +59,7 @@ function file_choose(dir, title, sort_by, descending)
local timer
local files = {}
local dirs = {}
local attrs = {}
local item = 1
local cancel_fn = nil
@ -82,20 +84,37 @@ function file_choose(dir, title, sort_by, descending)
rb.splash(1, "Searching for Files")
dirs, files = get_files(dir, recurse, f_finddir, f_findfile, sort_by, cancel_fn, dirs, files)
local ndirs = #dirs
local parentdir = dirs[1]
for i = 1, #dirs do
for i = 1, ndirs do
dirs[i] = "\t" .. dirs[i]
end
if not descending then
for i = 1, #files do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
if not get_attr then
if not descending then
for i = 1, #files do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
end
else
for i = #files, 1, -1 do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
end
end
else
for i = #files, 1, -1 do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
else -- get_attr == true
if not descending then
for i = 1, #files do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
table.insert(attrs, string.match(files[i], ";(.+)"))
end
else
for i = #files, 1, -1 do
-- only store file name .. strip attributes from end and parent from beginning
table.insert(dirs, "\t" .. string.match(string.match(files[i], "[^/\\]+$") or "?", "[^;]+") or "?")
table.insert(attrs, string.match(files[i], ";(.+)"))
end
end
end
for i=1, #files do files[i] = nil end -- empty table for reuse
@ -113,9 +132,9 @@ function file_choose(dir, title, sort_by, descending)
dir = string.gsub(dirs[item], "%c+","")
if not rb.dir_exists("/" .. dir) then
if (parentdir == "/") then
return parentdir .. dir
return parentdir .. dir, attrs[item - ndirs - 1] -- -1 for hstr
else
return parentdir .. "/" .. dir
return parentdir .. "/" .. dir, attrs[item - ndirs - 1]
end
end
end
@ -125,7 +144,7 @@ function file_choose(dir, title, sort_by, descending)
if dir == "" then dir = "/" end
end
for i=1, #dirs do dirs[i] = nil end -- empty table for reuse
for i=1, #attrs do attrs[i] = nil end -- empty table for reuse
end
end -- file_choose
--------------------------------------------------------------------------------

View file

@ -27,7 +27,7 @@ if ... == nil then rb.splash(rb.HZ * 3, "use 'require'") end
-- findfile & finddir are definable search functions
-- if not defined all files/dirs are returned if false is passed.. none
or you can provide your own function see below..
-- sort_by can be by "name" "size" "date" or "none" to perform no sorting
-- sort_by can be by "name" "size" "date" "type" or "none" to perform no sorting
note: for "size" and "date" you may need to strip the attribute data to use
the returned filename e.g. string.match(file, "[^;]+")
-- cancel_fn if not defined or not a function no user cancel otherwise supply
@ -137,6 +137,23 @@ local function get_files(path, recurse, finddir, findfile, sort_by, cancel_fn, f
end
return s1 < s2
end
elseif sort_by == "type" then
filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
return string.format("%s%s%s; At:%d, Sz:%d, Tm:%d", path, sep, fname, fattrib, fsize, ftime)
end
sort_by_function = function(s1, s2)
local v1, v2
v1 = string.match(s1, ".+%.([^%s]+).AT:")
v2 = string.match(s2, ".+%.([^%s]+).AT:")
if v1 or v2 then
--rb.splash(20, (v1 or "?") .. " " .. (v2 or "?"))
if (v1 ~= v2) then
return (v1 or "") < (v2 or "")
end
end
return s1 < s2
end
else -- "none"
filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
return string.format("%s%s%s", path, sep, fname)

View file

@ -34,6 +34,7 @@ package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory
require("printmenus") --menu
require("filebrowse") -- file browser
require("file_attrs")
rb.actions = nil
package.loaded["actions"] = nil
@ -45,22 +46,61 @@ function main_menu()
[2] = "Sort by Name",
[3] = "Sort by Size",
[4] = "Sort by Date",
[5] = "Exit"
[5] = "Sort by Type",
[6] = "Exit"
}
local ft = {
[0] = exit_now, --if user cancels do this function
[1] = function(TITLE) return true end, -- shouldn't happen title occupies this slot
[2] = function(SBNAME)
_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "name", false) or "None")
_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "name") or "None")
end,
[3] = function(SBSIZE)
_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "size", true) or "None")
local file, attr = file_choose("/", "", "size", true, true)
if file and attr then
local sz = tonumber(string.match(attr, ".+Sz:(%d+)") or 0)
if sz > 1024 then
_lcd:splashf(rb.HZ, "%s", file .. " " .. sz / 1024 .. " kiB")
else
_lcd:splashf(rb.HZ, "%s", file .. " " .. sz .. " B")
end
else
_lcd:splashf(rb.HZ, "%s", file or "None")
end
--_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "size") or "None")
end,
[4] = function(SBDATE)
_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "date") or "None")
local file, attr = file_choose("/", "", "date", true, true)
if file and attr then
local tm = tonumber(string.match(attr, ".+Tm:(%d+)") or 0)
_lcd:splashf(rb.HZ, "%s", file .. " " .. attr)--" Tm:" .. tm)
else
_lcd:splashf(rb.HZ, "%s", file or "None")
end
--_lcd:splashf(rb.HZ, "%s", file_choose("/", "", "date") or "None")
end,
[5] = function(EXIT_) return true end
[5] = function(SBTYPE)
local file = file_choose("/", "", "type")
if file then
local known = false
local attr = rb.filetype_get_attr(file)
for k, v in pairs(rb) do
if nil ~= string.find (k, "^FILE_ATTR_(.+)") then
if v == attr then
file = file .. " " .. k
known = true
break
end
end
end
if not known then
file = file .. " Unknown Type " .. string.match(file, "%.[^%.]+$") or ""
end
end
_lcd:splashf(rb.HZ, "%s", file or "None")
end,
[6] = function(EXIT_) return true end
}
print_menu(mt, ft)