From 0f23cadbca7abeedcb493d0612e250a0259ca33e Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Fri, 25 Sep 2020 23:07:30 -0400 Subject: 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 --- apps/plugins/lua_scripts/file_browser.lua | 77 +++++++++++++++++++++++++++++++ apps/plugins/lua_scripts/filebrowse.lua | 73 ++++++++++++++++++++++++----- 2 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 apps/plugins/lua_scripts/file_browser.lua 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 @@ +--[[ + __________ __ ___. + Open \______ \ ____ ____ | | _\_ |__ _______ ___ + Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + \/ \/ \/ \/ \/ + $Id$ + Example Lua File Viewer script + Copyright (C) 2020 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. +]]-- + +require("actions") -- Contains rb.actions & rb.contexts +-- require("buttons") -- Contains rb.buttons -- not needed for this example + +--local _timer = require("timer") +--local _clr = require("color") -- clrset, clrinc provides device independent colors +local _lcd = require("lcd") -- lcd helper functions +--local _print = require("print") -- advanced text printing +--local _img = require("image") -- image manipulation save, rotate, resize, tile, new, load +--local _blit = require("blit") -- handy list of blit operations +--local _draw = require("draw") -- draw all the things (primitives) +--local _math = require("math_ex") -- missing math sine cosine, sqrt, clamp functions + + +local scrpath = rb.current_path()--rb.PLUGIN_DIR .. "/demos/lua_scripts/" + +package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path + +require("printmenu") --menu +require("filebrowse") -- file browser + +rb.actions = nil +package.loaded["actions"] = nil + +-- uses print_table to display a menu +function main_menu() + local mt = { + [1] = "Rocklua File Browser Example", + [2] = "Sort by Name", + [3] = "Sort by Size", + [4] = "Sort by Date", + [5] = "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") + end, + [3] = function(SBSIZE) + _lcd:splashf(rb.HZ, "%s", file_choose("/", "", "size", true) or "None") + end, + [4] = function(SBDATE) + _lcd:splashf(rb.HZ, "%s", file_choose("/", "", "date") or "None") + end, + [5] = function(EXIT_) return true end + } + + print_menu(mt, ft) + +end + +function exit_now() + _lcd:update() + os.exit() +end -- exit_now + +main_menu() +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") -- 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, f_t, d_t) - +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 {} @@ -67,13 +67,15 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t) local function _get_files(path, cancelbtn) local sep = "" + local filepath + local finfo_t if string.sub(path, - 1) ~= "/" then sep = "/" end - for fname, isdir in luadir.dir(path) do - + 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 - table.insert(files, path .. sep ..fname) + 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 @@ -82,6 +84,8 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t) end end + + local function cmp_alphanum (op1, op2) local type1= type(op1) local type2 = type(op2) @@ -92,6 +96,7 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t) if type1 == "string" then op1 = op1:upper() op2 = op2:upper() + return sort_by_function(op1, op2) end return op1 < op2 end @@ -99,10 +104,44 @@ local function get_files(path, norecurse, finddir, findfile, f_t, d_t) _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 @@ -118,7 +157,9 @@ end -- get_files -------------------------------------------------------------------------------- -- uses print_table and get_files to display simple file browser -function file_choose(dir, title) +-- sort_by "date" "name" "size" +-- descending true/false +function file_choose(dir, title, sort_by, descending) local dstr, hstr = "" if not title then dstr = "%d items found in %0d.%02d seconds" @@ -126,6 +167,9 @@ function file_choose(dir, title) hstr = title end + if not sort_by then sort_by = "name" end + sort_by = sort_by:lower() + -- returns whole seconds and remainder local function tick2seconds(ticks) local secs = (ticks / rb.HZ) @@ -150,17 +194,24 @@ function file_choose(dir, title) timer = _timer() end - dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, dirs, files) + dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, sort_by, dirs, files) local parentdir = dirs[1] for i = 1, #dirs do dirs[i] = "\t" .. dirs[i] end - for i = 1, #files do - table.insert(dirs, "\t" .. files[i]) + if not descending then + for i = 1, #files do + -- only store file name .. strip attributes from end + table.insert(dirs, "\t" .. string.match(files[i], "[^;]+") or "?") + end + else + for i = #files, 1, -1 do + -- only store file name .. strip attributes from end + table.insert(dirs, "\t" .. string.match(files[i], "[^;]+") or "?") + end end - for i=1, #files do files[i] = nil end -- empty table for reuse if not title then -- cgit v1.2.3