aboutsummaryrefslogtreecommitdiffstats
path: root/home/quickshell/bar/blocks/Battery.qml
blob: dd52f7f5059d30b7079849b5eb1b1101aad7f926 (plain) (blame)
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
import QtQuick
import Quickshell.Io
import "../"

BarBlock {
  property string battery
  property bool hasBattery: false
  visible: hasBattery
  
  content: BarText {
    symbolText: battery
  }

  Process {
    id: batteryCheck
    command: ["sh", "-c", "test -d /sys/class/power_supply/BAT*"]
    running: true
    onExited: function(exitCode) { hasBattery = exitCode === 0 }
  }

  Process {
    id: batteryProc
    // Modify command to get both capacity and status in one call
    command: ["sh", "-c", "echo $(cat /sys/class/power_supply/BAT*/capacity),$(cat /sys/class/power_supply/BAT*/status)"]
    running: hasBattery

    stdout: SplitParser {
      onRead: function(data) {
        const [capacityStr, status] = data.trim().split(',')
        const capacity = parseInt(capacityStr)
        let batteryIcon = "󰂂"
        if (capacity <= 20) batteryIcon = "󰁺"
        else if (capacity <= 40) batteryIcon = "󰁽"
        else if (capacity <= 60) batteryIcon = "󰁿"
        else if (capacity <= 80) batteryIcon = "󰂁"
        else batteryIcon = "󰂂"
        
        const symbol = status === "Charging" ? "🔌" : batteryIcon
        battery = `${symbol} ${capacity}%`
      }
    }
  }

  Timer {
    interval: 1000
    running: hasBattery
    repeat: true
    onTriggered: batteryProc.running = true
  }
}