✨ feat(music-stats): improve stats format and add additional metadata
Improves the shortening function. Adds loop, shuffle, and volume metadata. Uses better seperator characters to improve reliability.
This commit is contained in:
+72
-18
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
|
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
|
||||||
|
local separator = "␟"
|
||||||
|
|
||||||
-- Function to read the cache file
|
-- Function to read the cache file
|
||||||
local function read_cache()
|
local function read_cache()
|
||||||
@@ -20,11 +21,30 @@ end
|
|||||||
local function parse_cache()
|
local function parse_cache()
|
||||||
local content = read_cache()
|
local content = read_cache()
|
||||||
if not content or content:match("No players found") then
|
if not content or content:match("No players found") then
|
||||||
return "", "", "", ""
|
return "", "", "", "", "", "", ""
|
||||||
end
|
end
|
||||||
local artist, title, album, status =
|
local pattern = "^(.-)%s*"
|
||||||
content:match("^(.-)%s*-%s*(.-)%s*-%s*(.-)%s*-%s*(.-)%s*$")
|
.. separator
|
||||||
return artist or "", title or "", album or "", status or ""
|
.. "%s*(.-)%s*"
|
||||||
|
.. separator
|
||||||
|
.. "%s*(.-)%s*"
|
||||||
|
.. separator
|
||||||
|
.. "%s*(.-)%s*"
|
||||||
|
.. separator
|
||||||
|
.. "%s*(.-)%s*"
|
||||||
|
.. separator
|
||||||
|
.. "%s*(.-)%s*"
|
||||||
|
.. separator
|
||||||
|
.. "%s*(.-)%s*$"
|
||||||
|
local artist, title, album, status, volume, loop, shuffle =
|
||||||
|
content:match(pattern)
|
||||||
|
return artist or "",
|
||||||
|
title or "",
|
||||||
|
album or "",
|
||||||
|
status or "",
|
||||||
|
volume or "",
|
||||||
|
loop or "",
|
||||||
|
shuffle or ""
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_status()
|
function M.get_status()
|
||||||
@@ -34,7 +54,7 @@ function M.get_status()
|
|||||||
elseif status:match("Paused") then
|
elseif status:match("Paused") then
|
||||||
return "paused"
|
return "paused"
|
||||||
else
|
else
|
||||||
return "ptopped"
|
return "stopped"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -68,14 +88,53 @@ function M.get_album()
|
|||||||
return album
|
return album
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.get_volume()
|
||||||
|
local _, _, _, _, volume = parse_cache()
|
||||||
|
return tonumber(volume) or 0.0
|
||||||
|
end
|
||||||
|
|
||||||
function M.is_shuffle()
|
function M.is_shuffle()
|
||||||
-- Add shuffle property parsing if needed
|
local _, _, _, _, _, _, shuffle = parse_cache()
|
||||||
return false
|
return shuffle == "On"
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_loop()
|
function M.is_loop()
|
||||||
-- Add loop property parsing if needed
|
local _, _, _, _, _, loop = parse_cache()
|
||||||
return false
|
return loop ~= "None" and loop ~= "false"
|
||||||
|
end
|
||||||
|
|
||||||
|
local function shorten_text(text)
|
||||||
|
if #text <= 25 then
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
local delimiters = { ",", "-", ":" }
|
||||||
|
for _, delimiter in ipairs(delimiters) do
|
||||||
|
local parts = vim.split(text, delimiter)
|
||||||
|
if #parts > 1 then
|
||||||
|
local first_part = parts[1]
|
||||||
|
local second_part = table.concat(vim.list_slice(parts, 2), delimiter)
|
||||||
|
if #first_part > #second_part then
|
||||||
|
return second_part
|
||||||
|
else
|
||||||
|
return first_part
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
local function format_icon_with_text(icon, artist, title)
|
||||||
|
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
|
||||||
|
|
||||||
function M.get_icon_with_text()
|
function M.get_icon_with_text()
|
||||||
@@ -88,15 +147,10 @@ function M.get_icon_with_text()
|
|||||||
return "" -- Return empty string if the status is neither "Playing" nor "Paused"
|
return "" -- Return empty string if the status is neither "Playing" nor "Paused"
|
||||||
end
|
end
|
||||||
|
|
||||||
if artist and #artist > 25 then
|
title = shorten_text(title)
|
||||||
return icon .. " " .. title
|
artist = shorten_text(artist)
|
||||||
elseif title and #title > 25 then
|
|
||||||
return icon .. " " .. title
|
return format_icon_with_text(icon, artist, 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
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
# Separator variable
|
||||||
|
separator="␟"
|
||||||
|
|
||||||
# Update the cache file every second
|
# Update the cache file every second
|
||||||
while true; do
|
while true; do
|
||||||
playerctl --ignore-player firefox,kdeconnect,plasma-browser-integration metadata --format '{{ artist }} - {{ title }} - {{ album }} - {{ status }}' >~/.cache/music_cache.txt
|
playerctl --ignore-player firefox,kdeconnect,haruna,plasma-browser-integration metadata --format "{{ artist }}${separator}{{ title }}${separator}{{ album }}${separator}{{ status }}${separator}{{ volume }}${separator}{{ loop }}${separator}{{ shuffle }}" >~/.cache/music_cache.txt
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user