blob: fd34c506e219c25087ccf4516a4b370de6cfb631 (
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
|
#!/usr/bin/env nu
const ICONS = [
[ normal charging];
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
];
const DELAY = 2sec;
def "main auto" [] {
loop {
let paths = ls "/sys/class/power_supply"
| each {|it| $it.name | path basename}
| filter {|it| $it starts-with "BAT"};
if ($paths | is-not-empty) {
let result = $paths
| each {|it| get_and_format $it}
| str join " | "
| prepend "| "
| str join;
print $result
} else {
print ""
}
sleep $DELAY;
}
}
def main [ path: string ] {
loop {
print (get_and_format $path)
sleep $DELAY;
}
}
def get_and_format [ path: string ] {
let fract = get_bat_charge_fraction $path;
let is_charging = get_bat_charging_status $path;
let percent = ($fract * 100) | math round;
return $"<span foreground=\"#d65d0e\">(get_bat_icon $fract $is_charging)</span> ($percent)<span foreground=\"#7c6f64\">%</span>";
}
def get_bat_charge_fraction [
path: string
] {
let energy_full = open $"/sys/class/power_supply/($path)/energy_full" | into float;
let energy_now = open $"/sys/class/power_supply/($path)/energy_now" | into float;
$energy_now / $energy_full
}
def get_bat_charging_status [
path: string
] {
let status = open $"/sys/class/power_supply/($path)/status";
$status like Charging
}
def get_bat_icon [
frac: float
is_charging = false
] {
$ICONS | get (($frac * 10) | math round) | get (if ($is_charging) { "charging" } else { "normal" })
}
|