-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
223 lines (190 loc) · 4.35 KB
/
init.lua
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
local path = {}
local is = {}
local can = {}
local list = {}
local get = {}
path.is = is
path.can = can
path.list = list
path.get = get
path.seperator = "/"
-- Template
--[[
NAME
ALIAS
DESCRIPTION
ERROR
EXAMPLE
]]
function path.is.string(filepath)
if type(filepath)~="string" then return false end
end
function path.append(filepath, file)
if string.sub(filepath, -1) == "/"
then
return filepath..file
end
return filepath..path.seperator..file
end
function path.exists(filepath)
return os.execute('test -e '..filepath)
end
function path.is.dir(filepath)
return os.execute('test -d '..filepath)
end
function path.is.folder(filepath)
return os.execute('test -d '..filepath)
end
function path.is.file(filepath)
return os.execute('test -f '..filepath)
end
function path.is.symlink(filepath)
return os.execute('test -L '..filepath)
end
function path.is.pipe(filepath)
return os.execute('test -p '..filepath)
end
function path.is.socket(filepath)
return os.execute('test -S '..filepath)
end
function path.is.character(filepath)
return os.execute('test -c '..filepath)
end
function path.is.block(filepath)
return os.execute('test -b '..filepath)
end
function path.can.read(filepath)
return os.execute('test -r '..filepath)
end
function path.can.write(filepath)
return os.execute('test -w '..filepath)
end
function path.can.run(filepath)
return os.execute('test -x '..filepath)
end
function path.can.exec(filepath)
return os.execute('test -x '..filepath)
end
-- Will return 3 items, works also on windows
-- folder, filename, extention
-- spilt("/tmp/test.lua.txt")
-- -> /tmp/ test.lua.txt txt
function path.split(filepath)
local d, n, x = string.match(filepath, "(.-)([^\\/]-%.?([^%.\\/]*))$")
if string.sub(d, -1) == "/"
then
d = string.sub(d, 0, string.len(d)-1)
end
return d, n, x
end
function path.get.dirname(filepath)
local str = path.get.abs(filepath)
local parts = path.list.split(str)
return parts[#parts]
end
function path.get.abs(filepath)
local p = path.realpath(filepath)
local result
if path.is.file(p)
then
result, _, _ = path.split(p)
elseif path.is.dir(p)
then
result = p
end
return result
end
function path.get.ext(filepath)
local result
if path.is.file(filepath)
then
_, _, result = path.split(filepath)
end
return result
end
function path.get.filename(filepath)
local result
if path.is.file(filepath)
then
_, result, _ = path.split(filepath)
end
return result
end
function path.list.all(filepath)
local p = io.popen('ls -A '..filepath)
local file_pairs = {}
for file in p:lines() do
local p = path.append(filepath, file)
file_pairs[#file_pairs + 1] = p
end
return file_pairs
end
function path.list.files(filepath)
local p = io.popen('ls -A '..filepath)
local file_pairs = {}
for file in p:lines() do
local p = path.append(filepath, file)
if path.is.file(p) then
file_pairs[#file_pairs + 1] = p
end
end
return file_pairs
end
function path.list.folders(filepath)
local p = io.popen('ls -A '..filepath)
local file_pairs = {}
for file in p:lines() do
local p = path.append(filepath, file)
if path.is.dir(p)
then
file_pairs[#file_pairs + 1] = p
end
end
return file_pairs
end
function path.list.dirs(filepath)
return path.list.folders(filepath)
end
function path.list.by_ext(filepath, ext)
local p = io.popen('ls -A '..filepath..'/*.'..ext)
local file_pairs = {}
for file in p:lines() do
file_pairs[#file_pairs + 1] = file
end
return file_pairs
end
function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t, cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
--[[
parts = split_path("/usr/local/bin")
--> {'usr','local','bin'}
]]
function path.list.split(filepath)
return split(filepath,'[\\/]+')
end
--[[
Print the resolved absolute file name; all but the last component must exist
realpath: [filepath]: No such file or directory ]]
function path.realpath(filepath)
local handle = io.popen('realpath -ez '..filepath)
local result = handle:read("*a")
handle:close()
return result
end
return path