added voltage in battery menu

This commit is contained in:
wt committed 2025-03-20 11:32:46 +07:00
1 parent cf7bf010f9
commit 0b5bd9cc9f
2 files changed
+54 -19

No files matched your search

+3 -2
View File
@@ -1,7 +1,7 @@
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, Variable } from "astal"; import { bind, Variable } from "astal";
import {cycles_count, health} from "./helpers.ts"; import { cycles_count, health, voltage } from "./helpers.ts";
const BatteryMenu = () => { const BatteryMenu = () => {
const { TOP, RIGHT } = Astal.WindowAnchor; const { TOP, RIGHT } = Astal.WindowAnchor;
@@ -9,6 +9,7 @@ const BatteryMenu = () => {
const bat = Battery.get_default(); const bat = Battery.get_default();
const batcycles = Variable<string>("").poll(5000, () => cycles_count()); const batcycles = Variable<string>("").poll(5000, () => cycles_count());
const bathealth = Variable<string>("").poll(5000, () => health()); const bathealth = Variable<string>("").poll(5000, () => health());
const batvoltage = Variable<string>("").poll(5000, () => voltage());
return ( return (
<window <window
@@ -80,7 +81,7 @@ const BatteryMenu = () => {
halign={START} halign={START}
label="Voltage:" label="Voltage:"
/> />
<label hexpand halign={END} label="12.1 V" /> <label hexpand halign={END} label={batvoltage()} />
</box> </box>
</box> </box>
</box> </box>
+51 -17
View File
@@ -1,37 +1,71 @@
import { GLib } from "astal" import { GLib } from "astal";
const cycles_count = (): string => { const cycles_count = (): string => {
try { try {
const [success, cycles] = GLib.file_get_contents("/sys/class/power_supply/BAT0/cycle_count"); const [success, cycles] = GLib.file_get_contents(
if (!success || !cycles) { "/sys/class/power_supply/BAT0/cycle_count",
throw new Error("Failed to read /sys/class/ or the file content is null."); );
} if (!success || !cycles) {
const cyclesinfo = new TextDecoder("utf-8").decode(cycles); throw new Error(
return cyclesinfo.replace(/\n$/, ''); "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) { } catch (error) {
console.error("Error get battery cycles_count"); console.error("Error get battery cycles_count");
return ""; return "";
} }
} };
const health = (): string => { const health = (): string => {
try { try {
const [success, charge_full] = GLib.file_get_contents("/sys/class/power_supply/BAT0/charge_full"); const [success, charge_full] = GLib.file_get_contents(
"/sys/class/power_supply/BAT0/charge_full",
);
if (!success || !charge_full) { if (!success || !charge_full) {
throw new Error("Failed to read /sys/class/ or the file content is null."); 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"); 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) { if (!success_chagre_full_design || !charge_full_design) {
throw new Error("Failed to read /sys/class/ or the file content is null."); 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 chargefullnow = parseInt(
const chargefulldesign = parseInt(new TextDecoder("utf-8").decode(charge_full_design)); new TextDecoder("utf-8").decode(charge_full),
const percents = Math.round(chargefullnow * 100 / chargefulldesign); );
const chargefulldesign = parseInt(
new TextDecoder("utf-8").decode(charge_full_design),
);
const percents = Math.round((chargefullnow * 100) / chargefulldesign);
return percents.toString() + " %"; return percents.toString() + " %";
} catch (error) { } catch (error) {
console.error("error get battery health"); console.error("error get battery health");
return ""; return "";
} }
} };
export {cycles_count, health}; const voltage = (): string => {
try {
const [success, voltage] = GLib.file_get_contents(
"/sys/class/power_supply/BAT0/voltage_now",
);
if (!success || !voltage) {
throw new Error(
"Failed to read /sys/class/ or the file content is null.",
);
}
const voltage_now = parseInt(new TextDecoder("utf-8").decode(voltage));
const voltage_string = (voltage_now / 1000).toString() + " V";
return voltage_string;
} catch (error) {
console.error("error get battery voltage");
return "";
}
};
export { cycles_count, health, voltage };