⚡️ perf(music-stats): use script to cache music stats
Refactor music-stats lualine component to use an external script to cache the metadata
This commit is contained in:
+66
-95
@@ -1,131 +1,102 @@
|
|||||||
-- -----------------------------------------------------------------------------
|
-- -----------------------------------------------------------------------------
|
||||||
-- ------------------------------ MUSIC-STATS ----------------------------------
|
-- ------------------------------- MUSIC-STATS ---------------------------------
|
||||||
-- -----------------------------------------------------------------------------
|
-- -----------------------------------------------------------------------------
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
local cache = {
|
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
|
||||||
text = "", -- Default to empty string to handle initial state
|
|
||||||
timestamp = 0,
|
|
||||||
}
|
|
||||||
local cache_duration = 1 -- Cache duration in seconds
|
|
||||||
|
|
||||||
-- List of players to ignore
|
-- Function to read the cache file
|
||||||
local ignored_players = {
|
local function read_cache()
|
||||||
"firefox",
|
local file = io.open(cache_file, "r")
|
||||||
"kdeconnect",
|
if file then
|
||||||
"plasma-browser-integration",
|
local content = file:read("*a")
|
||||||
}
|
file:close()
|
||||||
|
return content
|
||||||
local function get_ignored_players_string()
|
end
|
||||||
return table.concat(ignored_players, ",")
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_music_current()
|
-- Parse the cached music data
|
||||||
local ignored_players_str = get_ignored_players_string()
|
local function parse_cache()
|
||||||
local cmd = string.format(
|
local content = read_cache()
|
||||||
"playerctl --ignore-player %s metadata --format '{{ artist }} - {{ title }}' 2>/dev/null",
|
if not content or content:match("No players found") then
|
||||||
ignored_players_str
|
return "", "", "", ""
|
||||||
)
|
|
||||||
|
|
||||||
local result = vim.call("system", cmd)
|
|
||||||
|
|
||||||
if not result or result:match("No players found") then
|
|
||||||
return "" -- Return empty string for no music
|
|
||||||
end
|
end
|
||||||
|
local artist, title, album, status =
|
||||||
local artist_title = result:match("^(.-)%s*$") -- Trim any whitespace
|
content:match("^(.-)%s*-%s*(.-)%s*-%s*(.-)%s*-%s*(.-)%s*$")
|
||||||
return artist_title or "" -- Return empty string if no artist/title
|
return artist or "", title or "", album or "", status or ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_music_icon()
|
function M.get_status()
|
||||||
local ignored_players_str = get_ignored_players_string()
|
local _, _, _, status = parse_cache()
|
||||||
local cmd = string.format(
|
if status:match("Playing") then
|
||||||
"playerctl --ignore-player %s status 2>/dev/null",
|
return "playing"
|
||||||
ignored_players_str
|
elseif status:match("Paused") then
|
||||||
)
|
return "paused"
|
||||||
|
|
||||||
local result = vim.call("system", cmd)
|
|
||||||
|
|
||||||
if not result then
|
|
||||||
return " " -- Default icon for music stopped
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Determine the icon based on the player status
|
|
||||||
if result:match("Playing") then
|
|
||||||
return " " -- Icon for music playing
|
|
||||||
elseif result:match("Paused") then
|
|
||||||
return " " -- Icon for music paused
|
|
||||||
else
|
else
|
||||||
return " " -- Default icon for music stopped
|
return "ptopped"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_music_metadata(field)
|
|
||||||
local ignored_players_str = get_ignored_players_string()
|
|
||||||
local cmd = string.format(
|
|
||||||
"playerctl --ignore-player %s metadata --format '{{ %s }}' 2>/dev/null",
|
|
||||||
ignored_players_str,
|
|
||||||
field
|
|
||||||
)
|
|
||||||
|
|
||||||
local result = vim.call("system", cmd)
|
|
||||||
|
|
||||||
if not result or result:match("No players found") then
|
|
||||||
return "" -- Return empty string for no music or missing metadata
|
|
||||||
end
|
|
||||||
|
|
||||||
local metadata = result:match("^(.-)%s*$") -- Trim any whitespace
|
|
||||||
return metadata or "" -- Return empty string if no metadata
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_player_property(property)
|
|
||||||
local ignored_players_str = get_ignored_players_string()
|
|
||||||
local cmd = string.format(
|
|
||||||
"playerctl --ignore-player %s %s 2>/dev/null",
|
|
||||||
ignored_players_str,
|
|
||||||
property
|
|
||||||
)
|
|
||||||
|
|
||||||
local result = vim.call("system", cmd)
|
|
||||||
|
|
||||||
if not result then
|
|
||||||
return nil -- Return nil for no property value
|
|
||||||
end
|
|
||||||
|
|
||||||
return result:match("^(.-)%s*$") -- Trim any whitespace
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.get_current()
|
function M.get_current()
|
||||||
local current_time = os.time()
|
return parse_cache() -- Just return title for simplicity
|
||||||
if current_time - cache.timestamp > cache_duration then
|
|
||||||
cache.text = get_music_current()
|
|
||||||
cache.timestamp = current_time
|
|
||||||
end
|
|
||||||
return cache.text
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_icon()
|
function M.get_icon()
|
||||||
return get_music_icon()
|
local _, _, _, status = parse_cache()
|
||||||
|
if status == "Playing" then
|
||||||
|
return " "
|
||||||
|
elseif status == "Paused" then
|
||||||
|
return " "
|
||||||
|
else
|
||||||
|
return " "
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_title()
|
function M.get_title()
|
||||||
return get_music_metadata("title")
|
local _, title = parse_cache()
|
||||||
|
return title
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_artist()
|
function M.get_artist()
|
||||||
return get_music_metadata("artist")
|
local artist = parse_cache()
|
||||||
|
return artist
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_album()
|
function M.get_album()
|
||||||
return get_music_metadata("album")
|
local _, _, album = parse_cache()
|
||||||
|
return album
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_shuffle()
|
function M.is_shuffle()
|
||||||
return get_player_property("shuffle") == "on"
|
-- Add shuffle property parsing if needed
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_loop()
|
function M.is_loop()
|
||||||
return get_player_property("loop") == "true"
|
-- Add loop property parsing if needed
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.get_icon_with_text()
|
||||||
|
local icon = M.get_icon()
|
||||||
|
local title = M.get_title()
|
||||||
|
local artist = M.get_artist()
|
||||||
|
local status = M.get_status() -- Assuming there's a function to get the music status
|
||||||
|
|
||||||
|
if status ~= "playing" and status ~= "paused" then
|
||||||
|
return "" -- Return empty string if the status is neither "Playing" nor "Paused"
|
||||||
|
end
|
||||||
|
|
||||||
|
if artist and #artist > 25 then
|
||||||
|
return icon .. " " .. title
|
||||||
|
elseif title and #title > 25 then
|
||||||
|
return icon .. " " .. title
|
||||||
|
elseif title and artist and #title <= 25 then
|
||||||
|
return icon .. " " .. artist .. " - " .. title
|
||||||
|
else
|
||||||
|
return icon .. " " .. (title or artist or "No music")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Executable
+6
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Update the cache file every second
|
||||||
|
while true; do
|
||||||
|
playerctl --ignore-player firefox,kdeconnect,plasma-browser-integration metadata --format '{{ artist }} - {{ title }} - {{ album }} - {{ status }}' >~/.cache/music_cache.txt
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
Reference in New Issue
Block a user