From 83ab1b000a92286e4c033653f25c6ca270393bcc Mon Sep 17 00:00:00 2001
From: sinanmohd <sinan@sinanmohd.com>
Date: Mon, 2 Oct 2023 10:21:56 +0530
Subject: lib/util: init run

---
 lib/util.lua | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/lib/util.lua b/lib/util.lua
index f3632f2..8ea7699 100644
--- a/lib/util.lua
+++ b/lib/util.lua
@@ -122,6 +122,40 @@ local opensubtitles_hash = function (fileName)
         return string.format("%08x%08x", hi,lo), size
 end
 
+local table_to_cmd = function (t)
+	local str = ""
+
+	for _, v in ipairs(t) do
+		v = v:gsub("'", "\'")
+		str = str .. " '" .. v .. "' "
+	end
+
+	return str
+end
+
+local run = function (args)
+	local sig, rc, stdout, cmd
+
+	if mp then
+		cmd = mp.command_native({
+			name = 'subprocess',
+			capture_stdout = true,
+			args = args,
+		})
+		stdout = cmd.stdout
+		rc = (cmd.status >= 0)
+	else
+		cmd = io.popen(table_to_cmd(args))
+		if cmd then
+			stdout = cmd:read('*all')
+			_, sig, rc = cmd:close()
+			rc = (sig == 'signal') or (sig == 'exit' and rc == 0)
+		end
+	end
+
+	return stdout, rc
+end
+
 return {
 	table_merge = table_merge,
 	table_print = table_print,
@@ -129,5 +163,6 @@ return {
 	zip_ext_first = zip_ext_first,
 	string_vid_path_to_name = string_vid_path_to_name,
 	opensubtitles_hash = opensubtitles_hash,
+	run = run,
 	error = error,
 }
-- 
cgit v1.2.3