Skip to content

Commit 1798245

Browse files
committed
battery: use json-glib for cli
1 parent 21cd759 commit 1798245

File tree

5 files changed

+9
-32
lines changed

5 files changed

+9
-32
lines changed

docs/libraries/battery.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Library and CLI tool for monitoring [upowerd](https://upower.freedesktop.org/) d
99
:::code-group
1010

1111
```sh [<i class="devicon-archlinux-plain"></i> Arch]
12-
sudo pacman -Syu meson vala gobject-introspection
12+
sudo pacman -Syu meson vala json-glib gobject-introspection
1313
```
1414

1515
```sh [<i class="devicon-fedora-plain"></i> Fedora]
16-
sudo dnf install meson gcc valac gobject-introspection-devel
16+
sudo dnf install meson gcc valac json-glib-devel gobject-introspection-devel
1717
```
1818

1919
```sh [<i class="devicon-ubuntu-plain"></i> Ubuntu]
20-
sudo apt install meson valac gobject-introspection
20+
sudo apt install meson valac libjson-glib-dev gobject-introspection
2121
```
2222

2323
:::

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
astal = mkPkg "astal" ./core [gtk3 gtk-layer-shell];
4848
apps = mkPkg "astal-apps" ./lib/apps [json-glib];
4949
auth = mkPkg "astal-auth" ./lib/auth [pam];
50-
battery = mkPkg "astal-battery" ./lib/battery [];
50+
battery = mkPkg "astal-battery" ./lib/battery [json-glib];
5151
bluetooth = mkPkg "astal-bluetooth" ./lib/bluetooth [];
5252
hyprland = mkPkg "astal-hyprland" ./lib/hyprland [json-glib];
5353
mpris = mkPkg "astal-mpris" ./lib/mpris [gvfs json-glib];

lib/battery/cli.vala

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,14 @@ int main(string[] argv) {
3737
}
3838

3939
var battery = AstalBattery.get_default();
40-
print("%s\n", to_json(battery));
40+
print("%s\n", Json.gobject_to_data(battery, null));
4141

4242
if (monitor) {
43-
battery.notify.connect((prop) => {
44-
if (prop.get_name() == "percentage"
45-
|| prop.get_name() == "state"
46-
|| prop.get_name() == "icon-name"
47-
|| prop.get_name() == "time-to-full"
48-
|| prop.get_name() == "time-to-empty"
49-
) {
50-
print("%s\n", to_json(battery));
51-
}
43+
battery.notify.connect(() => {
44+
print("%s\n", Json.gobject_to_data(battery, null));
5245
});
5346
new GLib.MainLoop(null, false).run();
5447
}
5548

5649
return 0;
5750
}
58-
59-
private string to_json(AstalBattery.Device device) {
60-
string s = "unknown";
61-
if (device.state == AstalBattery.State.CHARGING)
62-
s = "charging";
63-
if (device.state == AstalBattery.State.DISCHARGING)
64-
s = "discharging";
65-
if (device.state == AstalBattery.State.FULLY_CHARGED)
66-
s = "fully_charged";
67-
68-
var p = device.percentage;
69-
var i = device.icon_name;
70-
var r = device.state == AstalBattery.State.CHARGING
71-
? device.time_to_full : device.time_to_empty;
72-
73-
return "{ \"percentage\": %f, \"state\": \"%s\", \"icon_name\": \"%s\", \"time_remaining\": %f }".printf(p, s, i, r);
74-
}

lib/battery/device.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class Device : Object {
105105
battery_icon_name = "battery-level-100-charged";
106106
} else {
107107
var state = charging ? "-charging" : "";
108-
var level = (int)Math.round(percentage * 100);
108+
var level = (int)Math.round(percentage * 10)*10;
109109
battery_icon_name = @"battery-level-$level$state-symbolic";
110110
}
111111

lib/battery/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pkgconfig_deps = [
3636
dependency('glib-2.0'),
3737
dependency('gio-2.0'),
3838
dependency('gobject-2.0'),
39+
dependency('json-glib-1.0'),
3940
]
4041

4142
deps = pkgconfig_deps + meson.get_compiler('c').find_library('m')

0 commit comments

Comments
 (0)