I have two Lua functions, getSongLyrics
and getSongTitle
, whose outputs are displayed in a conky window.
getSongLyrics
uses a python script that runs sptlrx pipe
and outputs it into a FIFO file, which is read by the function and then displayed in conky using conky_displaySongLyrics
.
getSongTitle
uses playerctl
to get the title and artist of the current song playing, which is then displayed directly to the conky window using conky_displaySongTitle
.
The problem happens when a new song plays or is changed, the output display for getSongTitle
in the conky wndow only gets updated whenever getSongLyrics
is updated (i.e. a new lyric line). This problem is further shown whenever a song doesn’t have any lyrics available, thus conky_displaySongLyrics
never gets updated, which causes conky_displaySongTitle
to never get updated from the previous song, until a song with lyrics plays.
I’m assuming that it’s because of the FIFO file waiting for a new line from sptlrx pipe
that causes this update problem, but I’m not quite sure how to make it so that conky updates getSongTitle
independent of getSongLyrics
.
How do I make it so that these two functions are ran independently from each other, so that conky_displaySongLyrics
does not affect conky_displaySongTitle
, and vise versa?
scripts.lua
-- read file and print contents of file
function readPrintFile(file)
local f = io.open(file, "rb")
-- if file doesn't exist, return nil
if not f then return nil end
local content = f:read("*a")
f:close()
return content
end
-- get song title from a music source
function getSongTitle()
local command = io.popen("playerctl metadata --player spotify --format '{{ artist }} - {{ title }}'", "r")
local title = command:read("*l")
command:close()
return format:gsub("\n", "")
end
-- get song lyrics from a music source
function getSongLyrics()
local lyrics = readPrintFile("/tmp/sptlrx_lyrics") or ""
return lyrics:gsub("\n", "")
end
function conky_displaySongTitle()
return getSongTitle()
end
function conky_displaySongLyrics()
return getSongLyrics()
end
sptlrx_lyrics.py
import os
import subprocess
pipe_path_lyrics = "/tmp/sptlrx_lyrics"
try:
os.mkfifo(pipe_path_lyrics)
except FileExistsError:
pass
process_lyrics = subprocess.Popen(["sptlrx", "pipe"], stdout=subprocess.PIPE)
for line_lyrics in iter(process_lyrics.stdout.readline, ""):
with open(pipe_path_lyrics, "wb") as lyrics:
lyrics.write(line_lyrics)
conky.conf
conky.config = {
lua_load = "$HOME/.config/conky/scripts.lua",
...
}
conky.text = [[
${if_running spotify}\
${alignc} ${lua displaySongTitle}
${alignc} ${lua displaySongLyrics}\
...
${endif}
]]