summaryrefslogtreecommitdiff
path: root/apps/plugins/lua_scripts.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua_scripts.lua')
-rw-r--r--apps/plugins/lua_scripts.lua161
1 files changed, 161 insertions, 0 deletions
diff --git a/apps/plugins/lua_scripts.lua b/apps/plugins/lua_scripts.lua
new file mode 100644
index 0000000000..02fe50b327
--- /dev/null
+++ b/apps/plugins/lua_scripts.lua
@@ -0,0 +1,161 @@
1--[[
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23
24local scrpath = rb.current_path() .. "/lua_scripts/"
25
26package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path
27require("printtable")
28
29rb.actions = nil
30package.loaded["actions"] = nil
31
32local excludedsrc = ";filebrowse.lua;fileviewers.lua;printmenu.lua;dbgettags.lua;"
33--------------------------------------------------------------------------------
34
35local function get_files(path, norecurse, finddir, findfile, f_t, d_t)
36
37 local quit = false
38
39 local files = f_t or {}
40 local dirs = d_t or {}
41
42 local function f_filedir(name)
43 --default find function
44 -- example: return name:find(".mp3", 1, true) ~= nil
45 if name:len() <= 2 and (name == "." or name == "..") then
46 return false
47 end
48 if string.find(excludedsrc, ";" .. name .. ";") then
49 return false
50 end
51 if string.sub(name, -4) == ".lua" then
52 return true
53 end
54 return false
55 end
56 local function d_filedir(name)
57 --default discard function
58 return false
59 end
60
61 if finddir == nil then
62 finddir = f_filedir
63 elseif type(finddir) ~= "function" then
64 finddir = d_filedir
65 end
66
67 if findfile == nil then
68 findfile = f_filedir
69 elseif type(findfile) ~= "function" then
70 findfile = d_filedir
71 end
72
73 local function _get_files(path, cancelbtn)
74 local sep = ""
75 if string.sub(path, - 1) ~= "/" then sep = "/" end
76 for fname, isdir in luadir.dir(path) do
77
78 if isdir and finddir(fname) then
79 table.insert(dirs, path .. sep ..fname)
80 elseif not isdir and findfile(fname) then
81 table.insert(files, path .. sep ..fname)
82 end
83
84 if rb.get_plugin_action(0) == cancelbtn then
85 return true
86 end
87 end
88 end
89
90 local function cmp_alphanum (op1, op2)
91 local type1= type(op1)
92 local type2 = type(op2)
93
94 if type1 ~= type2 then
95 return type1 < type2
96 else
97 if type1 == "string" then
98 op1 = op1:upper()
99 op2 = op2:upper()
100 end
101 return op1 < op2
102 end
103 end
104
105 table.insert(dirs, path) -- root
106
107 for key,value in pairs(dirs) do
108 --luadir.dir may error out so we need to do the call protected
109 _, quit = pcall(_get_files, value, CANCEL_BUTTON)
110
111 if quit == true or norecurse then
112 break;
113 end
114 end
115
116 table.sort(files, cmp_alphanum)
117 table.sort(dirs, cmp_alphanum)
118
119 return dirs, files
120end -- get_files
121--------------------------------------------------------------------------------
122
123-- uses print_table and get_files to display simple file browser
124function script_choose(dir, title)
125 local dstr
126 local hstr = title
127
128 local norecurse = true
129 local f_finddir = false -- function to match directories; nil all, false none
130 local f_findfile = nil -- function to match files; nil all, false none
131
132 local p_settings = {wrap = true, hasheader = true}
133 local files = {}
134 local dirs = {}
135 local item = 1
136 rb.lcd_clear_display()
137
138 while item > 0 do
139 dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, dirs, files)
140 for i=1, #dirs do dirs[i] = nil end -- empty table for reuse
141 table.insert(dirs, 1, hstr)
142 for i = 1, #files do
143 table.insert(dirs, "\t" .. string.gsub(files[i], ".*/",""))
144 end
145
146 item = print_table(dirs, #dirs, p_settings)
147
148 -- If item was selected follow directory or return filename
149 if item > 0 then
150 dir = files[item - 1]
151 if not rb.dir_exists("/" .. dir) then
152 return dir
153 end
154 end
155
156 end
157end -- file_choose
158--------------------------------------------------------------------------------
159
160local script_path = script_choose(scrpath, "lua scripts")
161if script_path then rb.restart_lua(script_path) end