--[[ 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 = {} 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(message, 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.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