fix(users/Profpatsch/lyric): create new lyric file if not exists

That was the original intention, but I didn’t understand that lua
would fail with "r+" if the file does not exist (and "w+" truncates
the file, so you have to try "r+" and then fall back to "w+" which
will create the file as well.)

Change-Id: Ib238f0b73ab403ceeaf035d053a14eba718d1b48
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12562
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
This commit is contained in:
Profpatsch 2024-10-01 21:05:36 +02:00
parent cf68a34b0d
commit 92ad57febe

View file

@ -36,7 +36,10 @@ function write_timestamp_to_lrc()
-- [au: Written by Kal Mann / Dave Appell, 1961] -- [au: Written by Kal Mann / Dave Appell, 1961]
-- [length: 2:23] -- [length: 2:23]
local file = io.open(lrc_filename, "r+") local file = io.open(lrc_filename, "r+")
if file then if not file then
file = io.open(lrc_filename, "w+")
end
-- read the file and check whether it only contains whitespace -- read the file and check whether it only contains whitespace
local content = file:read("*all") local content = file:read("*all")
if content:match("^%s*$") then if content:match("^%s*$") then
@ -49,9 +52,6 @@ function write_timestamp_to_lrc()
file:write("\n") file:write("\n")
end end
file:close() file:close()
else
mp.msg.error("Failed to open " .. lrc_filename)
end
-- Append the timestamp to the LRC file -- Append the timestamp to the LRC file
local file = io.open(lrc_filename, "a") local file = io.open(lrc_filename, "a")