From 163aa7d6565661e68b101be0360375e1f386c092 Mon Sep 17 00:00:00 2001 From: rootiest Date: Fri, 9 Aug 2024 05:37:09 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf(cache=5Fstats):=20kee?= =?UTF-8?q?p=20last=20known=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lua/utils/music_stats.lua | 12 +++++++++--- lua/utils/wakatime_stats.lua | 21 +++++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lua/utils/music_stats.lua b/lua/utils/music_stats.lua index 57100ff..3c2a5d7 100644 --- a/lua/utils/music_stats.lua +++ b/lua/utils/music_stats.lua @@ -7,6 +7,7 @@ require("utils.cache_stats") local cache_file = os.getenv("HOME") .. "/.cache/music_cache.txt" local separator = "␟" +local last_known_value = "" -- Function to read the cache file local function read_cache() @@ -14,15 +15,20 @@ local function read_cache() if file then local content = file:read("*a") file:close() + last_known_value = content -- Update the last known value return content end - return "" + return last_known_value -- Return last known value if file cannot be read end -- Parse the cached music data local function parse_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 "", "", "", "", "", "", "" end local pattern = "^(.-)%s*" @@ -119,7 +125,7 @@ local function abbreviate(text) ["Original Mix%s"] = "Orig. Mix ", } 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 return text end diff --git a/lua/utils/wakatime_stats.lua b/lua/utils/wakatime_stats.lua index 8732da8..3be7263 100644 --- a/lua/utils/wakatime_stats.lua +++ b/lua/utils/wakatime_stats.lua @@ -6,29 +6,30 @@ local M = {} require("utils.cache_stats") local cache_file = os.getenv("HOME") .. "/.cache/wakatime_cache.txt" +local last_known_value = "" + local function read_cache() local file = io.open(cache_file, "r") if file then local content = file:read("*a") file:close() + last_known_value = content return content end - return "" -- Return empty string if file is not found or empty + return last_known_value end local function get_wakatime_today() local result = read_cache() if not result or result:match("^%s*$") then - -- Return empty string if result is nil or only whitespace - return "" + return last_known_value end - -- Handle common error messages if result:match("command not found") or result:match("No such file or directory") then - return "" + return last_known_value end return result:match("^%s*(.-)%s*$") -- Trim any whitespace @@ -41,7 +42,7 @@ end function M.get_icon() local text = M.get_today() if text ~= "" then - return "󱦺 " -- Default icon + return "󱦺 " -- Default icon else return "" -- No icon end @@ -77,14 +78,14 @@ end function M.get_color() 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 - return { fg = rooticolor.get_fg_color("GitSignsAdd") } + return { fg = rootilities.get_fg_color("GitSignsAdd") } elseif total_minutes >= 30 then - return { fg = rooticolor.get_fg_color("GitSignsChange") } + return { fg = rootilities.get_fg_color("GitSignsChange") } else - return { fg = rooticolor.get_fg_color("GitSignsDelete") } + return { fg = rootilities.get_fg_color("GitSignsDelete") } end end