aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/bar-toggle.sh
diff options
context:
space:
mode:
authorJustine Smithies <justine@smithies.me.uk>2025-11-04 20:40:38 +0000
committerJustine Smithies <justine@smithies.me.uk>2025-11-04 20:40:38 +0000
commitaa66d791c82e02e6e5529d906d4009daf8bb34f9 (patch)
tree7f8d7b1d519a6e6ded08cd37146c371f04fbc5cb /.local/bin/bar-toggle.sh
parentb347fc3db35ef68103beb4b735a5e9df04b3a082 (diff)
Initial commit
Diffstat (limited to '.local/bin/bar-toggle.sh')
-rwxr-xr-x.local/bin/bar-toggle.sh61
1 files changed, 61 insertions, 0 deletions
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