[sketchybar] multiple monitor aerospace support
change mkxwopruskysyrvwxzkklmolrzvutvox
commit 216199115047ffb0112e270e1e870cfd65aa41f1
author Alpha Chen <alpha@kejadlen.dev>
date
parent zxtxurxu
diff --git a/.config/sketchybar/plugins/aerospace.sh b/.config/sketchybar/plugins/aerospace.sh
index 660f67ba..c3b018f3 100755
--- a/.config/sketchybar/plugins/aerospace.sh
+++ b/.config/sketchybar/plugins/aerospace.sh
@@ -3,6 +3,68 @@
 # Original source:
 #   https://nikitabobko.github.io/AeroSpace/goodness#show-aerospace-workspaces-in-sketchybar
 
-# Update the current workspace label
-CURRENT_WORKSPACE=$(aerospace list-workspaces --focused)
-sketchybar --set $NAME label="$CURRENT_WORKSPACE"
+# Function to get focused workspace for a specific monitor
+get_focused_workspace_for_monitor() {
+    local monitor_id=$1
+
+    # Get all workspaces for this monitor and check which one has focus
+    # We'll use aerospace focus to determine which workspace is active on which monitor
+    local workspaces=$(aerospace list-workspaces --monitor $monitor_id 2>/dev/null)
+    local focused_workspace=$(aerospace list-workspaces --focused 2>/dev/null)
+
+    # Check if the focused workspace belongs to this monitor
+    if echo "$workspaces" | grep -q "^$focused_workspace$"; then
+        echo "$focused_workspace"
+    else
+        # If focused workspace is not on this monitor, get the first workspace
+        echo "$workspaces" | head -n 1
+    fi
+}
+
+# Function to update a specific monitor's workspace
+update_monitor_workspace() {
+    local monitor_id=$1
+    local item_name="aerospace_$monitor_id"
+
+    local workspace=$(get_focused_workspace_for_monitor $monitor_id)
+
+    if [ -n "$workspace" ]; then
+        local focused_workspace=$(aerospace list-workspaces --focused 2>/dev/null)
+
+        if [ "$workspace" = "$focused_workspace" ]; then
+            # Highlight the focused workspace
+            sketchybar --set $item_name \
+                label="$workspace" \
+                background.drawing=on \
+                background.color=0xffcccccc \
+                background.border_color=0xffcccccc \
+                background.corner_radius=6 \
+                background.height=26 \
+                label.padding_left=8 \
+                label.padding_right=8 \
+                padding_left=3 \
+                padding_right=3 \
+                label.color=0xff000000
+        else
+            # Normal appearance for non-focused workspaces
+            sketchybar --set $item_name \
+                label="$workspace" \
+                background.drawing=off \
+                label.color=0xffffffff
+        fi
+    fi
+}
+
+# If NAME is provided (called for specific item), update that item
+if [ -n "$NAME" ]; then
+    # Extract monitor ID from item name (aerospace_1 -> 1)
+    monitor_id=${NAME#aerospace_}
+    update_monitor_workspace $monitor_id
+else
+    # Update all monitors
+    # Get list of monitors and update each one
+    aerospace list-monitors | while IFS= read -r monitor; do
+        monitor_id=$(echo "$monitor" | awk '{print $1}')
+        update_monitor_workspace $monitor_id
+    done
+fi
diff --git a/.config/sketchybar/sketchybarrc b/.config/sketchybar/sketchybarrc
index 302cd6f6..0aeac245 100755
--- a/.config/sketchybar/sketchybarrc
+++ b/.config/sketchybar/sketchybarrc
@@ -38,11 +38,22 @@ sketchybar --default "${default[@]}"
 ### Aerospace
 
 sketchybar --add event aerospace_workspace_change
-sketchybar --add item current_workspace left \
-    --subscribe current_workspace aerospace_workspace_change \
-    --set current_workspace \
-    label="$(aerospace list-workspaces --focused)" \
-    script="$CONFIG_DIR/plugins/aerospace.sh"
+
+if command -v aerospace >/dev/null 2>&1; then
+    aerospace list-monitors 2>/dev/null | while IFS= read -r monitor; do
+        monitor_id=$(echo "$monitor" | awk '{print $1}')
+        item_name="aerospace_$monitor_id"
+
+        initial_workspace=$(aerospace list-workspaces --monitor $monitor_id --focused 2>/dev/null)
+
+        sketchybar --add item $item_name left \
+            --subscribe $item_name aerospace_workspace_change \
+            --set $item_name \
+            label="$initial_workspace" \
+            icon.drawing=off \
+            script="$CONFIG_DIR/plugins/aerospace.sh"
+    done
+fi
 
 ##### Adding Left Items #####
 # We add some regular items to the left side of the bar, where