summaryrefslogtreecommitdiff
path: root/powerprofiles/src/cli.vala
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-09-01 03:23:22 +0200
committerAylur <[email protected]>2024-09-01 03:23:22 +0200
commit054cbb9a46fffecc854f2c1fbd59f8d011305bad (patch)
treed9932ce020b3f3d22b506b0cfd045c9ca9af4e1d /powerprofiles/src/cli.vala
parent9573f14c3e6dbfadbf443936e8f6375c48bfa8c3 (diff)
parent10f67b0be2b8888b7e7e395be837551c66a28a1a (diff)
merge powerprofiles
Diffstat (limited to 'powerprofiles/src/cli.vala')
-rw-r--r--powerprofiles/src/cli.vala80
1 files changed, 80 insertions, 0 deletions
diff --git a/powerprofiles/src/cli.vala b/powerprofiles/src/cli.vala
new file mode 100644
index 0000000..7be01d2
--- /dev/null
+++ b/powerprofiles/src/cli.vala
@@ -0,0 +1,80 @@
+static bool help;
+static bool version;
+static bool daemonize;
+static bool list;
+static string set;
+
+const OptionEntry[] options = {
+ { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, null, null },
+ { "help", 'h', OptionFlags.NONE, OptionArg.NONE, ref help, null, null },
+ { "daemonize", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, null, null },
+ { "list", 'l', OptionFlags.NONE, OptionArg.NONE, ref list, null, null },
+ { "set", 's', OptionFlags.NONE, OptionArg.STRING, ref set, null, null },
+ { null },
+};
+
+int main(string[] argv) {
+ try {
+ var opts = new OptionContext();
+ opts.add_main_entries(options, null);
+ opts.set_help_enabled(false);
+ opts.set_ignore_unknown_options(false);
+ opts.parse(ref argv);
+ } catch (OptionError err) {
+ printerr (err.message);
+ return 1;
+ }
+
+ if (help) {
+ print("Usage:\n");
+ print(" %s [flags]\n\n", argv[0]);
+ print("Flags:\n");
+ print(" -h, --help Print this help and exit\n");
+ print(" -v, --version Print version number and exit\n");
+ print(" -d, --daemonize Monitor for changes\n");
+ print(" -l, --list List available profiles\n");
+ return 0;
+ }
+
+ if (version) {
+ print(AstalPowerProfiles.VERSION);
+ return 0;
+ }
+
+ var profiles = AstalPowerProfiles.get_default();
+ if (set != null) {
+ profiles.active_profile = set;
+ }
+
+ else if (list) {
+ foreach (var p in profiles.profiles) {
+ print("%s\n", p.profile);
+ }
+ return 0;
+ }
+
+ if (daemonize) {
+ var loop = new MainLoop();
+
+ stdout.printf("%s\n", profiles.to_json_string());
+ stdout.flush();
+
+ profiles.notify.connect(() => {
+ stdout.printf("%s\n", profiles.to_json_string());
+ stdout.flush();
+ });
+
+ profiles.profile_released.connect(() => {
+ stdout.printf("%s\n", profiles.to_json_string());
+ stdout.flush();
+ });
+
+ loop.run();
+ }
+
+ if (set == null && !daemonize) {
+ stdout.printf("%s\n", profiles.to_json_string());
+ }
+
+ return 0;
+}