local/bin/hudmenu

   1 #!/bin/bash
   2 
   3 if [[ ! $1 ]]; then
   4     win=$(xprop -root _NET_ACTIVE_WINDOW | awk '{ print $5 }')
   5     win=${win%,}
   6 
   7     $0 $win | dmenu -i -p "$(icon hud)" -l 15 | xargs -rI % $0 $win %
   8     exit
   9 fi
  10 
  11 declare -A pids labels
  12 busname=$(xprop -id $1 _GTK_UNIQUE_BUS_NAME     | cut -d \" -f 2)
  13 objpath=$(xprop -id $1 _GTK_MENUBAR_OBJECT_PATH | cut -d \" -f 2)
  14 
  15 while IFS=\| read id pos sid action accel label; do
  16     pid=${pids[$id]}
  17     label=${label//_/}
  18 
  19     [[ $pid ]] &&
  20     label="${labels[$id]} > $label"
  21 
  22     if [[ $sid ]]; then
  23         pids[$sid]="$pid.$sid"
  24         labels[$sid]="$label"
  25         continue
  26     fi
  27 
  28     accel=${accel//<Primary>/Ctrl+}
  29     accel=${accel//<Shift>/Shift+}
  30     accel=${accel//<Alt>/Alt+}
  31     accel=${accel//ampersand/&}
  32     accel=${accel//asciicircum/^}
  33     accel=${accel//asterisk/*}
  34     accel=${accel//bar/|}
  35     accel=${accel//bracketleft/[}
  36     accel=${accel//bracketright/]}
  37     accel=${accel//comma/,}
  38     accel=${accel//exclam/!}
  39     accel=${accel//ISO_Left_Tab/Shift+Tab}
  40     accel=${accel//minus/-}
  41     accel=${accel//numbersign/\#}
  42     accel=${accel//parenleft/(}
  43     accel=${accel//parenright/)}
  44     accel=${accel//period/.}
  45     accel=${accel//plus/+}
  46     accel=${accel//question/?}
  47     accel=${accel//slash//}
  48     accel=${accel//_/ }
  49 
  50     letter=${accel##*+}
  51     [[ ${#letter} == 1 ]] &&
  52     accel=${accel:0:-1}${letter^}
  53 
  54     printf '%s\t%s\t%s\n' "$pid.$id.$pos.$(( ++i ))" "$label" "$accel"
  55 
  56     if [[ $2 && $2 == "$label"* ]]; then
  57         busctl --user  -j call $busname $objpath \
  58         org.gtk.Actions Activate sava{sv} -- "${action#unity.}" 0 0
  59         exit
  60     fi
  61 done < <(
  62     busctl --user  -j call $busname $objpath \
  63     org.gtk.Menus Start au -- 100 {0..99} | jq -r '
  64     .data[] | .[] |
  65     .[0] as $id   |
  66     .[1] as $pos  |
  67     (.[2] | .[] | select(.label) | [
  68         $id,
  69         $pos,
  70         (.":submenu".data | .[0]),
  71         .action.data,
  72         .accel.data,
  73         .label.data
  74     ] | @tsv)
  75     ' | tr \\t \|
  76 ) | sort -k 1,1V | cut -f 2- | column -ts $'\t'
  77