_bluetoothctl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #compdef bluetoothctl
  2. __bluetoothctl() {
  3. bluetoothctl "$@" 2>/dev/null
  4. }
  5. _bluezcomp_controller() {
  6. local -a controllers
  7. bluetoothctl list |
  8. while read _ MAC NAME; do
  9. controllers+="${MAC//:/\\:}:${NAME//:/\\:}"
  10. done
  11. _describe -t controllers 'controller' controllers
  12. }
  13. _bluezcomp_device() {
  14. local -a devices
  15. bluetoothctl devices |
  16. while read _ MAC NAME; do
  17. devices+="${MAC//:/\\:}:${NAME//:/\\:}"
  18. done
  19. _describe -t devices 'device' devices
  20. }
  21. _bluezcomp_agentcap() {
  22. local -a agent_options=(${(f)"$(__bluetoothctl agent help)"})
  23. agent_options=( "${(@)agent_options:#(on|off)}" )
  24. compadd -a agent_options
  25. }
  26. _bluetoothctl_agent() {
  27. local -a agent_options=(${(f)"$(__bluetoothctl agent help)"})
  28. agent_options+=help
  29. compadd -a agent_options
  30. }
  31. _bluetoothctl_advertise() {
  32. local -a ad_options=(${(f)"$(__bluetoothctl advertise help)"})
  33. ad_options+=help
  34. compadd -a ad_options
  35. }
  36. _bluetoothctl() {
  37. local -a toggle_commands=(
  38. "discoverable" "pairable" "power" "scan"
  39. )
  40. local -a controller_commands=(
  41. "select" "show"
  42. )
  43. local -a device_commands=(
  44. "block" "connect" "disconnect" "info"
  45. "pair" "remove" "trust" "unblock" "untrust"
  46. )
  47. # Other commands may be handled by _bluetoothctl_$command
  48. local -a all_commands=( "${(@f)$(__bluetoothctl --zsh-complete help)}" )
  49. local curcontext=$curcontext state line ret=1
  50. _arguments -C \
  51. + '(info)' \
  52. {-h,--help}'[Show help message and exit]' \
  53. {-v,--version}'--version[Show version info and exit]' \
  54. + 'mod' \
  55. '(info)'{-a+,--agent=}'[Register agent handler]:agent:_bluezcomp_agentcap' \
  56. '(info)'{-t,--timeout}'[Timeout in seconds for non-interactive mode]' \
  57. '(info)'{-m,--monitor}'[Enable monitor output]' \
  58. + 'command' \
  59. '(info):command:->command' \
  60. '(info):: :->argument' \
  61. ': :_message "no more arguments"'
  62. if [[ $state == "command" ]]; then
  63. _describe -t commands 'command' all_commands
  64. elif [[ $state == "argument" ]]; then
  65. if (( ! ${"${(@)all_commands%%:*}"[(I)${line[1]}]} )); then
  66. _message "Unknown bluetoothctl command: $line[1]"
  67. return 1;
  68. fi
  69. curcontext="${curcontext%:*:*}:bluetoothctl-$line[1]:"
  70. if ! _call_function ret _bluetoothctl_$line[1]; then
  71. case $line[1] in
  72. (${(~j.|.)toggle_commands})
  73. compadd on off
  74. ;;
  75. (${(~j.|.)device_commands})
  76. _bluezcomp_device
  77. ;;
  78. (${(~j.|.)controller_commands})
  79. _bluezcomp_controller
  80. ;;
  81. esac
  82. fi
  83. return ret
  84. fi
  85. } && _bluetoothctl