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")
## TODO
## TODO 1
- Tree creation should be async
- better error checking when fs updates
- sneak like cd command to find a directory
- better default colors (use default vim groups)
- give user option to choose for file generation command
- 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
- 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:
- 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
- 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

View File

@ -17,16 +17,7 @@ local function link_to(path)
return luv.fs_readlink(path) or ''
end
local function check_dir_access(path)
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)
local function print_err(err)
if err ~= nil then
api.nvim_command('echohl ErrorMsg')
api.nvim_command('echomsg "'..err..'"')
@ -34,17 +25,32 @@ function print_err(err)
end
end
local function rm(path)
local stat = luv.fs_lstat(path)
if stat and stat.type == 'directory' then
return luv.fs_rmdir(path, vim.schedule_wrap(print_err))
local function system(v)
print_err(api.nvim_call_function('system', { v }))
end
local function check_dir_access(path)
if luv.fs_access(path, 'R') == true then
return true
else
return luv.fs_unlink(path, vim.schedule_wrap(print_err))
print_err('Permission denied: ' .. path)
return false
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)
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
return {
@ -55,4 +61,5 @@ return {
is_dir = is_dir;
rename = rename;
rm = rm;
create = create;
}

View File

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