From d7c541742f5e6ec07dbcc8e1346efde9d807437e Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sat, 4 May 2024 08:41:16 -0400 Subject: Allow first level folders in plugin menu add sorting directories as files move picross files to a hidden folder use directory for lua_scripts, sgt_puzzles make plugin browser able to handle 1st level directories Change-Id: I30852d71dc992c378d5790756e94f06f5a2e9bef --- apps/plugins/lua_scripts/lua_scripts.lua | 172 ++++++++++++++++++++++++++++++ apps/plugins/lua_scripts/lua_scripts.make | 4 +- 2 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 apps/plugins/lua_scripts/lua_scripts.lua (limited to 'apps/plugins/lua_scripts') diff --git a/apps/plugins/lua_scripts/lua_scripts.lua b/apps/plugins/lua_scripts/lua_scripts.lua new file mode 100644 index 0000000000..331611d389 --- /dev/null +++ b/apps/plugins/lua_scripts/lua_scripts.lua @@ -0,0 +1,172 @@ +--[[ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2017 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. + * + ****************************************************************************/ +]] + +local scrpath = rb.current_path() + +package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path +require("printmenus") + +rb.actions = nil +package.loaded["actions"] = nil + +-------------------------------------------------------------------------------- +local Icon_Plugin = 0x9 + + +local function get_files(path, norecurse, finddir, findfile, f_t, d_t) + + local quit = false + + 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 + if string.sub(name, 1, 1) == '.' then + return false + end + if string.sub(name, -4) == ".lua" then + return true + end + return false + 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 = "" + if string.sub(path, - 1) ~= "/" then sep = "/" end + for fname, isdir in luadir.dir(path) 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) + 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() + end + return op1 < op2 + 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 + _, 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 +-------------------------------------------------------------------------------- + +function icon_fn(item, icon) + if item ~= 0 then + icon = Icon_Plugin + else + icon = -1 + end + return icon +end + +-- uses print_table and get_files to display simple file browser +function script_choose(dir, title) + local dstr + local hstr = title + + local norecurse = true + local f_finddir = false -- function to match directories; nil all, false none + local f_findfile = nil -- function to match files; nil all, false none + local t_linedesc = {show_icons = true, icon_fn = icon_fn} + local p_settings = {wrap = true, hasheader = true, justify = "left", linedesc = t_linedesc} + local files = {} + local dirs = {} + local item = 1 + rb.lcd_clear_display() + + while item > 0 do + dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, dirs, files) + for i=1, #dirs do dirs[i] = nil end -- empty table for reuse + table.insert(dirs, 1, hstr) + for i = 1, #files do + table.insert(dirs, "\t" .. string.gsub(files[i], ".*/","")) + end + --print_menu(menu_t, func_t, selected, settings, copy_screen) + _, item = print_menu(dirs, nil, 0, p_settings) + + -- If item was selected follow directory or return filename + item = item or -1 + if item > 0 then + dir = files[item - 1] + if not rb.dir_exists("/" .. dir) then + return dir + end + end + + end +end -- file_choose +-------------------------------------------------------------------------------- + +local script_path = script_choose(scrpath, "lua scripts") +if script_path then rb.restart_lua(script_path) end diff --git a/apps/plugins/lua_scripts/lua_scripts.make b/apps/plugins/lua_scripts/lua_scripts.make index 2f46f9d74a..9151ab6d1c 100644 --- a/apps/plugins/lua_scripts/lua_scripts.make +++ b/apps/plugins/lua_scripts/lua_scripts.make @@ -10,7 +10,6 @@ LUASCR_SRCDIR := $(APPSDIR)/plugins/lua_scripts LUASCR_BUILDDIR := $(BUILDDIR)/apps/plugins/lua_scripts LUASCRS := $(wildcard $(LUASCR_SRCDIR)/*.lua) - #DUMMY := $(info [${LUASCRS}]) DUMMY : all @@ -18,7 +17,6 @@ DUMMY : all all: $(subst $(LUASCR_SRCDIR)/,$(LUASCR_BUILDDIR)/,$(LUASCRS)) $(LUASCR_BUILDDIR)/%.lua: $(LUASCR_SRCDIR)/%.lua | $(LUASCR_BUILDDIR) - $(call PRINTS,CP $(subst $(LUASCR_SRCDIR)/,,$<))cp $< $@ - + $(call PRINTS,CP $(subst $(APPSDIR)/,,$<))cp $< $@ $(LUASCR_BUILDDIR): $(call PRINTS,MKDIR $@)mkdir -p $(LUASCR_BUILDDIR)/ -- cgit v1.2.3