about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2024-10-01T19·05+0200
committerProfpatsch <mail@profpatsch.de>2024-10-05T13·49+0000
commit92ad57febe5247816d8fadc1b29d0772536d4a67 (patch)
treeb57d343459c53c3cad371ac3dba68c8baaaa7afe
parentcf68a34b0d882374a02aa418eccae4f588483094 (diff)
fix(users/Profpatsch/lyric): create new lyric file if not exists r/8768
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
-rw-r--r--users/Profpatsch/lyric/lyric-timing-mpv-script.lua30
1 files changed, 15 insertions, 15 deletions
diff --git a/users/Profpatsch/lyric/lyric-timing-mpv-script.lua b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
index db28c0a24f95..dd74d334414c 100644
--- a/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
+++ b/users/Profpatsch/lyric/lyric-timing-mpv-script.lua
@@ -36,22 +36,22 @@ function write_timestamp_to_lrc()
     -- [au: Written by Kal Mann / Dave Appell, 1961]
     -- [length: 2:23]
     local file = io.open(lrc_filename, "r+")
-    if file then
-        -- read the file and check whether it only contains whitespace
-        local content = file:read("*all")
-        if content:match("^%s*$") then
-            file:write("[ar: " .. artist .. "]\n")
-            file:write("[al: " .. album .. "]\n")
-            file:write("[ti: " .. title .. "]\n")
-            local duration = mp.get_property_number("duration", 0)
-            local formatted_duration = string.format("%02d:%02d", math.floor(duration / 60), duration % 60)
-            file:write("[length: " .. formatted_duration .. "]\n")
-            file:write("\n")
-        end
-        file:close()
-    else
-        mp.msg.error("Failed to open " .. lrc_filename)
+    if not file then
+        file = io.open(lrc_filename, "w+")
+    end
+
+    -- read the file and check whether it only contains whitespace
+    local content = file:read("*all")
+    if content:match("^%s*$") then
+        file:write("[ar: " .. artist .. "]\n")
+        file:write("[al: " .. album .. "]\n")
+        file:write("[ti: " .. title .. "]\n")
+        local duration = mp.get_property_number("duration", 0)
+        local formatted_duration = string.format("%02d:%02d", math.floor(duration / 60), duration % 60)
+        file:write("[length: " .. formatted_duration .. "]\n")
+        file:write("\n")
     end
+    file:close()
 
     -- Append the timestamp to the LRC file
     local file = io.open(lrc_filename, "a")