.files/.config/waybar/scripts/workspaces
2024-11-01 11:46:32 +01:00

69 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Read configuration from environment variables
NUMBER=${NUMBER:-8} # Default to 4 if not set
SYMBOLS=${SYMBOLS:-";;;;;;;"} # Default symbols if not set
IFS=';' read -r -a CUSTOM_SYMBOLS <<< "$SYMBOLS"
PIPE=/tmp/workspace
WRAP=true
format_line() {
local current_workspace=$1
local output_text="{\"text\": \""
# Iterate through the workspace symbols
for i in $(seq 1 $NUMBER); do
local symbol="${CUSTOM_SYMBOLS[$((i-1))]}"
# Determine the span for the current symbol
if (( i == current_workspace )); then
output_text+="<span>[$symbol]</span>" # Indicate active workspace
else
output_text+="<span>$symbol</span>" # Indicate inactive workspace
fi
# Add a separator only if it's not the last element
if (( i < NUMBER )); then
output_text+="|"
fi
done
output_text+="\"}"
# Output the JSON object with a "text" field
echo "$output_text"
}
# Remove existing pipe and create a new one
rm -f $PIPE
mkfifo $PIPE
# Print initial state
current=1
format_line $current
# Main loop to handle input and update workspaces
while true; do
if read -t 0.1 input <$PIPE; then
if [ "$input" == "right" ]; then input=$((current + 1)); fi
if [ "$input" == "left" ]; then input=$((current - 1)); fi
if (( input < 1 )); then
if [ "$WRAP" == false ]; then continue; fi
input=$NUMBER
fi
if (( input > NUMBER )); then
if [ "$WRAP" == false ]; then continue; fi
input=1
fi
if (( input == current )); then continue; fi
current=$input
fi
format_line $current
done