#!/bin/sh # Toggle Polybar on the currently focused monitor under bspwm get_polybar_id() { # List monitors and extract names monitors=$(polybar --list-monitors | cut -d":" -f1) # Get PIDs of polybar processes pids=$(pgrep polybar) # Get the currently focused monitor name focused=$(bspc query -M -m focused --names) # Convert monitors to an array set -- "$monitors" mon_count=$# # Number of monitors # Initialize index ind=0 # Find the index of the focused monitor for monitor in $monitors; do ind=$((ind + 1)) if [ "$monitor" = "$focused" ]; then break fi done # Extract the correct PID based on the index # Convert pids into an array using space as a delimiter pid_array=$(echo "$pids" | tr ' ' '\n') poly_id=$(echo "$pid_array" | sed -n "${ind}p") # Check if poly_id is set; if not, exit with an error if [ -z "$poly_id" ]; then echo "No PID found for the focused monitor." exit 1 fi } get_polybar_id # Define the state file STATE_FILE="/tmp/polybar_toggle_state.$poly_id" # Check if the state file exists, create it if it doesn't if [ ! -f "$STATE_FILE" ]; then echo "show" > "$STATE_FILE" # Default state fi # Read the current state CURRENT_STATE=$(cat "$STATE_FILE") # Toggle the state if [ "$CURRENT_STATE" == "show" ]; then echo "hide" > "$STATE_FILE" polybar-msg -p "$poly_id" cmd hide | bspc config -m "$focused" top_padding 0 else echo "show" > "$STATE_FILE" polybar-msg -p "$poly_id" cmd show fi