️ perf(wakatime): use script to cache wakatime stats

Refactor wakatime lualine component to use an external script to cache the wakatime stats
This commit is contained in:
2024-08-03 22:46:21 -04:00
parent f625c215a7
commit 1cb641b326
2 changed files with 26 additions and 14 deletions
@@ -1,18 +1,24 @@
-- ----------------------------------------------------------------------------- -- -----------------------------------------------------------------------------
-- ----------------------------- WAKATIME-CACHE -------------------------------- -- ----------------------------- WAKATIME-STATS --------------------------------
-- ----------------------------------------------------------------------------- -- -----------------------------------------------------------------------------
local M = {} local M = {}
local cache = { local cache_file = os.getenv("HOME") .. "/.cache/wakatime_cache.txt"
value = nil,
timestamp = 0, local function read_cache()
} local file = io.open(cache_file, "r")
local cache_duration = 60 -- Cache duration in seconds 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 function get_wakatime_today()
local result = vim.call("system", "~/.wakatime/wakatime-cli --today 2>&1") local result = read_cache()
if not result or result == "" then if not result or result == "" then
return "Error: Unable to read wakatime-cli output" return ""
end end
-- Handle common error messages -- Handle common error messages
@@ -27,12 +33,7 @@ local function get_wakatime_today()
end end
function M.get_today() function M.get_today()
local current_time = os.time() return get_wakatime_today()
if current_time - cache.timestamp > cache_duration then
cache.value = get_wakatime_today()
cache.timestamp = current_time
end
return cache.value
end end
function M.get_icon() function M.get_icon()
@@ -44,6 +45,10 @@ function M.get_icon()
end end
end end
function M.get_icon_with_text()
return M.get_icon() .. " " .. (M.get_today() or "Loading...")
end
function M.get_total_minutes() function M.get_total_minutes()
local wakatime_string = M.get_today() local wakatime_string = M.get_today()
if not wakatime_string or wakatime_string:match("Error:") then if not wakatime_string or wakatime_string:match("Error:") then
+7
View File
@@ -0,0 +1,7 @@
#!/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