-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushd.lua
More file actions
95 lines (80 loc) · 1.94 KB
/
pushd.lua
File metadata and controls
95 lines (80 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
VERSION = "0.0.2"
local micro = import("micro")
local config = import("micro/config")
local os = import("os")
local function getCurrentDir()
local wd, err = os.Getwd()
if err ~= nil then
micro.InfoBar:Error("Getwd failed")
return nil
else
return wd
end
end
local function setCurrentDir(bp, path)
bp:CdCmd({ path })
micro.InfoBar():Message("Changed directory to " .. path)
end
local dirStack = {}
function Pushd(bp, args)
if #args == 0 then
if #dirStack == 0 then
micro.InfoBar():Error("Directory stack is empty")
return
else
local dir = dirStack[1]
dirStack[1] = getCurrentDir()
setCurrentDir(bp, dir)
return
end
end
if #args == 1 then
table.insert(dirStack, 1, getCurrentDir())
setCurrentDir(bp, args[1])
return
end
micro.InfoBar():Error("Too many arguments")
end
function Popd(bp)
if #dirStack == 0 then
micro.InfoBar():Error("Directory stack is empty")
return
end
setCurrentDir(bp, table.remove(dirStack, 1))
end
function Dirs(bp, args)
if #args == 0 then
local buf = {}
table.insert(buf, 0 .. " " .. getCurrentDir())
for i, v in ipairs(dirStack) do
table.insert(buf, i .. " " .. v)
end
micro.TermMessage(table.concat(buf, "\n"))
return
end
local match = args[1]
for i, v in ipairs(dirStack) do
if v == match then
table.remove(dirStack, i)
table.insert(dirStack, 1, getCurrentDir())
setCurrentDir(bp, v)
return
end
end
micro.InfoBar():Error("No such directory in stack: " .. match)
end
function DirsComplete(bp)
local completions = {}
local descriptions = {}
for i, dir in ipairs(dirStack) do
table.insert(completions, dir)
table.insert(descriptions, "No. " .. i .. " directory in stack")
end
return completions, descriptions
end
function init()
config.MakeCommand("pushd", Pushd, config.FileComplete)
config.MakeCommand("popd", Popd, config.NoComplete)
config.MakeCommand("dirs", Dirs, DirsComplete)
config.AddRuntimeFile("pushd", config.RTHelp, "help/pushd.md")
end