refactor: don't overwrite default libraries and expose join_path() and path_separator() types

This commit is contained in:
tomasklaen
2022-10-31 09:43:18 +01:00
parent d9e5853542
commit e8060c74ad
3 changed files with 15 additions and 15 deletions

View File

@@ -482,7 +482,7 @@ function load_file_index_in_current_directory(index)
if index < 0 then index = #files + index + 1 end if index < 0 then index = #files + index + 1 end
if files[index] then if files[index] then
mp.commandv('loadfile', utils.join_path(serialized.dirname, files[index])) mp.commandv('loadfile', join_path(serialized.dirname, files[index]))
end end
end end
end end

View File

@@ -186,7 +186,7 @@ function open_file_navigation_menu(directory_path, handle_select, opts)
local path_separator = path_separator(directory.path) local path_separator = path_separator(directory.path)
for _, dir in ipairs(directories) do for _, dir in ipairs(directories) do
local serialized = serialize_path(utils.join_path(directory.path, dir)) local serialized = serialize_path(join_path(directory.path, dir))
if serialized then if serialized then
serialized.is_directory = true serialized.is_directory = true
items[#items + 1] = {title = serialized.basename, value = serialized, hint = path_separator} items[#items + 1] = {title = serialized.basename, value = serialized, hint = path_separator}
@@ -194,7 +194,7 @@ function open_file_navigation_menu(directory_path, handle_select, opts)
end end
for _, file in ipairs(files) do for _, file in ipairs(files) do
local serialized = serialize_path(utils.join_path(directory.path, file)) local serialized = serialize_path(join_path(directory.path, file))
if serialized then if serialized then
serialized.is_file = true serialized.is_file = true
items[#items + 1] = {title = serialized.basename, value = serialized} items[#items + 1] = {title = serialized.basename, value = serialized}

View File

@@ -145,23 +145,23 @@ function opacity_to_alpha(opacity)
return 255 - math.ceil(255 * opacity) return 255 - math.ceil(255 * opacity)
end end
do path_separator = (function()
local os_separator = state.os == 'windows' and '\\' or '/' local os_separator = state.os == 'windows' and '\\' or '/'
-- Get appropriate path separator for the given path. -- Get appropriate path separator for the given path.
---@param path string ---@param path string
---@return string ---@return string
function path_separator(path) return function(path)
return path:sub(1, 2) == '\\\\' and '\\' or os_separator return path:sub(1, 2) == '\\\\' and '\\' or os_separator
end end
end)()
-- Joins paths with the OS aware path separator or UNC separator. -- Joins paths with the OS aware path separator or UNC separator.
---@param p1 string ---@param p1 string
---@param p2 string ---@param p2 string
---@return string ---@return string
function utils.join_path(p1, p2) function join_path(p1, p2)
return p1 .. path_separator(p1) .. p2 return p1 .. path_separator(p1) .. p2
end
end end
-- Check if path is absolute. -- Check if path is absolute.
@@ -178,7 +178,7 @@ end
---@return string ---@return string
function ensure_absolute(path) function ensure_absolute(path)
if is_absolute(path) then return path end if is_absolute(path) then return path end
return utils.join_path(state.cwd, path) return join_path(state.cwd, path)
end end
-- Remove trailing slashes/backslashes. -- Remove trailing slashes/backslashes.
@@ -286,7 +286,7 @@ function read_directory(path, allowed_types)
for _, item in ipairs(items) do for _, item in ipairs(items) do
if item ~= '.' and item ~= '..' then if item ~= '.' and item ~= '..' then
local info = utils.file_info(utils.join_path(path, item)) local info = utils.file_info(join_path(path, item))
if info then if info then
if info.is_file then if info.is_file then
if not allowed_types or has_any_extension(item, allowed_types) then if not allowed_types or has_any_extension(item, allowed_types) then
@@ -313,7 +313,7 @@ function get_adjacent_files(file_path, allowed_types)
local current_file_index local current_file_index
local paths = {} local paths = {}
for index, file in ipairs(files) do for index, file in ipairs(files) do
paths[#paths + 1] = utils.join_path(current_file.dirname, file) paths[#paths + 1] = join_path(current_file.dirname, file)
if current_file.basename == file then current_file_index = index end if current_file.basename == file then current_file_index = index end
end end
if not current_file_index then return end if not current_file_index then return end