refaktor
This commit is contained in:
@@ -4,6 +4,7 @@ import Quickshell
|
||||
import Quickshell.Io
|
||||
|
||||
Item {
|
||||
property bool available: false
|
||||
property real percentage: 0
|
||||
property bool charging: false
|
||||
property real energy: 0
|
||||
@@ -14,37 +15,30 @@ Item {
|
||||
|
||||
function parseUpower(line) {
|
||||
line = line.trim()
|
||||
|
||||
if (line.startsWith("percentage:")) {
|
||||
percentage = parseInt(line.split(":")[1])
|
||||
// console.log("percentage =", percentage)
|
||||
}
|
||||
|
||||
else if (line.startsWith("state:")) {
|
||||
charging = !line.includes("discharging")
|
||||
// console.log("charging =", charging)
|
||||
}
|
||||
|
||||
else if (line.startsWith("energy:")) {
|
||||
energy = parseFloat(line.split(":")[1])
|
||||
// console.log("energy =", energy)
|
||||
}
|
||||
|
||||
else if (line.startsWith("voltage:")) {
|
||||
voltage = parseFloat(line.split(":")[1])
|
||||
// console.log("voltage =", voltage)
|
||||
}
|
||||
|
||||
else if (line.startsWith("energy-rate:")) {
|
||||
energy_rate = parseFloat(line.split(":")[1])
|
||||
// console.log("energy_rate =", energy_rate)
|
||||
}
|
||||
|
||||
else if (line.startsWith("charge-cycles:")) {
|
||||
charge_cycle = parseInt(line.split(":")[1])
|
||||
// console.log("charge_cycle =", charge_cycle)
|
||||
}
|
||||
|
||||
else if (line.startsWith("time to empty:")) {
|
||||
time_to_empty = parseFloat(line.split(":")[1])
|
||||
// console.log("time_to_empty =", time_to_empty)
|
||||
@@ -53,12 +47,25 @@ Item {
|
||||
|
||||
Process {
|
||||
id: upowerProc
|
||||
command: ["bash", "-c", "upower -i $(upower -e | grep BAT)"]
|
||||
|
||||
command: ["bash", "-c", "upower -i $(upower -e | grep '^/org/freedesktop/UPower/devices/battery')"]
|
||||
stdout: SplitParser {
|
||||
onRead: function(data) {
|
||||
parseUpower(data)
|
||||
// console.log("battery available:", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: batteryAvailable
|
||||
command: ["sh", "-c", "upower -e | grep -q '^/org/freedesktop/UPower/devices/battery' && echo '1' || echo '0'"]
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const hasBattery = this.text.trim() === "1"
|
||||
available = hasBattery
|
||||
if (hasBattery) {
|
||||
pollTimer.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,16 +75,11 @@ Item {
|
||||
interval: 1000
|
||||
running: false
|
||||
repeat: true
|
||||
triggeredOnStart: true
|
||||
triggeredOnStart: false
|
||||
onTriggered: upowerProc.running = true
|
||||
}
|
||||
|
||||
function start() {
|
||||
pollTimer.start()
|
||||
upowerProc.running = true
|
||||
}
|
||||
|
||||
function stop() {
|
||||
pollTimer.stop()
|
||||
Component.onCompleted: {
|
||||
batteryAvailable.running = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user