Skip to content

Instantly share code, notes, and snippets.

@ig0rsky
Last active May 25, 2021 12:50
Show Gist options
  • Save ig0rsky/af69f345dd96dd30594209becbfbe780 to your computer and use it in GitHub Desktop.
Save ig0rsky/af69f345dd96dd30594209becbfbe780 to your computer and use it in GitHub Desktop.
Custom logic implementations of Yabai for better UX.
#!/bin/bash
# The default logic for creating and destroying OS-level desktops is not user-friendly.
# We're implementing the following the logic:
# 1. Always create the new desktop next to the currently focused one.
# 2. When destroying the currently focused desktop, switch the focus to the nearest sibling.
index=$(jq -r '.[0].index ' < <(yabai -m query --spaces --window ))
prev_space=$(( index-1 ))
next_space=$(( index+1 ))
spaces=$(jq -r ' sort_by(.index) | .[-1].index + 1' < <(yabai -m query --spaces --display))
echo "At index: $index, space: $spaces"
function create_new_desktop {
echo "Creating new desktop and moving the current window to it."
yabai -m space --create
yabai -m space --move $spaces
difference=$(( $spaces - $next_space ))
for i in $(seq 1 $difference)
do
yabai -m space --move prev
done
}
function destroy_desktop {
echo "Destroying desktop"
yabai -m space --destroy
yabai -m space --focus $prev_space
}
#!/usr/bin/env bash
# We want to stay in the spaces range of the display we're currently working on (focused).
# If we want to switch to another monitor, then we have dedicated shortcuts for that.
# Yabai will go through the list of spaces in all the monitors by default.
# Example:
# MONITOR 1 -> 1 2 3 | MONITOR 2 -> 4 5 6 7
# We want to create a barrier between these two.
# Why is this necessary?
# When you spam focus next | previous over and over again, you will accidentally change the focus on -
# your other monitors as well.
# This means that now you have to now switch your focus and fix those up; it gets pretty annoying after a while.
INPUT_DIRECTION=$1
# can be prev, next or recent
COMMAND=$2
# can be switch or focus
FOCUSED_SPACE=$(yabai -m query --spaces --display | jq '.[]|select(.focused == 1).index')
MIN=$(yabai -m query --displays --display | jq '.spaces[0]')
MAX=$(yabai -m query --displays --display | jq '.spaces[-1]')
echo "SPACES: $FOCUSED_SPACE, MIN: $MIN, MAX: $MAX"
if [[ $FOCUSED_SPACE == "$MAX" && $INPUT_DIRECTION == "next" ]]
then
echo "You're at the edge man!"
elif [[ $FOCUSED_SPACE == "$MIN" && $INPUT_DIRECTION == "prev" ]]
then
echo "You're at the edge man!"
else
if [[ $COMMAND == "switch" ]]; then
yabai -m window --space $INPUT_DIRECTION; yabai -m space --focus $INPUT_DIRECTION
elif [[ $COMMAND == "focus" ]]; then
yabai -m space --focus $INPUT_DIRECTION
else
echo "Unknown command."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment