A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 172 lines 5.2 kB view raw
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() 25 26package.path = scrpath .. "/?.lua;" .. package.path --add lua_scripts directory to path 27require("printmenus") 28 29rb.actions = nil 30package.loaded["actions"] = nil 31 32-------------------------------------------------------------------------------- 33local Icon_Plugin = 0x9 34 35 36local function get_files(path, norecurse, finddir, findfile, f_t, d_t) 37 38 local quit = false 39 40 local files = f_t or {} 41 local dirs = d_t or {} 42 43 local function f_filedir(name) 44 --default find function 45 -- example: return name:find(".mp3", 1, true) ~= nil 46 if name:len() <= 2 and (name == "." or name == "..") then 47 return false 48 end 49 if string.sub(name, 1, 1) == '.' then 50 return false 51 end 52 if string.sub(name, -4) == ".lua" then 53 return true 54 end 55 return false 56 end 57 local function d_filedir(name) 58 --default discard function 59 return false 60 end 61 62 if finddir == nil then 63 finddir = f_filedir 64 elseif type(finddir) ~= "function" then 65 finddir = d_filedir 66 end 67 68 if findfile == nil then 69 findfile = f_filedir 70 elseif type(findfile) ~= "function" then 71 findfile = d_filedir 72 end 73 74 local function _get_files(path, cancelbtn) 75 local sep = "" 76 if string.sub(path, - 1) ~= "/" then sep = "/" end 77 for fname, isdir in luadir.dir(path) do 78 79 if isdir and finddir(fname) then 80 table.insert(dirs, path .. sep ..fname) 81 elseif not isdir and findfile(fname) then 82 table.insert(files, path .. sep ..fname) 83 end 84 85 if rb.get_plugin_action(0) == cancelbtn then 86 return true 87 end 88 end 89 end 90 91 local function cmp_alphanum (op1, op2) 92 local type1= type(op1) 93 local type2 = type(op2) 94 95 if type1 ~= type2 then 96 return type1 < type2 97 else 98 if type1 == "string" then 99 op1 = op1:upper() 100 op2 = op2:upper() 101 end 102 return op1 < op2 103 end 104 end 105 106 table.insert(dirs, path) -- root 107 108 for key,value in pairs(dirs) do 109 --luadir.dir may error out so we need to do the call protected 110 _, quit = pcall(_get_files, value, CANCEL_BUTTON) 111 112 if quit == true or norecurse then 113 break; 114 end 115 end 116 117 table.sort(files, cmp_alphanum) 118 table.sort(dirs, cmp_alphanum) 119 120 return dirs, files 121end -- get_files 122-------------------------------------------------------------------------------- 123 124function icon_fn(item, icon) 125 if item ~= 0 then 126 icon = Icon_Plugin 127 else 128 icon = -1 129 end 130 return icon 131end 132 133-- uses print_table and get_files to display simple file browser 134function script_choose(dir, title) 135 local dstr 136 local hstr = title 137 138 local norecurse = true 139 local f_finddir = false -- function to match directories; nil all, false none 140 local f_findfile = nil -- function to match files; nil all, false none 141 local t_linedesc = {show_icons = true, icon_fn = icon_fn} 142 local p_settings = {wrap = true, hasheader = true, justify = "left", linedesc = t_linedesc} 143 local files = {} 144 local dirs = {} 145 local item = 1 146 rb.lcd_clear_display() 147 148 while item > 0 do 149 dirs, files = get_files(dir, norecurse, f_finddir, f_findfile, dirs, files) 150 for i=1, #dirs do dirs[i] = nil end -- empty table for reuse 151 table.insert(dirs, 1, hstr) 152 for i = 1, #files do 153 table.insert(dirs, "\t" .. string.gsub(files[i], ".*/","")) 154 end 155 --print_menu(menu_t, func_t, selected, settings, copy_screen) 156 _, item = print_menu(dirs, nil, 0, p_settings) 157 158 -- If item was selected follow directory or return filename 159 item = item or -1 160 if item > 0 then 161 dir = files[item - 1] 162 if not rb.dir_exists("/" .. dir) then 163 return dir 164 end 165 end 166 167 end 168end -- file_choose 169-------------------------------------------------------------------------------- 170 171local script_path = script_choose(scrpath, "lua scripts") 172if script_path then rb.restart_lua(script_path) end