diff --git a/lua/config/wakatime_cache.lua b/lua/config/wakatime_stats.lua similarity index 70% rename from lua/config/wakatime_cache.lua rename to lua/config/wakatime_stats.lua index b7615fc..20d08ae 100644 --- a/lua/config/wakatime_cache.lua +++ b/lua/config/wakatime_stats.lua @@ -1,18 +1,24 @@ -- ----------------------------------------------------------------------------- --- ----------------------------- WAKATIME-CACHE -------------------------------- +-- ----------------------------- WAKATIME-STATS -------------------------------- -- ----------------------------------------------------------------------------- local M = {} -local cache = { - value = nil, - timestamp = 0, -} -local cache_duration = 60 -- Cache duration in seconds +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 = vim.call("system", "~/.wakatime/wakatime-cli --today 2>&1") + local result = read_cache() if not result or result == "" then - return "Error: Unable to read wakatime-cli output" + return "" end -- Handle common error messages @@ -27,12 +33,7 @@ local function get_wakatime_today() end function M.get_today() - local current_time = os.time() - if current_time - cache.timestamp > cache_duration then - cache.value = get_wakatime_today() - cache.timestamp = current_time - end - return cache.value + return get_wakatime_today() end function M.get_icon() @@ -44,6 +45,10 @@ function M.get_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 diff --git a/scripts/update_wakatime.sh b/scripts/update_wakatime.sh new file mode 100755 index 0000000..e828426 --- /dev/null +++ b/scripts/update_wakatime.sh @@ -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