summaryrefslogtreecommitdiff
path: root/lib/battery/cli.vala
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-09-17 15:46:06 +0000
committerAylur <[email protected]>2024-09-17 15:46:06 +0000
commit1798245134406e91450c66f7d49bda1d987f4e2b (patch)
tree0335656af0067d47cb26ff28e1bc440b8239ab19 /lib/battery/cli.vala
parent21cd759d61073066fee9b5a22f14c5fadb9d5c8e (diff)
battery: use json-glib for cli
Diffstat (limited to 'lib/battery/cli.vala')
-rw-r--r--lib/battery/cli.vala30
1 files changed, 3 insertions, 27 deletions
diff --git a/lib/battery/cli.vala b/lib/battery/cli.vala
index 710edec..c51264b 100644
--- a/lib/battery/cli.vala
+++ b/lib/battery/cli.vala
@@ -37,38 +37,14 @@ int main(string[] argv) {
}
var battery = AstalBattery.get_default();
- print("%s\n", to_json(battery));
+ print("%s\n", Json.gobject_to_data(battery, null));
if (monitor) {
- battery.notify.connect((prop) => {
- if (prop.get_name() == "percentage"
- || prop.get_name() == "state"
- || prop.get_name() == "icon-name"
- || prop.get_name() == "time-to-full"
- || prop.get_name() == "time-to-empty"
- ) {
- print("%s\n", to_json(battery));
- }
+ battery.notify.connect(() => {
+ print("%s\n", Json.gobject_to_data(battery, null));
});
new GLib.MainLoop(null, false).run();
}
return 0;
}
-
-private string to_json(AstalBattery.Device device) {
- string s = "unknown";
- if (device.state == AstalBattery.State.CHARGING)
- s = "charging";
- if (device.state == AstalBattery.State.DISCHARGING)
- s = "discharging";
- if (device.state == AstalBattery.State.FULLY_CHARGED)
- s = "fully_charged";
-
- var p = device.percentage;
- var i = device.icon_name;
- var r = device.state == AstalBattery.State.CHARGING
- ? device.time_to_full : device.time_to_empty;
-
- return "{ \"percentage\": %f, \"state\": \"%s\", \"icon_name\": \"%s\", \"time_remaining\": %f }".printf(p, s, i, r);
-}