Use system function instead of luv calls

- add dynamic path creation 'foo/bar/baz'
- fixes delete function
This commit is contained in:
kyazdani42
2020-02-28 15:34:02 +01:00
parent f6e44c1726
commit b9398b285d
3 changed files with 45 additions and 32 deletions

View File

@@ -52,18 +52,18 @@ nnoremap <leader>n :LuaTreeRefresh<CR>
![alt text](.github/screenshot.png?raw=true "file explorer") ![alt text](.github/screenshot.png?raw=true "file explorer")
## TODO ## TODO 1
- Tree creation should be async - Tree creation should be async
- better error checking when fs updates
- sneak like cd command to find a directory - sneak like cd command to find a directory
- better default colors (use default vim groups) - better default colors (use default vim groups)
- give user option to choose for file generation command - give user option to choose for file generation command
- command to find current file in the directory structure - command to find current file in the directory structure
- refactor all `system` call to `libuv` functions, with better error management
- create proper highlight groups or add highlight function to give the user ability to setup colors themselves - create proper highlight groups or add highlight function to give the user ability to setup colors themselves
- bufferize leafs of node being closed so when opening again the node, we open every directory that was previously open - bufferize leafs of node being closed so when opening again the node, we open every directory that was previously open
- use libuv functions instead of `touch` and `mkdir` in `create_file()` and allow file creation with path like `foo/bar/baz`
- better window management: - better window management:
- check tree buffer/window for change so we can avoid it being resized or moved around or replaced by another file - check tree buffer/window for change so we can avoid it being resized or moved around or replaced by another file
- monitor window layout in current tab to open files in the right place - monitor window layout in current tab to open files in the right place
- add `<C-t>` to open buffer in new tab - add `<C-t>` to open buffer in new tab
> this might be a little hard to implement since window layout events do not exist yet > this might be a little hard to implement since window layout events do not exist yet

View File

@@ -17,16 +17,7 @@ local function link_to(path)
return luv.fs_readlink(path) or '' return luv.fs_readlink(path) or ''
end end
local function check_dir_access(path) local function print_err(err)
if luv.fs_access(path, 'R') == true then
return true
else
api.nvim_err_writeln('Permission denied: ' .. path)
return false
end
end
function print_err(err)
if err ~= nil then if err ~= nil then
api.nvim_command('echohl ErrorMsg') api.nvim_command('echohl ErrorMsg')
api.nvim_command('echomsg "'..err..'"') api.nvim_command('echomsg "'..err..'"')
@@ -34,17 +25,32 @@ function print_err(err)
end end
end end
local function rm(path) local function system(v)
local stat = luv.fs_lstat(path) print_err(api.nvim_call_function('system', { v }))
if stat and stat.type == 'directory' then end
return luv.fs_rmdir(path, vim.schedule_wrap(print_err))
local function check_dir_access(path)
if luv.fs_access(path, 'R') == true then
return true
else else
return luv.fs_unlink(path, vim.schedule_wrap(print_err)) print_err('Permission denied: ' .. path)
return false
end end
end end
-- TODO: better handling of path removal, rename and file creation with luv calls
-- it will take some time so leave it for a dedicated PR
local function rm(path)
system('rm -rf ' ..path)
end
local function rename(file, new_path) local function rename(file, new_path)
luv.fs_rename(file, new_path, vim.schedule_wrap(print_err)) system('mv '..file..' '..new_path)
end
local function create(file, folders)
if folders ~= "" then system('mkdir -p '..folders) end
if file ~= nil then system('touch ' ..folders..file) end
end end
return { return {
@@ -55,4 +61,5 @@ return {
is_dir = is_dir; is_dir = is_dir;
rename = rename; rename = rename;
rm = rm; rm = rm;
create = create;
} }

View File

@@ -3,12 +3,11 @@ local api = vim.api
local update_view = require 'lib/winutils'.update_view local update_view = require 'lib/winutils'.update_view
local refresh_tree = require 'lib/state'.refresh_tree local refresh_tree = require 'lib/state'.refresh_tree
local refresh_git = require 'lib/git'.refresh_git local refresh_git = require 'lib/git'.refresh_git
local rm = require 'lib/fs'.rm
local rename = require 'lib/fs'.rename
local function system(v) local fs = require 'lib/fs'
api.nvim_call_function('system', { v }) local rm = fs.rm
end local rename = fs.rename
local create = fs.create
local function input(v) local function input(v)
local param local param
@@ -17,20 +16,27 @@ local function input(v)
end end
local function clear_prompt() local function clear_prompt()
api.nvim_command('echo "\r' .. string.rep(" ", 80) .. '"') api.nvim_command('echo "\r' .. string.rep(" ", 200) .. '\n"')
end end
local function create_file(path) local function create_file(path)
-- TODO: create files dynamically
local new_file = input("Create file: " .. path) local new_file = input("Create file: " .. path)
local new_path = path .. new_file
clear_prompt() clear_prompt()
-- TODO: replace system() calls with luv calls
if string.match(new_file, '.*/$') then local file = nil
system('mkdir -p ' .. new_path) if not string.match(new_file, '.*/$') then
else file = string.reverse(string.gsub(string.reverse(new_file), '/.*$', ''))
system('touch ' .. new_path) new_file = string.gsub(new_file, '[^/]*$', '')
end end
local folders = ""
if #new_file ~= 0 then
for p in string.gmatch(new_file, '[^/]*') do
folders = folders .. p .. '/'
end
end
create(file, folders)
refresh_git() refresh_git()
refresh_tree() refresh_tree()
update_view() update_view()