summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua_scripts/lua_scripts.lua
blob: 331611d3897086927c7eee22b6b34312ff000ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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