Skip to content

Commit 27c6634

Browse files
author
Nicolai Søborg
committed
Add deletion option, bump to version 1.3.0, refactor
1 parent 98449eb commit 27c6634

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

filetree.lua

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "1.2.7"
1+
VERSION = "1.3.0"
22

33
treeView = nil
44
cwd = DirectoryName(".")
@@ -13,7 +13,8 @@ function OpenTree()
1313
SetLocalOption("ruler", "false", treeView)
1414
SetLocalOption("softwrap", "true", treeView)
1515
SetLocalOption("autosave", "false", treeView)
16-
SetLocalOption("statusline", "false", treeView)
16+
SetLocalOption("statusline", "false", treeView)
17+
--SetLocalOption("readonly", "true", treeView)
1718
tabs[curTab+1]:Resize()
1819
RefreshTree()
1920
end
@@ -23,10 +24,15 @@ function RefreshTree()
2324
treeView.Buf:Insert(Loc(0,0), table.concat(scandir(cwd), "\n "))
2425
end
2526

27+
-- returns currently selected line in treeView
28+
function getSelection()
29+
return (treeView.Buf:Line(treeView.Cursor.Loc.Y)):sub(2)
30+
end
31+
2632
-- When user press enter
2733
function preInsertNewline(view)
2834
if view == treeView then
29-
local selected = (view.Buf:Line(view.Cursor.Loc.Y)):sub(2)
35+
local selected = getSelection()
3036
if view.Cursor.Loc.Y == 0 then
3137
return false -- topmost line is cwd, so disallowing selecting it
3238
elseif isDir(selected) then
@@ -44,34 +50,49 @@ function preInsertNewline(view)
4450
return true
4551
end
4652

47-
-- don't use build-in view.Cursor:SelectLine() as it will copy to clipboard
48-
function SelectLine(v)
49-
local y = v.Cursor.Loc.Y
50-
v.Cursor.CurSelection[1] = Loc(0, y)
51-
v.Cursor.CurSelection[2] = Loc(v.Width, y)
52-
end
53-
5453
-- disallow selecting topmost line in treeview:
5554
function preCursorUp(view)
5655
if view == treeView then
5756
if view.Cursor.Loc.Y == 1 then
5857
return false
5958
end end end
6059

61-
-- 'beautiful' file selection:
62-
function onCursorDown(view) if view == treeView then SelectLine(view) end end
63-
function onCursorUp(view) if view == treeView then SelectLine(view) end end
60+
-- don't use build-in view.Cursor:SelectLine() as it will copy to clipboard (in old versions of Micro)
61+
function SelectLineInTree(v)
62+
if v == treeView then
63+
local y = v.Cursor.Loc.Y
64+
v.Cursor.CurSelection[1] = Loc(0, y)
65+
v.Cursor.CurSelection[2] = Loc(v.Width, y)
66+
end
67+
end
6468

69+
-- 'beautiful' file selection:
70+
function onCursorDown(view) SelectLineInTree(view) end
71+
function onCursorUp(view) SelectLineInTree(view) end
6572

66-
--[[ allows for deleting files
73+
-- allows for deleting files
6774
function preDelete(view)
6875
if view == treeView then
69-
messenger:YesNoPrompt("Do you want to delete ...?")
76+
local selected = getSelection()
77+
if selected == ".." then return false end
78+
local question, command
79+
if isDir(selected) then
80+
question = "Do you want to delete dir '"..selected.."' ?"
81+
command = isWin and "del /S /Q" or "rm -r"
82+
else
83+
question = "Do you want to delete file '"..selected.."' ?"
84+
command = isWin and "del" or "rm -I"
85+
end
86+
command = command .. " " .. (isWin and driveLetter or "") .. JoinPaths(cwd, selected)
87+
local yes, cancel = messenger:YesNoPrompt(question)
88+
if not cancel and yes then
89+
io.popen(command .. " " .. selected):close()
90+
RefreshTree()
91+
end
7092
return false
7193
end
7294
return true
7395
end
74-
]]--
7596

7697
-- don't prompt to save tree view
7798
function preQuit(view)
@@ -82,36 +103,34 @@ function preQuit(view)
82103
end
83104

84105
function scandir(directory)
85-
local i, t, popen = 3, {}, io.popen
86-
local pfile
106+
local i, t, proc = 3, {}, nil
87107
t[1] = (isWin and driveLetter or "") .. cwd
88108
t[2] = ".."
89109
if isWin then
90-
pfile = popen('dir /a /b "'..directory..'"')
110+
proc = io.popen('dir /a /b "'..directory..'"')
91111
else
92-
pfile = popen('ls -Ap "'..directory..'"')
112+
proc = io.popen('ls -Ap "'..directory..'"')
93113
end
94-
for filename in pfile:lines() do
114+
for filename in proc:lines() do
95115
t[i] = filename
96116
i = i + 1
97117
end
98-
pfile:close()
118+
proc:close()
99119
return t
100120
end
101121

102122
function isDir(path)
103-
local status = false
104-
local pfile
123+
local dir, proc = false, nil
105124
if isWin then
106-
pfile = io.popen('IF EXIST ' .. driveLetter .. JoinPaths(cwd, path) .. '/* (ECHO d) ELSE (ECHO -)')
125+
proc = io.popen('IF EXIST ' .. driveLetter .. JoinPaths(cwd, path) .. '/* (ECHO d) ELSE (ECHO -)')
107126
else
108-
pfile = io.popen('ls -adl "' .. JoinPaths(cwd, path) .. '"')
127+
proc = io.popen('ls -adl "' .. JoinPaths(cwd, path) .. '"')
109128
end
110-
if pfile:read(1) == "d" then
111-
status = true
129+
if proc:read(1) == "d" then
130+
dir = true
112131
end
113-
pfile:close()
114-
return status
132+
proc:close()
133+
return dir
115134
end
116135

117136
MakeCommand("tree", "filetree.OpenTree", 0)

0 commit comments

Comments
 (0)