about summary refs log tree commit diff stats
path: root/pkgs/by-name/ri/river-mk-keymap/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkgs/by-name/ri/river-mk-keymap/src/main.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/pkgs/by-name/ri/river-mk-keymap/src/main.rs b/pkgs/by-name/ri/river-mk-keymap/src/main.rs
new file mode 100644
index 00000000..18c291cf
--- /dev/null
+++ b/pkgs/by-name/ri/river-mk-keymap/src/main.rs
@@ -0,0 +1,70 @@
+// nixos-config - My current NixOS configuration
+//
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// This file is part of my nixos-config.
+//
+// You should have received a copy of the License along with this program.
+// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+use std::fs;
+
+use anyhow::Context;
+use clap::Parser;
+
+pub mod cli;
+pub mod key_map;
+pub mod wayland;
+
+use crate::{cli::Args, key_map::KeyMap};
+
+fn main() -> Result<(), anyhow::Error> {
+    let args = Args::parse();
+
+    let keymap_path = &args.keymap.canonicalize().with_context(|| {
+        format!(
+            "Failed to canonicalize kepmay path: '{}'",
+            args.keymap.display()
+        )
+    })?;
+
+    let config = {
+        let keymap_file = fs::read_to_string(keymap_path).with_context(|| {
+            format!(
+                "Failed to open keymap file at: '{}'.",
+                keymap_path.display()
+            )
+        })?;
+
+        let keymap: KeyMap = keymap_file.parse().with_context(|| {
+            format!("Failed to parse keymap file at: {}", keymap_path.display())
+        })?;
+
+        keymap
+    };
+
+    match args.command {
+        cli::SubCommand::Init { dry_run } => {
+            println!("{config}");
+            for mut command in config.to_commands(keymap_path)? {
+                if dry_run {
+                    println!("{command:?}");
+                } else {
+                    let status = command
+                        .status()
+                        .with_context(|| format!("Failed to run command: '{command:?}'"))?;
+
+                    if !status.success() {
+                        eprintln!(
+                            "Command ('{command:?}') returned with non zero exit code: {status}"
+                        );
+                    }
+                }
+            }
+        }
+        cli::SubCommand::ShowHelp {} => wayland::main(config)?,
+    }
+
+    Ok(())
+}