aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/bar-toggle.sh
blob: c39de27f81b04d82898d160f9b8016bb851c298a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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