♻️ refactor(cache-stats)!: refactor the wakatime and music stats modules
Modules now spawn the scripts automatically at startup and kill them at exit. Both stats are handled by a single script and a lockfile is used to avoid multiple instances spawning. BREAKING CHANGE: Please remove any user services previously used to spawn scripts.
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
-- ╭─────────────────────────────────────────────────────────╮
|
||||||
|
-- │ Cache Stats │
|
||||||
|
-- ╰─────────────────────────────────────────────────────────╯
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local config_dir = vim.fn.stdpath("config")
|
||||||
|
local cache_script = config_dir .. "/scripts/update_cache.sh"
|
||||||
|
|
||||||
|
-- Function to run the cache update script using vim.system
|
||||||
|
function M.setup_script()
|
||||||
|
vim.system({ cache_script }, { detach = true }, function() end)
|
||||||
|
end
|
||||||
|
|
||||||
|
M.setup_script()
|
||||||
|
|
||||||
|
return M
|
||||||
@@ -2,7 +2,10 @@
|
|||||||
-- │ Music Stats │
|
-- │ Music Stats │
|
||||||
-- ╰─────────────────────────────────────────────────────────╯
|
-- ╰─────────────────────────────────────────────────────────╯
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
require("utils.cache_stats")
|
||||||
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
|
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
|
||||||
|
|
||||||
local separator = "␟"
|
local separator = "␟"
|
||||||
|
|
||||||
-- Function to read the cache file
|
-- Function to read the cache file
|
||||||
@@ -58,7 +61,7 @@ function M.get_status()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.get_current()
|
function M.get_current()
|
||||||
return parse_cache() -- Just return title for simplicity
|
return parse_cache()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_icon()
|
function M.get_icon()
|
||||||
@@ -102,6 +105,58 @@ function M.is_loop()
|
|||||||
return loop ~= "None" and loop ~= "false"
|
return loop ~= "None" and loop ~= "false"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Function to remove text inside parentheses, including the parentheses themselves
|
||||||
|
local function remove_parentheses(text)
|
||||||
|
return text:gsub("%b()", "")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to abbreviate common words or phrases
|
||||||
|
local function abbreviate(text)
|
||||||
|
local replacements = {
|
||||||
|
["feat%.?%s"] = "ft. ",
|
||||||
|
["and%s"] = "& ",
|
||||||
|
["versus%s"] = "vs ",
|
||||||
|
["Original Mix%s"] = "Orig. Mix ",
|
||||||
|
}
|
||||||
|
for long, short in pairs(replacements) do
|
||||||
|
text = text:gsub(long, short)
|
||||||
|
end
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to strip unnecessary words or phrases
|
||||||
|
local function strip_redundant(text)
|
||||||
|
local redundant_phrases =
|
||||||
|
{ "Radio Edit", "Extended Version", "Remix", "Instrumental", "Live" }
|
||||||
|
for _, phrase in ipairs(redundant_phrases) do
|
||||||
|
text = text:gsub(phrase, "")
|
||||||
|
end
|
||||||
|
return text:gsub("%s+", " "):gsub("^%s*(.-)%s*$", "%1") -- Clean up any extra spaces
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to limit the number of words in the text
|
||||||
|
local function limit_words(text, max_words)
|
||||||
|
local words = vim.split(text, "%s+")
|
||||||
|
if #words > max_words then
|
||||||
|
return table.concat(vim.list_slice(words, 1, max_words), " ") .. "..."
|
||||||
|
end
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to remove duplicate words
|
||||||
|
local function remove_duplicates(text)
|
||||||
|
local seen = {}
|
||||||
|
local result = {}
|
||||||
|
for word in text:gmatch("%S+") do
|
||||||
|
if not seen[word:lower()] then
|
||||||
|
table.insert(result, word)
|
||||||
|
seen[word:lower()] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.concat(result, " ")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to shorten text based on delimiters
|
||||||
local function shorten_text(text)
|
local function shorten_text(text)
|
||||||
if #text <= 25 then
|
if #text <= 25 then
|
||||||
return text
|
return text
|
||||||
@@ -124,6 +179,17 @@ local function shorten_text(text)
|
|||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Combine all simplification functions
|
||||||
|
local function simplify_text(text)
|
||||||
|
text = remove_parentheses(text)
|
||||||
|
text = strip_redundant(text)
|
||||||
|
text = abbreviate(text)
|
||||||
|
text = shorten_text(text) -- Apply shortening based on delimiters
|
||||||
|
text = limit_words(text, 7) -- Adjust max words as needed
|
||||||
|
text = remove_duplicates(text)
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
local function format_icon_with_text(icon, artist, title)
|
local function format_icon_with_text(icon, artist, title)
|
||||||
if artist and #artist > 25 then
|
if artist and #artist > 25 then
|
||||||
return icon .. " " .. title
|
return icon .. " " .. title
|
||||||
@@ -140,14 +206,15 @@ function M.get_icon_with_text()
|
|||||||
local icon = M.get_icon()
|
local icon = M.get_icon()
|
||||||
local title = M.get_title()
|
local title = M.get_title()
|
||||||
local artist = M.get_artist()
|
local artist = M.get_artist()
|
||||||
local status = M.get_status() -- Assuming there's a function to get the music status
|
local status = M.get_status()
|
||||||
|
|
||||||
if status ~= "playing" and status ~= "paused" then
|
if status ~= "playing" and status ~= "paused" then
|
||||||
return "" -- Return empty string if the status is neither "Playing" nor "Paused"
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
title = shorten_text(title)
|
-- Apply the simplification methods
|
||||||
artist = shorten_text(artist)
|
title = simplify_text(title)
|
||||||
|
artist = simplify_text(artist)
|
||||||
|
|
||||||
return format_icon_with_text(icon, artist, title)
|
return format_icon_with_text(icon, artist, title)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
-- │ WakaTime Stats │
|
-- │ WakaTime Stats │
|
||||||
-- ╰─────────────────────────────────────────────────────────╯
|
-- ╰─────────────────────────────────────────────────────────╯
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
require("utils.cache_stats")
|
||||||
local cache_file = os.getenv("HOME") .. "/.cache/wakatime_cache.txt"
|
local cache_file = os.getenv("HOME") .. "/.cache/wakatime_cache.txt"
|
||||||
|
|
||||||
local function read_cache()
|
local function read_cache()
|
||||||
|
|||||||
Executable
+37
@@ -0,0 +1,37 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Separator variable
|
||||||
|
separator="␟"
|
||||||
|
|
||||||
|
# Lock file path
|
||||||
|
lock_file="/tmp/music_wakatime_update.lock"
|
||||||
|
|
||||||
|
# Create a lock file to ensure only one instance of the script runs
|
||||||
|
if [[ -e $lock_file ]]; then
|
||||||
|
echo "Another instance of this script is already running."
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
# Create the lock file
|
||||||
|
touch "$lock_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Function to clean up lock file on exit
|
||||||
|
cleanup() {
|
||||||
|
rm -f "$lock_file"
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
# Update the music cache file every second
|
||||||
|
while true; do
|
||||||
|
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
|
||||||
|
done & # Run this loop in the background
|
||||||
|
|
||||||
|
# Update the Wakatime cache file every 60 seconds
|
||||||
|
while true; do
|
||||||
|
~/.wakatime/wakatime-cli --today >~/.cache/wakatime_cache.txt 2>/dev/null
|
||||||
|
sleep 60
|
||||||
|
done & # Run this loop in the background
|
||||||
|
|
||||||
|
# Wait for all background processes to finish
|
||||||
|
wait
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# Separator variable
|
|
||||||
separator="␟"
|
|
||||||
|
|
||||||
# Update the cache file every second
|
|
||||||
while true; do
|
|
||||||
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
|
|
||||||
done
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Update the cache file with the Wakatime CLI command
|
|
||||||
while true; do
|
|
||||||
~/.wakatime/wakatime-cli --today >~/.cache/wakatime_cache.txt 2>/dev/null
|
|
||||||
sleep 60
|
|
||||||
done
|
|
||||||
Reference in New Issue
Block a user