Repository navigation
Expand file tree
/
Copy pathhelpers.lua
More file actions
90 lines (75 loc) · 1.96 KB
/
Copy pathhelpers.lua
File metadata and controls
90 lines (75 loc) · 1.96 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
local mp = require('mp')
local utils = require('mp.utils')
local Helpers = {}
function Helpers:exp(path)
return mp.command_native({"expand-path", path})
end
function Helpers:path_sep()
return package.config:sub(1, 1)
end
function Helpers:on_windows()
return Helpers:path_sep() == "\\"
end
function Helpers:shallow_copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function Helpers:index_of(array, key)
for i, key2 in ipairs(array) do
if key2 == key then
return i
end
end
end
function Helpers:cycle(current, max, step, wrap)
current = current + step
if current < 1 then
return wrap and max or 1
elseif current > max then
return wrap and 1 or max
else
return current
end
end
function Helpers:snake_case(name)
return name:gsub(" ", "_"):lower()
end
function Helpers:path_exists(path)
local f = io.open(path, "r")
if f == nil then return false end
io.close(f)
return true
end
function Helpers:read_json(path)
local f = assert(io.open(Helpers:exp(path), "r"))
local data = f:read("*all")
f:close()
return assert(utils.parse_json(data))
end
function Helpers:mkdir(path)
if Helpers:path_exists(path) then return end
local args = {"mkdir", "-p", path}
if Helpers:on_windows() then
local path2, _count = path:gsub("/", "\\")
args = {"cmd", "/c", "mkdir", path2}
end
local result, error = mp.command_native({
name = "subprocess", playback_only = false, args = args,
})
if error then print("Error creating folder", path, "-", error) end
end
function Helpers:write_file(path, data)
path = Helpers:exp(path)
local dir, _fname = utils.split_path(path)
Helpers:mkdir(dir)
local f = assert(io.open(path, "w"))
f:write(data)
f:close()
end
function Helpers:write_json(path, table)
Helpers:write_file(path, assert(utils.format_json(table)))
end
return Helpers