Skip to content

Commit

Permalink
fix: no longer dependent on the 'shell' option. also, optimized git c…
Browse files Browse the repository at this point in the history
…ommand processing.
  • Loading branch information
obaland committed Apr 4, 2023
1 parent e9ada82 commit d5a2dea
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 87 deletions.
2 changes: 1 addition & 1 deletion lua/vfiler/items/directory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function new_item(stat)
end

function Directory.create(dirpath)
if vim.fn.mkdir(dirpath) ~= 1 then
if not fs.create_directory(dirpath) then
return nil
end
local stat = fs.stat(dirpath)
Expand Down
11 changes: 1 addition & 10 deletions lua/vfiler/items/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ local fs = require('vfiler/libs/filesystem')
local File = {}

function File.create(path)
-- create file
local command
if core.is_windows then
command = ('type nul > "%s"'):format(path)
else
command = ('touch "%s"'):format(path)
end

local result = core.system(command)
if #result > 0 then
if not fs.create_file(path) then
return nil
end
return File.new(fs.stat(path))
Expand Down
3 changes: 1 addition & 2 deletions lua/vfiler/items/item.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local core = require('vfiler/libs/core')
local fs = require('vfiler/libs/filesystem')
local vim = require('vfiler/libs/vim')

local Item = {}
Item.__index = Item
Expand All @@ -22,7 +21,7 @@ function Item.new(stat)
end

function Item:delete()
if vim.fn.delete(self.path, 'rf') < 0 then
if not fs.delete(self.path) then
core.message.error('"%s" Cannot delete.', self.name)
return false
end
Expand Down
4 changes: 3 additions & 1 deletion lua/vfiler/libs/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function M.try(block)
end

function M.system(expr)
local result
local shell = vim.get_option('shell')
M.try({
function()
Expand All @@ -52,12 +53,13 @@ function M.system(expr)
else
vim.set_option('shell', '$SHELL')
end
vim.fn.system(expr)
result = vim.fn.system(expr)
end,
finally = function()
vim.set_option('shell', shell)
end,
})
return result
end

------------------------------------------------------------------------------
Expand Down
78 changes: 49 additions & 29 deletions lua/vfiler/libs/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,71 +166,91 @@ else
end
end

local copy_file_format, copy_directory_format

if core.is_windows then
copy_file_format = 'copy /y %s %s'
copy_directory_format = 'robocopy /s /e %s %s'
else
copy_file_format = 'cp -f %s %s'
copy_directory_format = 'cp -fR %s %s'
end

local function escape(path)
if core.is_windows then
-- trim end
path = path:gsub('[/\\]+$', '')
-- convert path separator
path = path:gsub('/', '\\')
return ('"%s"'):format(vim.fn.escape(path, '/'))
return path:gsub('/', '\\')
else
return vim.fn.shellescape(path)
end
end

function M.copy_directory(src, dest)
local command = copy_directory_format:format(escape(src), escape(dest))
return core.system(command)
if core.is_windows then
function M.copy_directory(src, dest)
core.system(
('robocopy /s /e "%s" "%s"'):format(escape(src), escape(dest))
)
return vim.v.shell_error < 8
end

function M.copy_file(src, dest)
core.system(('copy /y "%s" "%s"'):format(escape(src), escape(dest)))
return vim.v.shell_error == 0
end

function M.create_file(path)
core.system(('type nul > "%s"'):format(escape(path)))
return core.path.filereadable(path)
end
else
function M.copy_directory(src, dest)
core.system(('cp -fR %s %s'):format(escape(src), escape(dest)))
return vim.v.shell_error == 0
end

function M.copy_file(src, dest)
core.system(('cp -f %s %s'):format(escape(src), escape(dest)))
return vim.v.shell_error == 0
end

function M.create_file(path)
core.system(('touch %s'):format(escape(path)))
return core.path.filereadable(path)
end
end

function M.create_directory(path)
return vim.fn.mkdir(path) == 1
end

function M.copy_file(src, dest)
local command = copy_file_format:format(escape(src), escape(dest))
return core.system(command)
function M.delete(path)
return vim.fn.delete(path, 'rf') == 0
end

function M.execute(path)
local command
local escaped_path = vim.fn.shellescape(path)
local expr
if core.is_windows then
command = ('start rundll32 url.dll,FileProtocolHandler %s'):format(
vim.fn.escape(path, '#%')
expr = ('start rundll32 url.dll,FileProtocolHandler "%s"'):format(
escape(path)
)
elseif core.is_mac and vim.fn.executable('open') == 1 then
-- For Mac OS
command = ('open %s &'):format(escaped_path)
expr = ('open %s &'):format(escape(path))
elseif core.is_cygwin then
-- For Cygwin
command = ('cygstart %s'):format(escaped_path)
expr = ('cygstart %s'):format(escape(path))
elseif vim.fn.executable('xdg-open') == 1 then
-- For Linux
command = ('xdg-open %s &'):format(escaped_path)
expr = ('xdg-open %s &'):format(escape(path))
elseif
os.getenv('KDE_FULL_SESSION')
and os.getenv('KDE_FULL_SESSION') == 'true'
then
-- For KDE
command = ('kioclient exec %s &'):format(escaped_path)
expr = ('kioclient exec %s &'):format(escape(path))
elseif os.getenv('GNOME_DESKTOP_SESSION_ID') then
-- For GNOME
command = ('gnome-open %s &'):format(escaped_path)
expr = ('gnome-open %s &'):format(escape(path))
elseif vim.fn.executable('exo-open') == 1 then
-- For Xfce
command = ('exo-open %s &'):format(escaped_path)
expr = ('exo-open %s &'):format(escape(path))
else
core.message.error('Not supported platform.')
return
end
core.system(command)
core.system(expr)
end

function M.move(src, dest)
Expand Down
15 changes: 9 additions & 6 deletions lua/vfiler/libs/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ function M.get_toplevel(dirpath)
end

function M.reload_status(rootpath, options, on_completed)
local args = {
local command = {
'git',
'-C',
rootpath,
'--no-optional-locks',
'status',
'--porcelain=v1',
}

-- Options
if options.untracked then
table.insert(args, '-u')
table.insert(command, '-u')
end
if options.ignored then
table.insert(args, '--ignored=matching')
table.insert(command, '--ignored=matching')
else
table.insert(args, '--ignored=no')
table.insert(command, '--ignored=no')
end

local gitstatus = {}
local command = ('git -C "%s" %s'):format(rootpath, table.concat(args, ' '))

local job = Job.new()
job:start(command, {
on_received = function(jself, data)
Expand Down
1 change: 1 addition & 0 deletions lua/vfiler/libs/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local M = {}
-- Aliases
------------------------------------------------------------------------------
M.fn = vim.fn
M.v = vim.v
if is_nvim then
M.command = vim.api.nvim_command
M.nvim = vim
Expand Down
Loading

0 comments on commit d5a2dea

Please sign in to comment.