.files/.config/waybar/scripts/workspaces

70 lines
1.7 KiB
Text
Raw Normal View History

2024-10-17 15:16:09 +00:00
#!/usr/bin/env bash
2024-10-22 16:33:54 +00:00
# Read configuration from environment variables
2024-11-01 10:46:32 +00:00
NUMBER=${NUMBER:-8} # Default to 4 if not set
SYMBOLS=${SYMBOLS:-";;;;;;;"} # Default symbols if not set
2024-10-22 16:33:54 +00:00
IFS=';' read -r -a CUSTOM_SYMBOLS <<< "$SYMBOLS"
2024-10-17 15:16:09 +00:00
PIPE=/tmp/workspace
WRAP=true
format_line() {
2024-10-22 16:33:54 +00:00
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"
2024-10-17 15:16:09 +00:00
}
2024-10-22 16:33:54 +00:00
# Remove existing pipe and create a new one
2024-10-17 15:16:09 +00:00
rm -f $PIPE
mkfifo $PIPE
2024-10-22 16:33:54 +00:00
# Print initial state
2024-10-17 15:16:09 +00:00
current=1
format_line $current
2024-10-22 16:33:54 +00:00
# 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
2024-10-17 15:16:09 +00:00
2024-10-22 16:33:54 +00:00
if (( input < 1 )); then
2024-10-17 15:16:09 +00:00
if [ "$WRAP" == false ]; then continue; fi
input=$NUMBER
fi
2024-10-22 16:33:54 +00:00
if (( input > NUMBER )); then
2024-10-17 15:16:09 +00:00
if [ "$WRAP" == false ]; then continue; fi
input=1
fi
2024-10-22 16:33:54 +00:00
if (( input == current )); then continue; fi
2024-10-17 15:16:09 +00:00
current=$input
fi
2024-10-22 16:33:54 +00:00
format_line $current
2024-10-17 15:16:09 +00:00
done
2024-10-22 16:33:54 +00:00