summary refs log tree commit diff stats
path: root/rust/format/src/main.rs
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-29 23:57:32 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-29 23:57:32 +0100
commite0ca2d19231c50543a6dc6bcc6335a3f9a84c595 (patch)
treeaf4a581ebb6aacb456ea493a16a9934e01b5dcef /rust/format/src/main.rs
parentfix(src): Add some default stuff (diff)
downloadqmk_layout-e0ca2d19231c50543a6dc6bcc6335a3f9a84c595.zip
feat(src): Make usable
Diffstat (limited to 'rust/format/src/main.rs')
-rw-r--r--rust/format/src/main.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/rust/format/src/main.rs b/rust/format/src/main.rs
new file mode 100644
index 0000000..3554087
--- /dev/null
+++ b/rust/format/src/main.rs
@@ -0,0 +1,55 @@
+use std::{env::args, fs::File, io::Read};
+
+mod format_layer;
+
+fn main() {
+    let mut keymap_h = String::new();
+
+    File::open(args().skip(1).last().expect("Exists"))
+        .expect("Should work")
+        .read_to_string(&mut keymap_h)
+        .expect("Failed to read keymap_h");
+
+    let mut c_column_max = [0; 14];
+
+    let out = calculate(&mut c_column_max, keymap_h);
+    let output = calculate(&mut c_column_max, out.join("\n"));
+
+    print!("{}", output.join("\n"));
+}
+
+fn calculate(c_column_max: &mut [usize; 14], keymap_h: String) -> Vec<String> {
+    let mut output: Vec<String> = vec![];
+    let mut c_layer: Vec<String> = vec![];
+    keymap_h
+        .lines()
+        .filter(|line| !line.trim().is_empty())
+        .for_each(|line| {
+            let first_char = line.trim().chars().take(1).last().unwrap();
+            let line = line.trim();
+
+            match first_char {
+                'c' | '}' => {
+                    // Start or end of the kemap def, leave it alone
+                    output.push(line.to_owned());
+                }
+                '[' => {
+                    // Start of a new layer
+                    assert!(c_layer.is_empty(), "No new layer, without empty");
+                    output.push(format!("  {}", line));
+                }
+                ')' => {
+                    // End of a layer
+                    output.push(format_layer::format_layer(c_layer.join("\n"), c_column_max));
+                    c_layer.clear();
+
+                    output.push(format!("  {}", line));
+                }
+                _ => {
+                    // We are in a layer
+                    c_layer.push(line.to_owned());
+                }
+            }
+        });
+    output
+}