nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

btmenu (641B)


      1 #!/usr/bin/env bash
      2 
      3 devices() {
      4 	bluetoothctl devices "$1" | while read -r _ mac name; do
      5 		echo "$mac $name"
      6 	done
      7 }
      8 
      9 pick() {
     10 	local type="$1"
     11 	shift 1
     12 	devices "$type" | dmenu "$@" | awk '{print $1}'
     13 }
     14 
     15 get_name() {
     16 	bluetoothctl info "$1" | awk '/Name:/ {sub(/.*Name: /, ""); print}'
     17 }
     18 
     19 case "$1" in
     20 connect | con | c)
     21 	cmd=connect
     22 	mac=$(pick Paired "${@:2}")
     23 	;;
     24 disconnect | dis | d)
     25 	cmd=disconnect
     26 	mac=$(pick Connected "${@:2}")
     27 	;;
     28 *)
     29 	echo "Usage: $0 connect|disconnect [dmenu_args]" >&2
     30 	exit 1
     31 	;;
     32 esac
     33 
     34 [ -z "$mac" ] && exit 1
     35 
     36 name=$(get_name "$mac")
     37 
     38 bluetoothctl "$cmd" "$mac" && notify-send "Bluetooth" "${cmd^}ed $name"