mirror of
https://github.com/Rockbox/rockbox.git
synced 2026-07-10 13:29:52 -04:00
FS#13953 - lua add get_files() to its own module
get_files(path, recurse, finddir, findfile, sort_by, cancel_fn, f_t, d_t)
get_files returns a sorted tables of directories and (another) of files
path is the starting path; recurse == false.. only that path will be searched
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
sort_by can be by "name" "size" "date" or "none" to perform no sorting
note: for "size" and "date" you may need to strip the attribute data to use
the returned filename
cancel_fn if not defined or not a function no user cancel otherwise supply
your own cancel function which returns true to cancel searching for files
f_t and d_t allow you to pass your own tables for re-use but isn't necessary
Change-Id: Ic4c2ad9c9b5abeeeeaf572880edbc8873d52066b
This commit is contained in:
parent
097403e1a2
commit
ca7399821f
3 changed files with 186 additions and 134 deletions
|
|
@ -23,136 +23,9 @@ if ... == nil then rb.splash(rb.HZ * 3, "use 'require'") end
|
|||
require("printtable")
|
||||
local _lcd = require("lcd")
|
||||
local _timer = require("timer")
|
||||
local tmploader = require("temploader")
|
||||
--local get_files = require("get_files")--Using tmploader instead
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
--[[ returns a sorted tables of directories and (another) of files
|
||||
-- path is the starting path; norecurse == true.. only that path will be searched
|
||||
-- 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..
|
||||
-- f_t and d_t allow you to pass your own tables for re-use but isn't necessary
|
||||
]]
|
||||
local function get_files(path, norecurse, finddir, findfile, sort_by, f_t, d_t)
|
||||
local quit = false
|
||||
local sort_by_function -- forward declaration
|
||||
local filepath_function -- forward declaration
|
||||
local files = f_t or {}
|
||||
local dirs = d_t or {}
|
||||
|
||||
local function f_filedir(name)
|
||||
--default find function
|
||||
-- example: return name:find(".mp3", 1, true) ~= nil
|
||||
if name:len() <= 2 and (name == "." or name == "..") then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
local function d_filedir(name)
|
||||
--default discard function
|
||||
return false
|
||||
end
|
||||
|
||||
if finddir == nil then
|
||||
finddir = f_filedir
|
||||
elseif type(finddir) ~= "function" then
|
||||
finddir = d_filedir
|
||||
end
|
||||
|
||||
if findfile == nil then
|
||||
findfile = f_filedir
|
||||
elseif type(findfile) ~= "function" then
|
||||
findfile = d_filedir
|
||||
end
|
||||
|
||||
local function _get_files(path, cancelbtn)
|
||||
local sep = ""
|
||||
local filepath
|
||||
local finfo_t
|
||||
if string.sub(path, - 1) ~= "/" then sep = "/" end
|
||||
for fname, isdir, finfo_t in luadir.dir(path, true) do
|
||||
if isdir and finddir(fname) then
|
||||
table.insert(dirs, path .. sep ..fname)
|
||||
elseif not isdir and findfile(fname) then
|
||||
filepath = filepath_function(path, sep, fname, finfo_t.attribute, finfo_t.size, finfo_t.time)
|
||||
table.insert(files, filepath)
|
||||
end
|
||||
|
||||
if rb.get_plugin_action(0) == cancelbtn then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function cmp_alphanum (op1, op2)
|
||||
local type1= type(op1)
|
||||
local type2 = type(op2)
|
||||
|
||||
if type1 ~= type2 then
|
||||
return type1 < type2
|
||||
else
|
||||
if type1 == "string" then
|
||||
op1 = op1:upper()
|
||||
op2 = op2:upper()
|
||||
return sort_by_function(op1, op2)
|
||||
end
|
||||
return op1 < op2
|
||||
end
|
||||
end
|
||||
|
||||
_lcd:splashf(1, "Searching for Files")
|
||||
|
||||
if sort_by == "name" then
|
||||
sort_by_function = function(s1, s2) return s1 < s2 end
|
||||
filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
|
||||
return string.format("%s%s%s;", path, sep, fname)
|
||||
end
|
||||
elseif sort_by == "size" 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, "SZ:(%d+)")
|
||||
v2 = string.match(s2, "SZ:(%d+)")
|
||||
if v1 or v2 then
|
||||
return tonumber(v1 or 0) < tonumber(v2 or 0)
|
||||
end
|
||||
return s1 < s2
|
||||
end
|
||||
elseif sort_by == "date" 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, "TM:(%d+)")
|
||||
v2 = string.match(s2, "TM:(%d+)")
|
||||
if v1 or v2 then
|
||||
return tonumber(v1 or 0) < tonumber(v2 or 0)
|
||||
end
|
||||
return s1 < s2
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(dirs, path) -- root
|
||||
|
||||
for key,value in pairs(dirs) do
|
||||
--luadir.dir may error out so we need to do the call protected
|
||||
-- _get_files(value, CANCEL_BUTTON)
|
||||
_, quit = pcall(_get_files, value, CANCEL_BUTTON)
|
||||
|
||||
if quit == true or norecurse then
|
||||
break;
|
||||
end
|
||||
end
|
||||
|
||||
table.sort(files, cmp_alphanum)
|
||||
table.sort(dirs, cmp_alphanum)
|
||||
|
||||
return dirs, files
|
||||
end -- get_files
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
-- uses print_table and get_files to display simple file browser
|
||||
|
|
@ -176,7 +49,7 @@ function file_choose(dir, title, sort_by, descending)
|
|||
return secs, csecs
|
||||
end
|
||||
|
||||
local norecurse = true
|
||||
local recurse = false
|
||||
local f_finddir = nil -- function to match directories; nil all, false none
|
||||
local f_findfile = nil -- function to match files; nil all, false none
|
||||
|
||||
|
|
@ -186,14 +59,28 @@ function file_choose(dir, title, sort_by, descending)
|
|||
local files = {}
|
||||
local dirs = {}
|
||||
local item = 1
|
||||
_lcd:clear()
|
||||
local cancel_fn = nil
|
||||
|
||||
if CANCEL_BUTTON and CANCEL_BUTTON > 0 then
|
||||
cancel_fn = function()
|
||||
if rb.get_plugin_action(0) == CANCEL_BUTTON then rb.splash(0, "Canceled") return true end
|
||||
return false
|
||||
end
|
||||
else
|
||||
cancel_fn = function()
|
||||
if rb.action_userabort(0)then rb.splash(0, "Canceled") return true end
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
_lcd:clear()
|
||||
local get_files = tmploader("get_files")
|
||||
while item > 0 do
|
||||
if not title then
|
||||
timer = _timer()
|
||||
end
|
||||
|
||||
dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, sort_by, dirs, files)
|
||||
rb.splash(1, "Searching for Files")
|
||||
dirs, files = get_files(dir, recurse, f_finddir, f_findfile, sort_by, cancel_fn, dirs, files)
|
||||
|
||||
local parentdir = dirs[1]
|
||||
for i = 1, #dirs do
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ local _print = require("print")
|
|||
local _timer = require("timer")
|
||||
|
||||
require("actions")
|
||||
local CANCEL_BUTTON = rb.actions.PLA_CANCEL
|
||||
_G.CANCEL_BUTTON = rb.actions.PLA_CANCEL --global
|
||||
--------------------------------------------------------------------------------
|
||||
-- builds an index of byte position of every line at each bufsz increment
|
||||
-- in filename; bufsz == 1 would be every line; saves to filename.ext.idx_ext
|
||||
|
|
|
|||
165
apps/plugins/lua/include_lua/get_files.lua
Normal file
165
apps/plugins/lua/include_lua/get_files.lua
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
--[[
|
||||
/***************************************************************************
|
||||
* __________ __ ___.
|
||||
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
||||
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
||||
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
||||
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
||||
* \/ \/ \/ \/ \/
|
||||
*
|
||||
* Copyright (C) 2026 William Wilgus
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
||||
* KIND, either express or implied.
|
||||
*
|
||||
****************************************************************************/
|
||||
]]
|
||||
if ... == nil then rb.splash(rb.HZ * 3, "use 'require'") end
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
--[[ returns a sorted tables of directories and (another) of files
|
||||
-- path is the starting path; recurse == false.. only that path will be searched
|
||||
-- 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
|
||||
note: for "size" and "date" you may need to strip the attribute data to use
|
||||
the returned filename
|
||||
-- cancel_fn if not defined or not a function no user cancel otherwise supply
|
||||
your own cancel function which returns true to cancel searching for files
|
||||
-- f_t and d_t allow you to pass your own tables for re-use but isn't necessary
|
||||
]]
|
||||
local function get_files(path, recurse, finddir, findfile, sort_by, cancel_fn, f_t, d_t)
|
||||
local quit = false
|
||||
local sort_by_function = nil
|
||||
local filepath_function -- forward declaration
|
||||
local files = f_t or {}
|
||||
local dirs = d_t or {}
|
||||
|
||||
local function f_filedir(name)
|
||||
--default find function
|
||||
-- example: return name:find(".mp3", 1, true) ~= nil
|
||||
if name:len() <= 2 and (name == "." or name == "..") then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
local function d_filedir(name)
|
||||
--default discard function
|
||||
return false
|
||||
end
|
||||
|
||||
if finddir == nil then
|
||||
finddir = f_filedir
|
||||
elseif type(finddir) ~= "function" then
|
||||
finddir = d_filedir
|
||||
end
|
||||
|
||||
if findfile == nil then
|
||||
findfile = f_filedir
|
||||
elseif type(findfile) ~= "function" then
|
||||
findfile = d_filedir
|
||||
end
|
||||
|
||||
if type(cancel_fn) ~= "function" then
|
||||
cancel_fn = function() return false end
|
||||
end
|
||||
|
||||
local function _get_files(path)
|
||||
local sep = ""
|
||||
local filepath
|
||||
local finfo_t
|
||||
if string.sub(path, - 1) ~= "/" then sep = "/" end
|
||||
for fname, isdir, finfo_t in luadir.dir(path, true) do
|
||||
if isdir and finddir(fname) then
|
||||
table.insert(dirs, path .. sep ..fname)
|
||||
elseif not isdir and findfile(fname) then
|
||||
filepath = filepath_function(path, sep, fname, finfo_t.attribute, finfo_t.size, finfo_t.time)
|
||||
table.insert(files, filepath)
|
||||
end
|
||||
|
||||
if cancel_fn() == true then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false;
|
||||
end
|
||||
|
||||
local function cmp_alphanum (op1, op2)
|
||||
local type1= type(op1)
|
||||
local type2 = type(op2)
|
||||
|
||||
if type1 ~= type2 then
|
||||
return type1 < type2
|
||||
else
|
||||
if type1 == "string" then
|
||||
op1 = op1:upper()
|
||||
op2 = op2:upper()
|
||||
return sort_by_function(op1, op2)
|
||||
end
|
||||
return op1 < op2
|
||||
end
|
||||
end
|
||||
|
||||
if sort_by == "name" then
|
||||
sort_by_function = function(s1, s2) return s1 < s2 end
|
||||
filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
|
||||
return string.format("%s%s%s;", path, sep, fname)
|
||||
end
|
||||
elseif sort_by == "size" 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, "SZ:(%d+)")
|
||||
v2 = string.match(s2, "SZ:(%d+)")
|
||||
if v1 or v2 then
|
||||
return tonumber(v1 or 0) < tonumber(v2 or 0)
|
||||
end
|
||||
return s1 < s2
|
||||
end
|
||||
elseif sort_by == "date" 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, "TM:(%d+)")
|
||||
v2 = string.match(s2, "TM:(%d+)")
|
||||
if v1 or v2 then
|
||||
return tonumber(v1 or 0) < tonumber(v2 or 0)
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(dirs, path) -- root
|
||||
|
||||
for key,value in pairs(dirs) do
|
||||
--luadir.dir may error out so we need to do the call protected
|
||||
-- _get_files(value, CANCEL_BUTTON)
|
||||
_, quit = pcall(_get_files, value)
|
||||
|
||||
if quit == true or not recurse then
|
||||
break;
|
||||
end
|
||||
end
|
||||
if type(sort_by_function) == "function" then
|
||||
table.sort(files, cmp_alphanum)
|
||||
table.sort(dirs, cmp_alphanum)
|
||||
end
|
||||
|
||||
return dirs, files
|
||||
end -- get_files
|
||||
--------------------------------------------------------------------------------
|
||||
return get_files
|
||||
Loading…
Add table
Add a link
Reference in a new issue