️ perf(cache_stats): keep last known value

Keep the last value from the cache file in case we can't read it or it's empty, we can fallback to the last value.
This commit is contained in:
2024-08-09 05:37:09 -04:00
parent 6a17dac714
commit 163aa7d656
2 changed files with 20 additions and 13 deletions
+9 -3
View File
@@ -7,6 +7,7 @@ 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 = ""
local last_known_value = ""
-- Function to read the cache file -- Function to read the cache file
local function read_cache() local function read_cache()
@@ -14,15 +15,20 @@ local function read_cache()
if file then if file then
local content = file:read("*a") local content = file:read("*a")
file:close() file:close()
last_known_value = content -- Update the last known value
return content return content
end end
return "" return last_known_value -- Return last known value if file cannot be read
end end
-- Parse the cached music data -- Parse the cached music data
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")
or content:match("^%s*$")
then
return "", "", "", "", "", "", "" return "", "", "", "", "", "", ""
end end
local pattern = "^(.-)%s*" local pattern = "^(.-)%s*"
@@ -119,7 +125,7 @@ local function abbreviate(text)
["Original Mix%s"] = "Orig. Mix ", ["Original Mix%s"] = "Orig. Mix ",
} }
for long, short in pairs(replacements) do for long, short in pairs(replacements) do
text = text:gsub(long, short) text = text:gsub("%f[%a]" .. long, short) -- Match only at word boundaries
end end
return text return text
end end
+10 -9
View File
@@ -6,29 +6,30 @@ local M = {}
require("utils.cache_stats") 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 last_known_value = ""
local function read_cache() local function read_cache()
local file = io.open(cache_file, "r") local file = io.open(cache_file, "r")
if file then if file then
local content = file:read("*a") local content = file:read("*a")
file:close() file:close()
last_known_value = content
return content return content
end end
return "" -- Return empty string if file is not found or empty return last_known_value
end end
local function get_wakatime_today() local function get_wakatime_today()
local result = read_cache() local result = read_cache()
if not result or result:match("^%s*$") then if not result or result:match("^%s*$") then
-- Return empty string if result is nil or only whitespace return last_known_value
return ""
end end
-- Handle common error messages
if if
result:match("command not found") result:match("command not found")
or result:match("No such file or directory") or result:match("No such file or directory")
then then
return "" return last_known_value
end end
return result:match("^%s*(.-)%s*$") -- Trim any whitespace return result:match("^%s*(.-)%s*$") -- Trim any whitespace
@@ -77,14 +78,14 @@ end
function M.get_color() function M.get_color()
local total_minutes = M.get_total_minutes() local total_minutes = M.get_total_minutes()
local rooticolor = require("utils.rooticolor") -- Adjust according to your actual setup local rootilities = require("utils.rootilities") -- Adjust according to your actual setup
if total_minutes >= 60 then if total_minutes >= 60 then
return { fg = rooticolor.get_fg_color("GitSignsAdd") } return { fg = rootilities.get_fg_color("GitSignsAdd") }
elseif total_minutes >= 30 then elseif total_minutes >= 30 then
return { fg = rooticolor.get_fg_color("GitSignsChange") } return { fg = rootilities.get_fg_color("GitSignsChange") }
else else
return { fg = rooticolor.get_fg_color("GitSignsDelete") } return { fg = rootilities.get_fg_color("GitSignsDelete") }
end end
end end