From aa66d791c82e02e6e5529d906d4009daf8bb34f9 Mon Sep 17 00:00:00 2001 From: Justine Smithies Date: Tue, 4 Nov 2025 20:40:38 +0000 Subject: Initial commit --- .local/bin/bar-toggle.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 .local/bin/bar-toggle.sh (limited to '.local/bin/bar-toggle.sh') diff --git a/.local/bin/bar-toggle.sh b/.local/bin/bar-toggle.sh new file mode 100755 index 0000000..c39de27 --- /dev/null +++ b/.local/bin/bar-toggle.sh @@ -0,0 +1,61 @@ +#!/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 -- cgit v1.2.3