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

rockbox-gerrit-noreply--- via rockbox-cvs <[email protected]>
Newsgroups gmane.comp.systems.archos.rockbox.cvs
Message-ID <[email protected]>
commit 49600dd77c9ee7195f34a139efe76a294b544e30
Author: William Wilgus <[email protected]>
Date:   Sun Aug 2 23:27:58 2026 -0400

    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

diff --git a/apps/filetypes.c b/apps/filetypes.c
index 30a1582da8..4c68fc9579 100644
--- a/apps/filetypes.c
+++ b/apps/filetypes.c
@@ -72,6 +72,8 @@ static const struct filetype_inbuilt inbuilt_filetypes[] = {
     { "m3u8", FILE_ATTR_M3U },
     { "cfg",  FILE_ATTR_CFG },
     { "wps",  FILE_ATTR_WPS },
+    { "bmp",  FILE_ATTR_BMP },
+    { "mod",  FILE_ATTR_MOD },
 #ifdef HAVE_REMOTE_LCD
     { "rwps", FILE_ATTR_RWPS },
 #endif
diff --git a/apps/plugins/lua/include_lua/filebrowse.lua b/apps/plugins/lua/include_lua/filebrowse.lua
index c41598fd63..d155074e29 100755
--- a/apps/plugins/lua/include_lua/filebrowse.lua
+++ b/apps/plugins/lua/include_lua/filebrowse.lua
@@ -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
 --------------------------------------------------------------------------------
diff --git a/apps/plugins/lua/include_lua/get_files.lua b/apps/plugins/lua/include_lua/get_files.lua
index 42c6c9c378..01a98d3ed7 100644
--- a/apps/plugins/lua/include_lua/get_files.lua
+++ b/apps/plugins/lua/include_lua/get_files.lua
@@ -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)
diff --git a/apps/plugins/lua_scripts/file_browser.lua b/apps/plugins/lua_scripts/file_browser.lua
index 8a16a1a544..0bcaed9101 100644
--- a/apps/plugins/lua_scripts/file_browser.lua
+++ b/apps/plugins/lua_scripts/file_browser.lua
@@ -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)
-- 
rockbox-cvs mailing list
[email protected]
https://lists.haxx.se/mailman/listinfo/rockbox-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.