Skip to content

Instantly share code, notes, and snippets.

@ClementTsang
Last active May 3, 2022 23:21
Show Gist options
  • Save ClementTsang/36f30ba8e6c62e929d69973542c8b0ff to your computer and use it in GitHub Desktop.
Save ClementTsang/36f30ba8e6c62e929d69973542c8b0ff to your computer and use it in GitHub Desktop.
Ugly script for polybar to get bluetooth battery and display to stdout; requires FA
#!/bin/python3
import subprocess
import sys
nicknames = dict(tuple(n.split("=")) for n in sys.argv[1:])
call = subprocess.run(["upower", "-d"], stdout=subprocess.PIPE)
out = call.stdout
devices = [o for o in out.split(b"\n\n")]
devs = []
for device in devices:
d = dict()
lines = device.splitlines()
for line in lines:
stripped_line = line.strip()
if stripped_line.startswith(b"model:"):
d["model"] = stripped_line.split(b":")[1].strip().decode("utf-8")
elif stripped_line.startswith(b"percentage:"):
d["percentage"] = stripped_line.split(b":")[1].strip().decode("utf-8").split(" ")[0].strip()
if "model" in d and "percentage" in d:
devs.append(d)
s = ""
for d in devs:
name = nicknames[d["model"]] if d["model"] in nicknames else d["model"]
if name == "!":
continue
s += " "
s += name
s += " "
s += d["percentage"]
s += " "
s = s.strip()
print(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment