# First arg always left|right. No need for guards b/c this is private function. direction=$1 shift # create fifo for collecting event stream temp=$(mktemp -d kak-niri-XXXXXX) fifo="$temp/fifo" mkfifo "$fifo" # register func to cleanup fifo cleanup() { rm -rf "$temp" } trap cleanup EXIT INT TERM # read event stream to fifo niri msg -j event-stream >"$fifo" & stream_pid=$! # register timeout fallback ( sleep 3 kill "$!" 2>/dev/null kill "$stream_pid" 2>/dev/null ) >/dev/null 2>&1 "$kak_command_fifo" fi ;; # newly opened window is now available to act on *"WindowOpenedOrChanged"*) # only operate if window isn't floating # This accursed incantation is to access the JSON field # without depending on jq. if expr "${line}" : '.*"is_floating":[[:space:]]*false.*' \ >/dev/null; then # handle new window being unfocused if expr "${line}" : '.*"is_focused":[[:space:]]*false.*' \ >/dev/null; then # extract the window ID # (POSIX compliant JSON parsing is ugly, sorry) id="$(echo "$line" | sed -n 's/.*"id":[[:space:]]*\([0-9][0-9]*\).*/\1/p')" # focus the new window niri msg action focus-window --id "$id" fi # consume the new window in specified direction niri msg action consume-or-expel-window-${direction} fi # close the stream reader and timeout watchdog kill "$stream_pid" wait "$stream_pid" 2>/dev/null kill "$watchdog_pid" 2>/dev/null exit 0 ;; esac done <"$fifo" } >/dev/null 2>&1