diff options
author | William Wilgus <wilgus.william@gmail.com> | 2020-09-25 23:07:30 -0400 |
---|---|---|
committer | William Wilgus <wilgus.william@gmail.com> | 2020-09-25 23:07:30 -0400 |
commit | 0f23cadbca7abeedcb493d0612e250a0259ca33e (patch) | |
tree | 58b3c2302f9a98c06de4fb3c35774fbc50fdd939 /apps | |
parent | 1f9e16e4df7cf03913c1f3e24d72a484e0c39f27 (diff) | |
download | rockbox-0f23cadbca7abeedcb493d0612e250a0259ca33e.tar.gz rockbox-0f23cadbca7abeedcb493d0612e250a0259ca33e.zip |
lua -- add sort by name, size, date to filebrowse include
I had previously added the fuctionality to luadir but I didn't update
the examples
also breaks out the file_browser function to be a bit more accessible
Change-Id: I14067256b9d76a757f732840cbee1cf84d775b1b
Diffstat (limited to 'apps')
-rw-r--r-- | apps/plugins/lua_scripts/file_browser.lua | 77 | ||||
-rwxr-xr-x | apps/plugins/lua_scripts/filebrowse.lua | 73 |
2 files changed, 139 insertions, 11 deletions
diff --git a/apps/plugins/lua_scripts/file_browser.lua b/apps/plugins/lua_scripts/file_browser.lua new file mode 100644 index 0000000000..19f475acf3 --- /dev/null +++ b/apps/plugins/lua_scripts/file_browser.lua | |||
@@ -0,0 +1,77 @@ | |||
1 | --[[ | ||
2 | __________ __ ___. | ||
3 | Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | \/ \/ \/ \/ \/ | ||
8 | $Id$ | ||
9 | Example Lua File Viewer script | ||
10 | Copyright (C) 2020 William Wilgus | ||
11 | This program is free software; you can redistribute it and/or | ||
12 | modify it under the terms of the GNU General Public License | ||
13 | as published by the Free Software Foundation; either version 2 | ||
14 | of the License, or (at your option) any later version. | ||
15 | This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | KIND, either express or implied. | ||
17 | ]]-- | ||
18 | |||
19 | require("actions") -- Contains rb.actions & rb.contexts | ||
20 | -- require("buttons") -- Contains rb.buttons -- not needed for this example | ||
21 | |||
22 | --local _timer = require("timer") | ||
23 | --local _clr = require("color") -- clrset, clrinc provides device independent colors | ||
24 | local _lcd = require("lcd") -- lcd helper functions | ||
25 | --local _print = require("print") -- advanced text printing | ||
26 | --local _img = require("image") -- image manipulation save, rotate, resize, tile, new, load | ||
27 | --local _blit = require("blit") -- handy list of blit operations | ||
28 | --local _draw = require("draw") -- draw all the things (primitives) | ||
29 | --local _math = require("math_ex") -- missing math sine cosine, sqrt, clamp functions | ||
30 | |||
31 | |||
32 | local scrpath = rb.current_path()--rb.PLUGIN_DIR .. "/demos/lua_scripts/" | ||
33 | |||
34 | package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path | ||
35 | |||
36 | require("printmenu") --menu | ||
37 | require("filebrowse") -- file browser | ||
38 | |||
39 | rb.actions = nil | ||
40 | package.loaded["actions"] = nil | ||
41 | |||
42 | -- uses print_table to display a menu | ||
43 | function main_menu() | ||
44 | local mt = { | ||
45 | [1] = "Rocklua File Browser Example", | ||
46 | [2] = "Sort by Name", | ||
47 | [3] = "Sort by Size", | ||
48 | [4] = "Sort by Date", | ||
49 | [5] = "Exit" | ||
50 | } | ||
51 | |||
52 | local ft = { | ||
53 | [0] = exit_now, --if user cancels do this function | ||
54 | [1] = function(TITLE) return true end, -- shouldn't happen title occupies this slot | ||
55 | [2] = function(SBNAME) | ||
56 | _lcd:splashf(rb.HZ, "%s", file_choose("/", "", "name", false) or "None") | ||
57 | end, | ||
58 | [3] = function(SBSIZE) | ||
59 | _lcd:splashf(rb.HZ, "%s", file_choose("/", "", "size", true) or "None") | ||
60 | end, | ||
61 | [4] = function(SBDATE) | ||
62 | _lcd:splashf(rb.HZ, "%s", file_choose("/", "", "date") or "None") | ||
63 | end, | ||
64 | [5] = function(EXIT_) return true end | ||
65 | } | ||
66 | |||
67 | print_menu(mt, ft) | ||
68 | |||
69 | end | ||
70 | |||
71 | function exit_now() | ||
72 | _lcd:update() | ||
73 | os.exit() | ||
74 | end -- exit_now | ||
75 | |||
76 | main_menu() | ||
77 | exit_now() | ||
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 | ]] |
36 | local function get_files(path, norecurse, finddir, findfile, f_t, d_t) | 36 | local 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 |
121 | function file_choose(dir, title) | 160 | -- sort_by "date" "name" "size" |
161 | -- descending true/false | ||
162 | function 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 |