local/bin/i3-focus

   1 #!/bin/bash
   2 
   3 current=0
   4 windows=()
   5 
   6 [[ $1 == -a || $1 == --all ]] && shift &&
   7 selector='.focused != null' ||
   8 selector=".num == $(
   9     i3-msg -t get_workspaces |
  10     jq '.[] | select(.focused == true).num'
  11 )"
  12 
  13 while read _ focused _ window; do
  14     [[ $window != null ]] && windows+=($window)
  15     [[ $focused == true ]] && current=$window
  16 done < <(
  17     i3-msg -t get_tree |
  18     jq ".nodes[].nodes[].nodes[] | select($selector)" |
  19     sed '/"focused"\|"window"/!d; /^   /!d; y/,/ /' | paste - -
  20 )
  21 
  22 prev=${windows[-1]}
  23 next=${windows[0]}
  24 
  25 for window in ${windows[@]}; do
  26     [[ $found ]] && next=$window && break
  27     [[ $window == $current ]] && found=1 || prev=$window
  28 done
  29 
  30 case $1 in
  31     prev) i3-msg [id=$prev] focus;;
  32     next) i3-msg [id=$next] focus;;
  33 
  34    first) i3-msg [id=${windows[0]}] focus;;
  35     last) i3-msg [id=${windows[-1]}] focus;;
  36 
  37  [-0-9]*) i3-msg [id=${windows[$1]}] focus;;
  38 esac