-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
helpers.lua
107 lines (86 loc) · 2.59 KB
/
helpers.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
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http://www.gnu.org/licenses/gpl.html
Various helper functions.
]]
local mp = require('mp')
local this = {}
local ass_start = mp.get_property_osd("osd-ass-cc/0")
this.is_wayland = function()
return os.getenv('WAYLAND_DISPLAY') ~= nil
end
this.is_win = function()
return mp.get_property('options/vo-mmcss-profile') ~= nil
end
this.is_mac = function()
return mp.get_property('options/macos-force-dedicated-gpu') ~= nil
end
this.notify = function(message, level, duration)
level = level or 'info'
duration = duration or 1
mp.msg[level](message)
mp.osd_message(ass_start .. "{\\fs12}{\\bord0.75}" .. message, duration)
end
this.notify_error = function(message, level, duration)
this.notify("{\\c&H7171f8&}" .. message, level, duration)
end
this.subprocess = function(args, stdin)
local command_table = {
name = "subprocess",
playback_only = false,
capture_stdout = true,
capture_stderr = true,
args = args,
stdin_data = (stdin or ""),
}
return mp.command_native(command_table)
end
this.subprocess_async = function(args, on_complete)
local command_table = {
name = "subprocess",
playback_only = false,
capture_stdout = true,
capture_stderr = true,
args = args
}
return mp.command_native_async(command_table, on_complete)
end
this.remove_extension = function(filename)
return filename:gsub('%.%w+$', '')
end
this.remove_text_in_brackets = function(str)
return str:gsub('%b[]', '')
end
this.remove_special_characters = function(str)
return str:gsub('[%-_]', ' '):gsub('[%c%p]', ''):gsub('%s+', ' ')
end
this.strip = function(str)
return str:gsub("^%s*(.-)%s*$", "%1")
end
this.human_readable_time = function(seconds)
if type(seconds) ~= 'number' or seconds < 0 then
return 'empty'
end
local parts = {}
parts.h = math.floor(seconds / 3600)
parts.m = math.floor(seconds / 60) % 60
parts.s = math.floor(seconds % 60)
parts.ms = math.floor((seconds * 1000) % 1000)
local ret = string.format("%02dm%02ds%03dms", parts.m, parts.s, parts.ms)
if parts.h > 0 then
ret = string.format('%dh%s', parts.h, ret)
end
return ret
end
this.quote_if_necessary = function(args)
local ret = {}
for _, v in ipairs(args) do
if v:find(" ") then
table.insert(ret, (v:find("'") and string.format('"%s"', v) or string.format("'%s'", v)))
else
table.insert(ret, v)
end
end
return ret
end
return this