added battery health and cycles count
This commit is contained in:
1 parent
8118227936
commit
cf7bf010f9
2 files changed
+45
-10
No files matched your search
@@ -1,17 +1,15 @@
|
|||||||
import { Astal, App, Gtk } from "astal/gtk3";
|
import { Astal, App, Gtk } from "astal/gtk3";
|
||||||
import Battery from "gi://AstalBattery";
|
import Battery from "gi://AstalBattery";
|
||||||
import { bind } from "astal";
|
import { bind, Variable } from "astal";
|
||||||
|
import {cycles_count, health} from "./helpers.ts";
|
||||||
|
|
||||||
const BatteryMenu = () => {
|
const BatteryMenu = () => {
|
||||||
const { TOP, RIGHT } = Astal.WindowAnchor;
|
const { TOP, RIGHT } = Astal.WindowAnchor;
|
||||||
const { START, END } = Gtk.Align;
|
const { START, END, CENTER } = Gtk.Align;
|
||||||
const bat = Battery.get_default();
|
const bat = Battery.get_default();
|
||||||
console.log(
|
const batcycles = Variable<string>("").poll(5000, () => cycles_count());
|
||||||
bat.charging,
|
const bathealth = Variable<string>("").poll(5000, () => health());
|
||||||
bat.capacity,
|
|
||||||
bat.energyFull,
|
|
||||||
bat.energy_full_design,
|
|
||||||
);
|
|
||||||
return (
|
return (
|
||||||
<window
|
<window
|
||||||
anchor={TOP | RIGHT}
|
anchor={TOP | RIGHT}
|
||||||
@@ -55,7 +53,7 @@ const BatteryMenu = () => {
|
|||||||
halign={START}
|
halign={START}
|
||||||
label="Health:"
|
label="Health:"
|
||||||
/>
|
/>
|
||||||
<label hexpand halign={END} label="92.5 %" />
|
<label hexpand halign={END} label={bathealth()} />
|
||||||
</box>
|
</box>
|
||||||
<box>
|
<box>
|
||||||
<label
|
<label
|
||||||
@@ -64,7 +62,7 @@ const BatteryMenu = () => {
|
|||||||
halign={START}
|
halign={START}
|
||||||
label="Charge cycles:"
|
label="Charge cycles:"
|
||||||
/>
|
/>
|
||||||
<label hexpand halign={END} label="325" />
|
<label hexpand halign={END} label={batcycles()} />
|
||||||
</box>
|
</box>
|
||||||
<box>
|
<box>
|
||||||
<label
|
<label
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { GLib } from "astal"
|
||||||
|
|
||||||
|
const cycles_count = (): string => {
|
||||||
|
try {
|
||||||
|
const [success, cycles] = GLib.file_get_contents("/sys/class/power_supply/BAT0/cycle_count");
|
||||||
|
if (!success || !cycles) {
|
||||||
|
throw new Error("Failed to read /sys/class/ or the file content is null.");
|
||||||
|
}
|
||||||
|
const cyclesinfo = new TextDecoder("utf-8").decode(cycles);
|
||||||
|
return cyclesinfo.replace(/\n$/, '');
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error get battery cycles_count");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const health = (): string => {
|
||||||
|
try {
|
||||||
|
const [success, charge_full] = GLib.file_get_contents("/sys/class/power_supply/BAT0/charge_full");
|
||||||
|
if (!success || !charge_full) {
|
||||||
|
throw new Error("Failed to read /sys/class/ or the file content is null.");
|
||||||
|
}
|
||||||
|
const [success_chagre_full_design, charge_full_design] = GLib.file_get_contents("/sys/class/power_supply/BAT0/charge_full_design");
|
||||||
|
if (!success_chagre_full_design || !charge_full_design) {
|
||||||
|
throw new Error("Failed to read /sys/class/ or the file content is null.");
|
||||||
|
}
|
||||||
|
const chargefullnow = parseInt(new TextDecoder("utf-8").decode(charge_full));
|
||||||
|
const chargefulldesign = parseInt(new TextDecoder("utf-8").decode(charge_full_design));
|
||||||
|
const percents = Math.round(chargefullnow * 100 / chargefulldesign);
|
||||||
|
return percents.toString() + " %";
|
||||||
|
} catch (error) {
|
||||||
|
console.error("error get battery health");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export {cycles_count, health};
|
||||||
Reference in new issue
Block a user