feat: enhance cache system for music and Wakatime integration with new options
This commit is contained in:
@@ -32,6 +32,18 @@ vim.g.usewakatime = true --- @type boolean Options: <true|false>
|
|||||||
vim.g.usehardtime = false --- @type boolean Options: <true|false>
|
vim.g.usehardtime = false --- @type boolean Options: <true|false>
|
||||||
vim.g.useimage = true --- @type boolean Options: <true|false>
|
vim.g.useimage = true --- @type boolean Options: <true|false>
|
||||||
|
|
||||||
|
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Lualine Stats ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
vim.g.stats_wakatime = true --- @type boolean Options: <true|false>
|
||||||
|
vim.g.stats_music = true --- @type boolean Options: <true|false>
|
||||||
|
vim.g.stats_ignored_players = { --- @type string[] Options: [ignored players]
|
||||||
|
"chromium",
|
||||||
|
"firefox",
|
||||||
|
"kdeconnect",
|
||||||
|
"haruna",
|
||||||
|
"plasma-browser-integration",
|
||||||
|
"plasma-browser-integration-76042",
|
||||||
|
}
|
||||||
|
|
||||||
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ COLOR ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ COLOR ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
-- Background color
|
-- Background color
|
||||||
vim.o.background = "dark" --- @type string Options: <dark|light>
|
vim.o.background = "dark" --- @type string Options: <dark|light>
|
||||||
|
|||||||
@@ -3,7 +3,13 @@
|
|||||||
-- ╰─────────────────────────────────────────────────────────╯
|
-- ╰─────────────────────────────────────────────────────────╯
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Options ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
|
-- Options can be set with:
|
||||||
|
-- vim.g.stats_wakatime = true|false (default: true)
|
||||||
|
-- vim.g.stats_music = true|false (default: true)
|
||||||
|
-- vim.g.stats_ignored_players = { "player1", "player2", ... }
|
||||||
|
|
||||||
|
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━ Script execution ━━━━━━━━━━━━━━━━━━━━━━━
|
||||||
-- Path to the cache script
|
-- Path to the cache script
|
||||||
local config_dir = vim.fn.stdpath("config") --[[@as string]]
|
local config_dir = vim.fn.stdpath("config") --[[@as string]]
|
||||||
local cache_script = vim.fs.joinpath(config_dir, "scripts", "update_cache.sh")
|
local cache_script = vim.fs.joinpath(config_dir, "scripts", "update_cache.sh")
|
||||||
@@ -11,15 +17,64 @@ local cache_script = vim.fs.joinpath(config_dir, "scripts", "update_cache.sh")
|
|||||||
-- Flag to check if the script has already been run
|
-- Flag to check if the script has already been run
|
||||||
local script_run_once = false
|
local script_run_once = false
|
||||||
|
|
||||||
|
-- Default list of players/sources to ignore when updating the status
|
||||||
|
local ignored_players = {
|
||||||
|
"chromium",
|
||||||
|
"firefox",
|
||||||
|
"kdeconnect",
|
||||||
|
"haruna",
|
||||||
|
"plasma-browser-integration",
|
||||||
|
"plasma-browser-integration-76042",
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Add any vim.g.stats_ignored_players to the list
|
||||||
|
for _, player in ipairs(vim.g.stats_ignored_players or {}) do
|
||||||
|
table.insert(ignored_players, player)
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.g.stats_music == nil then
|
||||||
|
vim.g.stats_music = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.g.stats_wakatime == nil then
|
||||||
|
vim.g.stats_wakatime = true
|
||||||
|
end
|
||||||
|
|
||||||
--- Function to run the cache script
|
--- Function to run the cache script
|
||||||
---@return nil
|
---@return boolean|number pid The pid of the script
|
||||||
function M.setup_script()
|
function M.setup_script()
|
||||||
if not script_run_once then
|
if not script_run_once then
|
||||||
-- Run the cache script
|
-- Build the arguments for the cache script
|
||||||
vim.system({ cache_script }, { detach = true }, function() end)
|
local args = { cache_script }
|
||||||
|
|
||||||
|
-- Add the --ignore option with the ignored players
|
||||||
|
if #ignored_players > 0 then
|
||||||
|
table.insert(args, "--ignore")
|
||||||
|
table.insert(args, table.concat(ignored_players, ","))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Add the --disable-wakatime option if wakatime is disabled
|
||||||
|
if vim.g.stats_wakatime == false then
|
||||||
|
table.insert(args, "--disable-wakatime")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Add the --no-music option if music is disabled
|
||||||
|
if vim.g.stats_music == false then
|
||||||
|
table.insert(args, "--no-music")
|
||||||
|
end
|
||||||
|
|
||||||
-- Prevent the script from running again
|
-- Prevent the script from running again
|
||||||
script_run_once = true
|
script_run_once = true
|
||||||
|
|
||||||
|
-- Run the cache script with the constructed arguments
|
||||||
|
local script = vim.system(args, { detach = true }, function() end)
|
||||||
|
|
||||||
|
-- Return the pid of the script
|
||||||
|
return script.pid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Return false if the script has already been run
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Run the setup script only once
|
-- Run the setup script only once
|
||||||
|
|||||||
+58
-23
@@ -4,20 +4,51 @@
|
|||||||
separator="␟"
|
separator="␟"
|
||||||
|
|
||||||
# Ignored player sources for music
|
# Ignored player sources for music
|
||||||
ignored_players=(
|
ignored_sources=""
|
||||||
"chromium"
|
|
||||||
"firefox"
|
|
||||||
"kdeconnect"
|
|
||||||
"haruna"
|
|
||||||
"plasma-browser-integration"
|
|
||||||
"plasma-browser-integration-76042"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Convert array to a comma-separated string with no spaces
|
# Flags for disabling functionality
|
||||||
ignored_sources=$(
|
disable_wakatime=false
|
||||||
IFS=,
|
no_music=false
|
||||||
echo "${ignored_players[*]}"
|
|
||||||
)
|
# Function to print usage
|
||||||
|
print_usage() {
|
||||||
|
echo "Usage: $0 [OPTION]..."
|
||||||
|
echo "Options:"
|
||||||
|
echo " -h, --help Print this help message and exit"
|
||||||
|
echo " -i, --ignore Comma-separated list of sources to ignore"
|
||||||
|
echo " -d, --disable-wakatime Disable wakatime integration"
|
||||||
|
echo " -n, --no-music Disable music integration"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse command-line arguments
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case "$1" in
|
||||||
|
-h | --help)
|
||||||
|
print_usage
|
||||||
|
;;
|
||||||
|
-i | --ignore)
|
||||||
|
if [[ -n $2 && $2 != -* ]]; then
|
||||||
|
ignored_sources=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo "Error: --ignore requires an argument."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-d | --disable-wakatime)
|
||||||
|
disable_wakatime=true
|
||||||
|
;;
|
||||||
|
-n | --no-music)
|
||||||
|
no_music=true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Unknown option: $1"
|
||||||
|
print_usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
# Lock file path
|
# Lock file path
|
||||||
lock_file="/tmp/music_wakatime_update.lock"
|
lock_file="/tmp/music_wakatime_update.lock"
|
||||||
@@ -37,17 +68,21 @@ cleanup() {
|
|||||||
}
|
}
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
# Update the music cache file every second
|
# Update the music cache file every second, if not disabled
|
||||||
while true; do
|
if ! $no_music; then
|
||||||
playerctl --ignore-player "${ignored_sources}" metadata --format "{{ artist }}${separator}{{ title }}${separator}{{ album }}${separator}{{ status }}${separator}{{ volume }}${separator}{{ loop }}${separator}{{ shuffle }}" >~/.cache/music_cache.txt
|
while true; do
|
||||||
sleep 1
|
playerctl --ignore-player "${ignored_sources}" metadata --format "{{ artist }}${separator}{{ title }}${separator}{{ album }}${separator}{{ status }}${separator}{{ volume }}${separator}{{ loop }}${separator}{{ shuffle }}" >~/.cache/music_cache.txt
|
||||||
done & # Run this loop in the background
|
sleep 1
|
||||||
|
done & # Run this loop in the background
|
||||||
|
fi
|
||||||
|
|
||||||
# Update the Wakatime cache file every 60 seconds
|
# Update the Wakatime cache file every 60 seconds, if not disabled
|
||||||
while true; do
|
if ! $disable_wakatime; then
|
||||||
~/.wakatime/wakatime-cli --today >~/.cache/wakatime_cache.txt 2>/dev/null
|
while true; do
|
||||||
sleep 60
|
~/.wakatime/wakatime-cli --today >~/.cache/wakatime_cache.txt 2>/dev/null
|
||||||
done & # Run this loop in the background
|
sleep 60
|
||||||
|
done & # Run this loop in the background
|
||||||
|
fi
|
||||||
|
|
||||||
# Wait for all background processes to finish
|
# Wait for all background processes to finish
|
||||||
wait
|
wait
|
||||||
|
|||||||
Reference in New Issue
Block a user