open file with relative path when possible (#487)

This commit is contained in:
YIQUN
2021-07-07 02:46:10 +08:00
committed by GitHub
parent fd7f60e242
commit db9e70fde5
2 changed files with 8 additions and 3 deletions

View File

@@ -80,7 +80,7 @@ local keypress_funcs = {
close = function() M.close() end, close = function() M.close() end,
preview = function(node) preview = function(node)
if node.entries ~= nil or node.name == '..' then return end if node.entries ~= nil or node.name == '..' then return end
return lib.open_file('preview', node.absolute_path) return lib.open_file('preview', node.relative_path)
end, end,
} }
@@ -102,11 +102,11 @@ function M.on_keypress(mode)
end end
if node.link_to and not node.entries then if node.link_to and not node.entries then
lib.open_file(mode, node.link_to) lib.open_file(mode, node.relative_path)
elseif node.entries ~= nil then elseif node.entries ~= nil then
lib.unroll_dir(node) lib.unroll_dir(node)
else else
lib.open_file(mode, node.absolute_path) lib.open_file(mode, node.relative_path)
end end
end end

View File

@@ -41,10 +41,12 @@ end
local function file_new(cwd, name) local function file_new(cwd, name)
local absolute_path = utils.path_join({cwd, name}) local absolute_path = utils.path_join({cwd, name})
local relative_path = utils.path_relative(absolute_path, luv.cwd())
local is_exec = luv.fs_access(absolute_path, 'X') local is_exec = luv.fs_access(absolute_path, 'X')
return { return {
name = name, name = name,
absolute_path = absolute_path, absolute_path = absolute_path,
relative_path = relative_path,
executable = is_exec, executable = is_exec,
extension = string.match(name, ".?[^.]+%.(.*)") or "", extension = string.match(name, ".?[^.]+%.(.*)") or "",
match_name = path_to_matching_str(name), match_name = path_to_matching_str(name),
@@ -62,6 +64,8 @@ local function link_new(cwd, name)
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it. --- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
local absolute_path = utils.path_join({ cwd, name }) local absolute_path = utils.path_join({ cwd, name })
local link_to = luv.fs_realpath(absolute_path) local link_to = luv.fs_realpath(absolute_path)
-- if links to a file outside cwd, relative_path equals absolute_path
local relative_path = utils.path_relative(link_to, luv.cwd())
local stat = luv.fs_stat(absolute_path) local stat = luv.fs_stat(absolute_path)
local open, entries local open, entries
if (link_to ~= nil) and luv.fs_stat(link_to).type == 'directory' then if (link_to ~= nil) and luv.fs_stat(link_to).type == 'directory' then
@@ -77,6 +81,7 @@ local function link_new(cwd, name)
return { return {
name = name, name = name,
absolute_path = absolute_path, absolute_path = absolute_path,
relative_path = relative_path,
link_to = link_to, link_to = link_to,
last_modified = last_modified, last_modified = last_modified,
open = open, open = open,