remove scratch winutils and use input to alter fs

This commit is contained in:
kyazdani42 2020-02-20 17:01:43 +01:00
parent 460fdf6b7a
commit e86c857960
7 changed files with 139 additions and 214 deletions

View File

@ -17,9 +17,10 @@
- [x] Mouse support
## TODO
- display error on Read access denied (and better handling of errors if any)
- fix all window problems (force size to stay always the same and keep it on the side)
- add options for users
- add docs
- add options for users (tree side, tree size)
- cd command to move faster accross the fs if needed
- quickly find file in the directory structure
- use libuv functions instead of `touch` and `mkdir` in `add_file()`
- use libuv functions instead of `touch` and `mkdir` in `create_file()` and allow file creation with path like `foo/bar/baz`
- tree should always stay on the left no matter what

View File

@ -1,75 +0,0 @@
local api = vim.api
local luv = vim.loop
local system = function(v) api.nvim_call_function('system', { v }) end
local update_tree_view = require 'lib/winutils'.update_view
local scratch_wrapper = require 'lib/winutils'.scratch_wrapper
local update_tree_state = require 'lib/state'.refresh_tree
local refresh_git = require 'lib/git'.refresh_git
local EDIT_FILE = nil
local function edit_add(path)
scratch_wrapper("add", { "Create File", path })
end
local function edit_remove(filename, path)
EDIT_FILE = path .. filename
scratch_wrapper("remove", { "Remove " .. filename .. " ?", "y/n: " })
end
local function edit_rename(filename, path)
EDIT_FILE = path .. filename
scratch_wrapper("rename", { "Rename " .. path, path .. filename })
end
local function add_file()
local path = api.nvim_get_current_line()
if string.match(path, '.*/$') then
system('mkdir -p ' .. path)
refresh_git()
update_tree_state()
update_tree_view()
else
system('touch ' .. path)
refresh_git()
update_tree_state()
update_tree_view()
end
api.nvim_command("q!")
end
local function remove_file()
local confirmation = api.nvim_get_current_line()
if string.match(confirmation, '^y/n: y.*$') ~= nil then
local stat = luv.fs_lstat(EDIT_FILE)
if stat.type == 'directory' then
luv.fs_rmdir(EDIT_FILE)
else
luv.fs_unlink(EDIT_FILE)
end
refresh_git()
update_tree_state()
update_tree_view()
end
EDIT_FILE = nil
api.nvim_command("q!")
end
local function rename_file()
local path = api.nvim_get_current_line()
luv.fs_rename(EDIT_FILE, path)
refresh_git()
update_tree_state()
update_tree_view()
EDIT_FILE = nil
api.nvim_command("q!")
end
return {
edit_add = edit_add;
edit_remove = edit_remove;
edit_rename = edit_rename;
add_file = add_file;
remove_file = remove_file;
rename_file = rename_file;
}

50
lua/lib/fs.lua Normal file
View File

@ -0,0 +1,50 @@
local api = vim.api
local luv = vim.loop
local function get_cwd() return luv.cwd() end
local function is_dir(path)
local stat = luv.fs_lstat(path)
return stat and stat.type == 'directory' or false
end
local function is_symlink(path)
local stat = luv.fs_lstat(path)
return stat and stat.type == 'link' or false
end
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
local function rm(path)
local stat = luv.fs_lstat(path)
if stat and stat.type == 'directory' then
return luv.fs_rmdir(path)
else
return luv.fs_unlink(path)
end
end
local function rename(file, new_path)
return luv.fs_rename(file, new_path)
end
return {
check_dir_access = check_dir_access;
is_dir = is_dir;
is_symlink = is_symlink;
link_to = link_to;
get_cwd = get_cwd;
rm = rm;
rename = rename;
}

52
lua/lib/fs_update.lua Normal file
View File

@ -0,0 +1,52 @@
local api = vim.api
local system = function(v) api.nvim_call_function('system', { v }) end
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 input = function(v)
local param
if type(v) == 'string' then param = { v } else param = v end
return api.nvim_call_function('input', param)
end
local function create_file(path)
-- TODO: create files dynamically
local new_file = input("Create file: " .. path)
local new_path = path .. new_file
if string.match(new_file, '.*/$') then
system('mkdir -p ' .. new_path)
else
system('touch ' .. new_path)
end
refresh_git()
refresh_tree()
update_view()
end
local function remove_file(filename, path)
local confirm = input("Remove " .. filename .. " ? y/n: ")
if string.match(confirm, 'y.*$') ~= nil then
rm(path .. filename)
refresh_git()
refresh_tree()
update_view()
end
end
local function rename_file(filename, path)
local new_path = input({"Rename file " .. filename .. " : ", path .. filename})
rename(path .. filename, new_path)
refresh_git()
refresh_tree()
update_view()
end
return {
create_file = create_file;
remove_file = remove_file;
rename_file = rename_file;
}

View File

@ -1,8 +1,13 @@
local api = vim.api
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end
local get_git_attr = require 'lib/git'.get_git_attr
local function get_cwd() return vim.loop.cwd() end
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end
local get_git_attr = require 'lib/git'.get_git_attr
local fs = require 'lib/fs'
local is_dir = fs.is_dir
local is_symlink = fs.is_symlink
local get_cwd = fs.get_cwd
local ROOT_PATH = get_cwd() .. '/'
local function set_root_path(path)
@ -11,32 +16,9 @@ end
local Tree = {}
local function is_dir(path)
local stat = vim.loop.fs_lstat(path)
return stat and stat.type == 'directory' or false
end
local function is_symlink(path)
local stat = vim.loop.fs_lstat(path)
return stat and stat.type == 'link' or false
end
local function link_to(path)
return vim.loop.fs_readlink(path) or ''
end
local function check_dir_access(path)
return vim.loop.fs_access(path, 'R') == true
end
local function list_dirs(path)
if check_dir_access(path) == false then
-- TODO: display an error here (permission denied)
return {}
else
local ls_cmd = 'ls -A --ignore=.git ' ..path
return syslist(ls_cmd)
end
local ls_cmd = 'ls -A --ignore=.git ' ..path
return syslist(ls_cmd)
end
local function sort_dirs(dirs)
@ -153,8 +135,6 @@ return {
get_tree = get_tree;
refresh_tree = refresh_tree;
open_dir = open_dir;
check_dir_access = check_dir_access;
is_dir = is_dir;
set_root_path = set_root_path;
get_cwd = get_cwd;
}

View File

@ -8,8 +8,6 @@ local highlight = libformat.highlight_buffer
local stateutils = require 'lib/state'
local get_tree = stateutils.get_tree
local scratch_buf = nil
local function get_buf()
local regex = '.*'..BUF_NAME..'$';
@ -34,89 +32,6 @@ local function get_win()
return nil
end
local function scratch_buffer()
scratch_buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(scratch_buf, 'bufhidden', 'wipe')
local width = api.nvim_get_option("columns")
local height = api.nvim_get_option("lines")
local win_height = 2
local win_width = 90
local row = math.ceil((height - win_height) / 2 - 1)
local col = math.ceil((width - win_width) / 2)
local opts = {
style = "minimal",
relative = "editor",
width = win_width,
height = win_height,
row = row,
col = col
}
local border_buf = api.nvim_create_buf(false, true)
local border_opts = {
style = "minimal",
relative = "editor",
width = win_width + 2,
height = win_height + 2,
row = row - 1,
col = col - 1
}
local border_lines = { '' .. string.rep('', win_width) .. '' }
local middle_line = '' .. string.rep(' ', win_width) .. ''
for _ = 1, win_height do
table.insert(border_lines, middle_line)
end
table.insert(border_lines, '' .. string.rep('', win_width) .. '')
api.nvim_buf_set_lines(border_buf, 0, -1, false, border_lines)
api.nvim_open_win(border_buf, true, border_opts)
api.nvim_command('setlocal nocursorline winhighlight=Normal:LuaNoEndOfBufferPopup')
api.nvim_open_win(scratch_buf, true, opts)
api.nvim_command('setlocal nocursorline winhighlight=Normal:LuaTreePopup')
api.nvim_command('au BufWipeout <buffer> exe "silent bwipeout! "'..border_buf)
end
local function set_scratch_mappings(edit_type)
local chars = {
'a', 'b', 'd', 'e', 'f', 'h', 'l', 'j', 'q', 'k', 'g', 'i', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
}
local options = { nowait = true, noremap = true, silent = true }
for _,v in ipairs(chars) do
api.nvim_buf_set_keymap(scratch_buf, 'n', v, '', options)
api.nvim_buf_set_keymap(scratch_buf, 'n', v:upper(), '', options)
api.nvim_buf_set_keymap(scratch_buf, 'n', '<c-'..v..'>', '', options)
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-'..v..'>', '', options)
api.nvim_buf_set_keymap(scratch_buf, 'i', '<c-' ..v:upper()..'>', '', options)
end
api.nvim_buf_set_keymap(scratch_buf, 'i', '<CR>', "<esc>:lua require'lib/file'."..edit_type.."_file()<CR>", options)
local ikeys = { '<esc>', '<C-c>', '<C-[' }
for _, map in pairs(ikeys) do
api.nvim_buf_set_keymap(scratch_buf, 'i', map, "<esc>:q!<CR>", options)
end
end
local function update_scratch_view(...)
api.nvim_buf_set_lines(scratch_buf, 0, -1, false, ...)
api.nvim_command('normal G')
api.nvim_command('startinsert!')
end
local function scratch_wrapper(edit_type, ...)
scratch_buffer()
update_scratch_view(...)
set_scratch_mappings(edit_type)
end
local BUF_OPTIONS = {
'nonumber', 'norelativenumber', 'winfixwidth', 'winfixheight',
'winhighlight=EndOfBuffer:LuaTreeEndOfBuffer', 'noswapfile'
@ -183,7 +98,7 @@ local function set_mappings()
['<C-v>'] = 'open_file("vsplit")';
['<C-x>'] = 'open_file("split")';
['<C-[>'] = 'open_file("chdir")';
a = 'edit_file("add")';
a = 'edit_file("create")';
d = 'edit_file("remove")';
r = 'edit_file("rename")';
}
@ -203,5 +118,4 @@ return {
get_buf = get_buf;
get_win = get_win;
set_mappings = set_mappings;
scratch_wrapper = scratch_wrapper;
}

View File

@ -1,19 +1,21 @@
local api = vim.api
local lib_file = require 'lib/file'
local edit_add = lib_file.edit_add
local edit_remove = lib_file.edit_remove
local edit_rename = lib_file.edit_rename
local fs_update = require 'lib/fs_update'
local create_file = fs_update.create_file
local rename_file = fs_update.rename_file
local remove_file = fs_update.remove_file
local stateutils = require 'lib/state'
local get_tree = stateutils.get_tree
local init_tree = stateutils.init_tree
local open_dir = stateutils.open_dir
local check_dir_access = stateutils.check_dir_access
local refresh_tree = stateutils.refresh_tree
local is_dir = stateutils.is_dir
local set_root_path = stateutils.set_root_path
local get_cwd = stateutils.get_cwd
local fs = require 'lib/fs'
local check_dir_access = fs.check_dir_access
local is_dir = fs.is_dir
local get_cwd = fs.get_cwd
local state = require 'lib/state'
local get_tree = state.get_tree
local init_tree = state.init_tree
local open_dir = state.open_dir
local refresh_tree = state.refresh_tree
local set_root_path = state.set_root_path
local winutils = require 'lib/winutils'
local update_view = winutils.update_view
@ -86,6 +88,7 @@ local function open_file(open_type)
end
elseif node.dir == true then
if check_dir_access(node.path .. node.name) == false then return end
open_dir(tree_index)
update_view(true)
@ -99,16 +102,16 @@ local function edit_file(edit_type)
local tree_index = api.nvim_win_get_cursor(0)[1]
local node = tree[tree_index]
if edit_type == 'add' then
if edit_type == 'create' then
if node.dir == true then
edit_add(node.path .. node.name .. '/')
create_file(node.path .. node.name .. '/')
else
edit_add(node.path)
create_file(node.path)
end
elseif edit_type == 'remove' then
edit_remove(node.name, node.path, node.dir)
remove_file(node.name, node.path)
elseif edit_type == 'rename' then
edit_rename(node.name, node.path, node.dir)
rename_file(node.name, node.path)
end
end