diff --git a/modules/Bluetooth.qml b/modules/Bluetooth.qml index 8a9833b..a257292 100644 --- a/modules/Bluetooth.qml +++ b/modules/Bluetooth.qml @@ -10,24 +10,31 @@ M.BarSection { if (root.state === "off") return "Bluetooth: off"; if (root.state === "connected") - return "Bluetooth: " + root.device; + return "Bluetooth: " + root.device + (root.batteryPct >= 0 ? "\nBattery: " + root.batteryPct + "%" : ""); return "Bluetooth: on"; } property string state: "unavailable" property string device: "" + property int batteryPct: -1 function _parse(text) { - const t = text.trim(); + const lines = text.trim().split("\n"); + const t = lines[0] || ""; const sep = t.indexOf(":"); root.state = sep === -1 ? t : t.slice(0, sep); root.device = sep === -1 ? "" : t.slice(sep + 1); + root.batteryPct = -1; + for (let i = 1; i < lines.length; i++) { + if (lines[i].startsWith("bat:")) + root.batteryPct = parseInt(lines[i].slice(4)) || -1; + } } Process { id: proc running: true - command: ["sh", "-c", "s=$(bluetoothctl show 2>/dev/null); " + "[ -z \"$s\" ] && echo unavailable && exit; " + "echo \"$s\" | grep -q 'Powered: yes' || { echo off:; exit; }; " + "d=$(bluetoothctl info 2>/dev/null | awk -F': ' '/\\tName:/{n=$2}/Connected: yes/{c=1}END{if(c)print n}'); " + "[ -n \"$d\" ] && echo \"connected:$d\" || echo on:"] + command: ["sh", "-c", "s=$(bluetoothctl show 2>/dev/null); " + "[ -z \"$s\" ] && echo unavailable && exit; " + "echo \"$s\" | grep -q 'Powered: yes' || { echo off:; exit; }; " + "info=$(bluetoothctl info 2>/dev/null); " + "d=$(echo \"$info\" | awk -F': ' '/\\tName:/{n=$2}/Connected: yes/{c=1}END{if(c)print n}'); " + "[ -n \"$d\" ] && echo \"connected:$d\" || { echo on:; exit; }; " + "bat=$(echo \"$info\" | awk -F': ' '/Battery Percentage.*\\(/{gsub(/[^0-9]/,\"\",$2);print $2}'); " + "[ -n \"$bat\" ] && echo \"bat:$bat\""] stdout: StdioCollector { onStreamFinished: root._parse(text) } @@ -54,7 +61,7 @@ M.BarSection { } M.BarLabel { visible: root.state === "connected" - label: root.device + label: root.device + (root.batteryPct >= 0 ? " " + root.batteryPct + "%" : "") anchors.verticalCenter: parent.verticalCenter }