♻️ refactor(utils): move stats modules to utils

Move wakatime_stats and music_stats to new utils dir
This commit is contained in:
2024-08-07 08:20:30 -04:00
parent 25352b81a0
commit e1e59bfea5
2 changed files with 13 additions and 0 deletions
+155
View File
@@ -0,0 +1,155 @@
-- ╭─────────────────────────────────────────────────────────╮
-- │ Music Stats │
-- ╰─────────────────────────────────────────────────────────╯
local M = {}
local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt"
local separator = ""
-- Function to read the cache file
local function read_cache()
local file = io.open(cache_file, "r")
if file then
local content = file:read("*a")
file:close()
return content
end
return ""
end
-- Parse the cached music data
local function parse_cache()
local content = read_cache()
if not content or content:match("No players found") then
return "", "", "", "", "", "", ""
end
local pattern = "^(.-)%s*"
.. separator
.. "%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
function M.get_status()
local _, _, _, status = parse_cache()
if status:match("Playing") then
return "playing"
elseif status:match("Paused") then
return "paused"
else
return "stopped"
end
end
function M.get_current()
return parse_cache() -- Just return title for simplicity
end
function M.get_icon()
local _, _, _, status = parse_cache()
if status == "Playing" then
return "󰝚 "
elseif status == "Paused" then
return "󰝛 "
else
return "󰓃 "
end
end
function M.get_title()
local _, title = parse_cache()
return title
end
function M.get_artist()
local artist = parse_cache()
return artist
end
function M.get_album()
local _, _, album = parse_cache()
return album
end
function M.get_volume()
local _, _, _, _, volume = parse_cache()
return tonumber(volume) or 0.0
end
function M.is_shuffle()
local _, _, _, _, _, _, shuffle = parse_cache()
return shuffle == "On"
end
function M.is_loop()
local _, _, _, _, _, loop = parse_cache()
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
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
title = shorten_text(title)
artist = shorten_text(artist)
return format_icon_with_text(icon, artist, title)
end
return M
+82
View File
@@ -0,0 +1,82 @@
-- ╭─────────────────────────────────────────────────────────╮
-- │ WakaTime Stats │
-- ╰─────────────────────────────────────────────────────────╯
local M = {}
local cache_file = os.getenv("HOME") .. "/.cache/wakatime_cache.txt"
local function read_cache()
local file = io.open(cache_file, "r")
if file then
local content = file:read("*a")
file:close()
return content
end
return "Error: Cache file not found"
end
local function get_wakatime_today()
local result = read_cache()
if not result or result == "" then
return ""
end
-- Handle common error messages
if
result:match("command not found")
or result:match("No such file or directory")
then
return "Error: wakatime-cli not found"
end
return result:match("^%s*(.-)%s*$") -- Trim any whitespace
end
function M.get_today()
return get_wakatime_today()
end
function M.get_icon()
local text = M.get_today()
if text and text:match("Error:") then
return "󰥕 " -- Icon for error or issue
else
return "󱦺 " -- Default icon
end
end
function M.get_icon_with_text()
return M.get_icon() .. " " .. (M.get_today() or "Loading...")
end
function M.get_total_minutes()
local wakatime_string = M.get_today()
if not wakatime_string or wakatime_string:match("Error:") then
return 0 -- Return 0 if there's an error in the wakatime string
end
local hours, mins = wakatime_string:match("(%d+)%s*hrs?%s*(%d+)%s*mins?")
if not hours then
mins = wakatime_string:match("(%d+)%s*mins?")
hours = 0
end
hours = tonumber(hours) or 0
mins = tonumber(mins) or 0
return (hours * 60) + mins
end
function M.get_color()
local total_minutes = M.get_total_minutes()
local rooticolor = require("utils.rooticolor") -- Adjust according to your actual setup
if total_minutes >= 60 then
return { fg = rooticolor.get_fg_color("GitSignsAdd") }
elseif total_minutes >= 30 then
return { fg = rooticolor.get_fg_color("GitSignsChange") }
else
return { fg = rooticolor.get_fg_color("GitSignsDelete") }
end
end
return M