file.lua (1207B)
1 -- This file is an internal file-browser addon. 2 -- It should not be imported like a normal module. 3 4 local msg = require 'mp.msg' 5 local utils = require 'mp.utils' 6 7 ---Parser for native filesystems 8 ---@type ParserConfig 9 local file_parser = { 10 name = "file", 11 priority = 110, 12 api_version = '1.0.0', 13 } 14 15 --try to parse any directory except for the root 16 function file_parser:can_parse(directory) 17 return directory ~= '' 18 end 19 20 --scans the given directory using the mp.utils.readdir function 21 function file_parser:parse(directory) 22 local new_list = {} 23 local list1 = utils.readdir(directory, 'dirs') 24 if list1 == nil then return nil end 25 26 --sorts folders and formats them into the list of directories 27 for i=1, #list1 do 28 local item = list1[i] 29 30 msg.trace(item..'/') 31 table.insert(new_list, {name = item..'/', type = 'dir'}) 32 end 33 34 --appends files to the list of directory items 35 local list2 = utils.readdir(directory, 'files') 36 if list2 == nil then return nil end 37 for i=1, #list2 do 38 local item = list2[i] 39 40 msg.trace(item) 41 table.insert(new_list, {name = item, type = 'file'}) 42 end 43 return new_list 44 end 45 46 return file_parser