perf: optimize reading directories

Instead of calling `utils.readdir()` twice to get directories and files, we now read the directory only once and sort it by item type. Also optimizes extension checking.

ref #344
This commit is contained in:
tomasklaen
2022-10-27 13:25:02 +02:00
parent 70c02b8a7c
commit 8538d6a177
3 changed files with 43 additions and 32 deletions

View File

@@ -157,17 +157,13 @@ function open_file_navigation_menu(directory_path, handle_select, opts)
return
end
local directories, dirs_error = utils.readdir(directory.path, 'dirs')
local files, files_error = get_files_in_directory(directory.path, opts.allowed_types)
local files, directories = read_directory(directory.path, opts.allowed_types)
local is_root = not directory.dirname
if not files or not directories then
msg.error('Retrieving files from ' .. directory .. ' failed: ' .. (dirs_error or files_error or ''))
return
end
if not files or not directories then return end
-- Files are already sorted
sort_filenames(directories)
sort_filenames(files)
-- Pre-populate items with parent directory selector if not at root
-- Each item value is a serialized path table it points to.