summaryrefslogtreecommitdiff
path: root/users/kassouni/palette/bluetooth.nix
blob: 4d2a59406b14060998cf70d3f053b941c982f9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{ pkgs, config, ...}:
{

  kassouni.palette.commands = [
    {
      name = "bluetooth";
      icon = "󰂯";
      # `jc --bluetoothctl` turns `bluetoothctl devices` into JSON, so the menu is
      # built from parsed records rather than by splitting "Device <addr> <name>"
      # lines. That subcommand leaves every status field empty, so connectedness
      # comes from a second `devices Connected` call intersected on address.
      exe = pkgs.writers.writePython3 "bluetooth-toggle" {
        # The interpolated store paths below are longer than 79 columns.
        flakeIgnore = [ "E501" ];
      } ''
        import json
        import subprocess
        import sys

        BT = "${pkgs.bluez}/bin/bluetoothctl"
        JC = "${pkgs.jc}/bin/jc"
        FUZZEL = "${pkgs.fuzzel}/bin/fuzzel"


        def devices(*args):
            """Parse `bluetoothctl devices [args]` into a list of records."""
            listed = subprocess.run(
                [BT, "devices", *args], capture_output=True, text=True
            )
            parsed = subprocess.run(
                [JC, "--bluetoothctl"],
                input=listed.stdout,
                capture_output=True,
                text=True,
            )
            return json.loads(parsed.stdout or "[]")


        def label(device, connected):
            name = device["name"]
            if device["address"] in connected:
                name += " (connected)"
            return name


        def main():
            known = devices()
            if not known:
                return 0

            # Older bluez has no `devices Connected`; that call just comes back
            # empty there, which degrades to "nothing is connected".
            connected = {d["address"] for d in devices("Connected")}

            rows = [label(d, connected) for d in known]
            choice = subprocess.run(
                [FUZZEL, "--dmenu", "--index", "--prompt", "bluetooth "],
                input="\n".join(rows),
                capture_output=True,
                text=True,
            )
            if choice.returncode != 0:
                return 1

            index = int(choice.stdout.strip() or -1)
            if not 0 <= index < len(known):
                return 1

            device = known[index]
            action = (
                "disconnect" if device["address"] in connected else "connect"
            )
            return subprocess.run([BT, action, device["address"]]).returncode


        if __name__ == "__main__":
            sys.exit(main())
      '';
    }
  ];
}