From 1cb641b326f1602730e3769eff1a4b977d68c88a Mon Sep 17 00:00:00 2001 From: rootiest Date: Sat, 3 Aug 2024 22:46:21 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf(wakatime):=20use=20sc?= =?UTF-8?q?ript=20to=20cache=20wakatime=20stats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactor wakatime lualine component to use an external script to cache the wakatime stats --- ...{wakatime_cache.lua => wakatime_stats.lua} | 33 +++++++++++-------- scripts/update_wakatime.sh | 7 ++++ 2 files changed, 26 insertions(+), 14 deletions(-) rename lua/config/{wakatime_cache.lua => wakatime_stats.lua} (70%) create mode 100755 scripts/update_wakatime.sh 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