summaryrefslogtreecommitdiff
path: root/apps/plugins/lua_scripts/filebrowse.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua_scripts/filebrowse.lua')
-rwxr-xr-xapps/plugins/lua_scripts/filebrowse.lua73
1 files changed, 62 insertions, 11 deletions
diff --git a/apps/plugins/lua_scripts/filebrowse.lua b/apps/plugins/lua_scripts/filebrowse.lua
index 5e75365cf8..0640ec3764 100755
--- a/apps/plugins/lua_scripts/filebrowse.lua
+++ b/apps/plugins/lua_scripts/filebrowse.lua
@@ -33,10 +33,10 @@ local _timer = require("timer")
33-- or you can provide your own function see below.. 33-- or you can provide your own function see below..
34-- f_t and d_t allow you to pass your own tables for re-use but isn't necessary 34-- f_t and d_t allow you to pass your own tables for re-use but isn't necessary
35]] 35]]
36local function get_files(path, norecurse, finddir, findfile, f_t, d_t) 36local function get_files(path, norecurse, finddir, findfile, sort_by, f_t, d_t)
37
38 local quit = false 37 local quit = false
39 38 local sort_by_function -- forward declaration
39 local filepath_function -- forward declaration
40 local files = f_t or {} 40 local files = f_t or {}
41 local dirs = d_t or {} 41 local dirs = d_t or {}
42 42
@@ -67,13 +67,15 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t)
67 67
68 local function _get_files(path, cancelbtn) 68 local function _get_files(path, cancelbtn)
69 local sep = "" 69 local sep = ""
70 local filepath
71 local finfo_t
70 if string.sub(path, - 1) ~= "/" then sep = "/" end 72 if string.sub(path, - 1) ~= "/" then sep = "/" end
71 for fname, isdir in luadir.dir(path) do 73 for fname, isdir, finfo_t in luadir.dir(path, true) do
72
73 if isdir and finddir(fname) then 74 if isdir and finddir(fname) then
74 table.insert(dirs, path .. sep ..fname) 75 table.insert(dirs, path .. sep ..fname)
75 elseif not isdir and findfile(fname) then 76 elseif not isdir and findfile(fname) then
76 table.insert(files, path .. sep ..fname) 77 filepath = filepath_function(path, sep, fname, finfo_t.attribute, finfo_t.size, finfo_t.time)
78 table.insert(files, filepath)
77 end 79 end
78 80
79 if rb.get_plugin_action(0) == cancelbtn then 81 if rb.get_plugin_action(0) == cancelbtn then
@@ -82,6 +84,8 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t)
82 end 84 end
83 end 85 end
84 86
87
88
85 local function cmp_alphanum (op1, op2) 89 local function cmp_alphanum (op1, op2)
86 local type1= type(op1) 90 local type1= type(op1)
87 local type2 = type(op2) 91 local type2 = type(op2)
@@ -92,6 +96,7 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t)
92 if type1 == "string" then 96 if type1 == "string" then
93 op1 = op1:upper() 97 op1 = op1:upper()
94 op2 = op2:upper() 98 op2 = op2:upper()
99 return sort_by_function(op1, op2)
95 end 100 end
96 return op1 < op2 101 return op1 < op2
97 end 102 end
@@ -99,10 +104,44 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t)
99 104
100 _lcd:splashf(1, "Searching for Files") 105 _lcd:splashf(1, "Searching for Files")
101 106
107 if sort_by == "name" then
108 sort_by_function = function(s1, s2) return s1 < s2 end
109 filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
110 return string.format("%s%s%s;", path, sep, fname)
111 end
112 elseif sort_by == "size" then
113 filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
114 return string.format("%s%s%s; At:%d, Sz:%d, Tm:%d", path, sep, fname, fattrib, fsize, ftime)
115 end
116 sort_by_function = function(s1, s2)
117 local v1, v2
118 v1 = string.match(s1, "SZ:(%d+)")
119 v2 = string.match(s2, "SZ:(%d+)")
120 if v1 or v2 then
121 return tonumber(v1 or 0) < tonumber(v2 or 0)
122 end
123 return s1 < s2
124 end
125 elseif sort_by == "date" then
126 filepath_function = function(path, sep, fname, fattrib, fsize, ftime)
127 return string.format("%s%s%s; At:%d, Sz:%d, Tm:%d", path, sep, fname, fattrib, fsize, ftime)
128 end
129 sort_by_function = function(s1, s2)
130 local v1, v2
131 v1 = string.match(s1, "TM:(%d+)")
132 v2 = string.match(s2, "TM:(%d+)")
133 if v1 or v2 then
134 return tonumber(v1 or 0) < tonumber(v2 or 0)
135 end
136 return s1 < s2
137 end
138 end
139
102 table.insert(dirs, path) -- root 140 table.insert(dirs, path) -- root
103 141
104 for key,value in pairs(dirs) do 142 for key,value in pairs(dirs) do
105 --luadir.dir may error out so we need to do the call protected 143 --luadir.dir may error out so we need to do the call protected
144 -- _get_files(value, CANCEL_BUTTON)
106 _, quit = pcall(_get_files, value, CANCEL_BUTTON) 145 _, quit = pcall(_get_files, value, CANCEL_BUTTON)
107 146
108 if quit == true or norecurse then 147 if quit == true or norecurse then
@@ -118,7 +157,9 @@ end -- get_files
118-------------------------------------------------------------------------------- 157--------------------------------------------------------------------------------
119 158
120-- uses print_table and get_files to display simple file browser 159-- uses print_table and get_files to display simple file browser
121function file_choose(dir, title) 160-- sort_by "date" "name" "size"
161-- descending true/false
162function file_choose(dir, title, sort_by, descending)
122 local dstr, hstr = "" 163 local dstr, hstr = ""
123 if not title then 164 if not title then
124 dstr = "%d items found in %0d.%02d seconds" 165 dstr = "%d items found in %0d.%02d seconds"
@@ -126,6 +167,9 @@ function file_choose(dir, title)
126 hstr = title 167 hstr = title
127 end 168 end
128 169
170 if not sort_by then sort_by = "name" end
171 sort_by = sort_by:lower()
172
129 -- returns whole seconds and remainder 173 -- returns whole seconds and remainder
130 local function tick2seconds(ticks) 174 local function tick2seconds(ticks)
131 local secs = (ticks / rb.HZ) 175 local secs = (ticks / rb.HZ)
@@ -150,17 +194,24 @@ function file_choose(dir, title)
150 timer = _timer() 194 timer = _timer()
151 end 195 end
152 196
153 dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, dirs, files) 197 dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, sort_by, dirs, files)
154 198
155 local parentdir = dirs[1] 199 local parentdir = dirs[1]
156 for i = 1, #dirs do 200 for i = 1, #dirs do
157 dirs[i] = "\t" .. dirs[i] 201 dirs[i] = "\t" .. dirs[i]
158 end 202 end
159 203
160 for i = 1, #files do 204 if not descending then
161 table.insert(dirs, "\t" .. files[i]) 205 for i = 1, #files do
206 -- only store file name .. strip attributes from end
207 table.insert(dirs, "\t" .. string.match(files[i], "[^;]+") or "?")
208 end
209 else
210 for i = #files, 1, -1 do
211 -- only store file name .. strip attributes from end
212 table.insert(dirs, "\t" .. string.match(files[i], "[^;]+") or "?")
213 end
162 end 214 end
163
164 for i=1, #files do files[i] = nil end -- empty table for reuse 215 for i=1, #files do files[i] = nil end -- empty table for reuse
165 216
166 if not title then 217 if not title then