Repository navigation
Expand file tree
/
Copy pathext.lua
More file actions
74 lines (68 loc) · 1.74 KB
/
Copy pathext.lua
File metadata and controls
74 lines (68 loc) · 1.74 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
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if not raw then
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
end
return s
end
-- color
dofile('module/ext/color.lua')
-- table.inspect
dofile('module/ext/inspect.lua')
function table.copy (t) -- shallow-copy a table
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do target[k] = v end
setmetatable(target, meta)
return target
end
function table.clone (t) -- deep-copy a table
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
for k, v in pairs(t) do
if type(v) == "table" then
target[k] = clone(v)
else
target[k] = v
end
end
setmetatable(target, meta)
return target
end
local function explode(delimiter, value)
local t = {}
local ll = 0
if (#string == 1) then
return { string }
end
while true do
l = string.find(value, delimiter, ll, true) -- find the next d in the string
if l ~= nil then -- if "not not" found then..
table.insert(t, string.sub(value, ll, l-1)) -- Save it in our array.
ll = l + 1 -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(value, ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end
function table.inject(obj, key, value)
local keys = explode('.', key)
local last = #keys
for i = 1, last do
local key = keys[i]
if i == last then
obj[key] = value
elseif not obj[key] then
obj[key] = {}
end
obj = obj[key]
end
end