diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-12-29 23:57:32 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-12-29 23:57:32 +0100 |
commit | e0ca2d19231c50543a6dc6bcc6335a3f9a84c595 (patch) | |
tree | af4a581ebb6aacb456ea493a16a9934e01b5dcef /rust | |
parent | fix(src): Add some default stuff (diff) | |
download | qmk_layout-e0ca2d19231c50543a6dc6bcc6335a3f9a84c595.zip |
feat(src): Make usable
Diffstat (limited to 'rust')
-rw-r--r-- | rust/format/.gitignore | 3 | ||||
-rw-r--r-- | rust/format/Cargo.lock | 7 | ||||
-rw-r--r-- | rust/format/Cargo.toml | 70 | ||||
-rw-r--r-- | rust/format/output | 6 | ||||
-rw-r--r-- | rust/format/output2 | 6 | ||||
-rw-r--r-- | rust/format/package.nix | 27 | ||||
-rw-r--r-- | rust/format/src/format_layer/mod.rs | 209 | ||||
-rw-r--r-- | rust/format/src/format_layer/output_def.fs | 22 | ||||
-rw-r--r-- | rust/format/src/format_layer/prints.txt | 6 | ||||
-rw-r--r-- | rust/format/src/main.rs | 55 |
10 files changed, 411 insertions, 0 deletions
diff --git a/rust/format/.gitignore b/rust/format/.gitignore new file mode 100644 index 0000000..ed14267 --- /dev/null +++ b/rust/format/.gitignore @@ -0,0 +1,3 @@ +# build +/target +/result diff --git a/rust/format/Cargo.lock b/rust/format/Cargo.lock new file mode 100644 index 0000000..8d41b97 --- /dev/null +++ b/rust/format/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "format" +version = "0.1.0" diff --git a/rust/format/Cargo.toml b/rust/format/Cargo.toml new file mode 100644 index 0000000..9bf593c --- /dev/null +++ b/rust/format/Cargo.toml @@ -0,0 +1,70 @@ +[package] +name = "format" +description = "A qmk layer source code formatter" +version = "0.1.0" +edition = "2021" +license = "AGPL-3.0-or-later" + + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] + +[profile.release] +lto = true +codegen-units = 1 +panic = "abort" +split-debuginfo = "off" + +[workspace.lints.rust] +# rustc lint groups https://doc.rust-lang.org/rustc/lints/groups.html +warnings = "warn" +future_incompatible = { level = "warn", priority = -1 } +let_underscore = { level = "warn", priority = -1 } +nonstandard_style = { level = "warn", priority = -1 } +rust_2018_compatibility = { level = "warn", priority = -1 } +rust_2018_idioms = { level = "warn", priority = -1 } +rust_2021_compatibility = { level = "warn", priority = -1 } +unused = { level = "warn", priority = -1 } + +# rustc allowed-by-default lints https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html +# missing_docs = "warn" +macro_use_extern_crate = "warn" +meta_variable_misuse = "warn" +missing_abi = "warn" +missing_copy_implementations = "warn" +missing_debug_implementations = "warn" +non_ascii_idents = "warn" +noop_method_call = "warn" +single_use_lifetimes = "warn" +trivial_casts = "warn" +trivial_numeric_casts = "warn" +unreachable_pub = "warn" +unsafe_op_in_unsafe_fn = "warn" +unused_crate_dependencies = "warn" +unused_import_braces = "warn" +unused_lifetimes = "warn" +unused_qualifications = "warn" +variant_size_differences = "warn" + +[workspace.lints.rustdoc] +# rustdoc lints https://doc.rust-lang.org/rustdoc/lints.html +broken_intra_doc_links = "warn" +private_intra_doc_links = "warn" +missing_crate_level_docs = "warn" +private_doc_tests = "warn" +invalid_codeblock_attributes = "warn" +invalid_rust_codeblocks = "warn" +bare_urls = "warn" + +[workspace.lints.clippy] +# clippy allowed by default +dbg_macro = "warn" + +# clippy categories https://doc.rust-lang.org/clippy/ +all = { level = "warn", priority = -1 } +correctness = { level = "warn", priority = -1 } +suspicious = { level = "warn", priority = -1 } +style = { level = "warn", priority = -1 } +complexity = { level = "warn", priority = -1 } +perf = { level = "warn", priority = -1 } +pedantic = { level = "warn", priority = -1 } diff --git a/rust/format/output b/rust/format/output new file mode 100644 index 0000000..95f3cdd --- /dev/null +++ b/rust/format/output @@ -0,0 +1,6 @@ +KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, RV_SPAWN_NHEKO, RV_SPAWN_NEORG_FIREFOX, RV_SPAWN_KEEPASSXC, RV_SPAWN_SIGNAL, KK_TP, KK_TP, +KK_TP, RV_SCREEN_SHOT, RV_PAUSE, RV_TOGGLE_MUSIC, RV_RUN, KK_TP, KK_TP, KK_TP, RV_TOGGLE_FLOAT, RV_TOGGLE_FULLSCREEN, RV_ZOOM_VIEW, KK_TP, RV_EXIT, KK_TP, +KK_TP, KK_TP, KK_TP, RV_MOVE_VIEW_PREVIOUS_TAG, RV_MOVE_PREVIOUS_TAG, RV_MOVE_OUTPUT_NEXT, KK_TP, KK_TP, RV_VIEW_CLOSE, RV_VIEW_NEXT, RV_SWAP_NEXT, RV_SWAP_PREV, RV_VIEW_PREV, KK_TP, +KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, RV_SEND_OUTPUT, KK_TP, KK_TP, KK_TP, KK_TP, +KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, +KK_TP, KK_TP, KK_TP, KK_TP, KK_TP, KK_TP diff --git a/rust/format/output2 b/rust/format/output2 new file mode 100644 index 0000000..edc3a82 --- /dev/null +++ b/rust/format/output2 @@ -0,0 +1,6 @@ +KC_EQL, KC_1, KC_2, KC_3, KC_4, KC_5, KC_LEFT, KC_RGHT, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, +KC_DEL, KC_Q, KC_W, KC_E, KC_R, KC_T, TG(SYMB), TG(SYMB), KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSLS, +KC_BSPC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_HYPR, KC_MEH, KC_H, KC_J, KC_K, KC_L, LT(MDIA,KC_SCLN), LGUI_T(KC_QUOT), +KC_LSFT, LCTL_T(KC_Z), KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, RCTL_T(KC_SLSH), KC_RSFT, +LT(SYMB,KC_GRV), WEBUSB_PAIR, A(KC_LSFT), KC_LEFT, KC_RGHT, LALT_T(KC_APP), RCTL_T(KC_ESC), KC_UP, KC_DOWN, KC_LBRC, KC_RBRC, MO(SYMB), +KC_SPC, KC_BSPC, KC_LGUI, KC_LALT, KC_TAB, KC_ENT diff --git a/rust/format/package.nix b/rust/format/package.nix new file mode 100644 index 0000000..0595b61 --- /dev/null +++ b/rust/format/package.nix @@ -0,0 +1,27 @@ +{ + rustPlatform, + lib, +}: +rustPlatform.buildRustPackage { + pname = "format"; + version = "1.0.0"; + + src = lib.cleanSourceWith { + src = lib.cleanSource ./.; + filter = name: type: + (type == "directory") + || (builtins.elem (builtins.baseNameOf name) [ + "Cargo.toml" + "Cargo.lock" + "prints.txt" + "output_def.fs" + ]) + || (lib.strings.hasSuffix ".rs" (builtins.baseNameOf name)); + }; + + doCheck = true; + + cargoLock = { + lockFile = ./Cargo.lock; + }; +} diff --git a/rust/format/src/format_layer/mod.rs b/rust/format/src/format_layer/mod.rs new file mode 100644 index 0000000..1b93c97 --- /dev/null +++ b/rust/format/src/format_layer/mod.rs @@ -0,0 +1,209 @@ +use std::fmt::{Display, Write}; + +include!("output_def.fs"); + +macro_rules! check { + ($c1:expr, $cf:expr, $ind:expr, $ma:expr) => { + if ($cf[$ind] >= $ma) { + $c1[$ind] = $cf[$ind]; + } else { + // eprintln!("Changed {} from {} -> {}", stringify!($cf), $cf[$ind], $ma); + $c1[$ind] = $ma; + $cf[$ind] = $ma; + } + }; +} + +macro_rules! get { + ($val:expr, $column:tt, $key:tt) => { + [ + $val.row1.$key.$column.to_string(), + $val.row2.$key.$column.to_string(), + $val.row3.$key.$column.to_string(), + $val.row4.$key.$column.to_string(), + $val.row5.$key.$column.to_string(), + $val.row6.$key.$column.to_string(), + ] + .iter() + .map(|val| val.chars().count()) + .max() + .expect("Works") + }; +} + +impl Output { + fn format(&self, c_column_max: &mut [usize; 14]) -> String { + let mut columns_max = [0; 14]; + + check!(columns_max, c_column_max, 0, get! {self, 0, 0}); + check!(columns_max, c_column_max, 1, get! {self, 1, 0}); + check!(columns_max, c_column_max, 2, get! {self, 2, 0}); + check!(columns_max, c_column_max, 3, get! {self, 3, 0}); + check!(columns_max, c_column_max, 4, get! {self, 4, 0}); + check!(columns_max, c_column_max, 5, get! {self, 5, 0}); + check!(columns_max, c_column_max, 6, get! {self, 6, 0}); + check!(columns_max, c_column_max, 7, get! {self, 0, 0}); + check!(columns_max, c_column_max, 8, get! {self, 1, 1}); + check!(columns_max, c_column_max, 9, get! {self, 2, 1}); + check!(columns_max, c_column_max, 10, get! {self, 3, 1}); + check!(columns_max, c_column_max, 11, get! {self, 4, 1}); + check!(columns_max, c_column_max, 12, get! {self, 5, 1}); + check!(columns_max, c_column_max, 13, get! {self, 6, 1}); + + let mut f = String::new(); + write!( + f, + include_str!("prints.txt"), + // row 1 + self.row1.0 .0, + self.row1.0 .1, + self.row1.0 .2, + self.row1.0 .3, + self.row1.0 .4, + self.row1.0 .5, + self.row1.0 .6, + self.row1.1 .0, + self.row1.1 .1, + self.row1.1 .2, + self.row1.1 .3, + self.row1.1 .4, + self.row1.1 .5, + self.row1.1 .6, + // row 2 + self.row2.0 .0, + self.row2.0 .1, + self.row2.0 .2, + self.row2.0 .3, + self.row2.0 .4, + self.row2.0 .5, + self.row2.0 .6, + self.row2.1 .0, + self.row2.1 .1, + self.row2.1 .2, + self.row2.1 .3, + self.row2.1 .4, + self.row2.1 .5, + self.row2.1 .6, + // row 3 + self.row3.0 .0, + self.row3.0 .1, + self.row3.0 .2, + self.row3.0 .3, + self.row3.0 .4, + self.row3.0 .5, + self.row3.0 .6, + self.row3.1 .0, + self.row3.1 .1, + self.row3.1 .2, + self.row3.1 .3, + self.row3.1 .4, + self.row3.1 .5, + self.row3.1 .6, + // row 4 + self.row4.0 .0.to_string(), + self.row4.0 .1.to_string(), + self.row4.0 .2.to_string(), + self.row4.0 .3.to_string(), + self.row4.0 .4.to_string(), + self.row4.0 .5.to_string(), + self.row4.0 .6.to_string(), + self.row4.1 .0.to_string(), + self.row4.1 .1.to_string(), + self.row4.1 .2.to_string(), + self.row4.1 .3.to_string(), + self.row4.1 .4.to_string(), + self.row4.1 .5.to_string(), + self.row4.1 .6.to_string(), + // row 5 + self.row5.0 .0.to_string(), + self.row5.0 .1.to_string(), + self.row5.0 .2.to_string(), + self.row5.0 .3.to_string(), + self.row5.0 .4.to_string(), + self.row5.0 .5.to_string(), + self.row5.0 .6.to_string(), + self.row5.1 .0.to_string(), + self.row5.1 .1.to_string(), + self.row5.1 .2.to_string(), + self.row5.1 .3.to_string(), + self.row5.1 .4.to_string(), + self.row5.1 .5.to_string(), + self.row4.1 .6.to_string(), + // thumbs + self.row6.0 .0.to_string(), + self.row6.0 .1.to_string(), + self.row6.0 .2.to_string(), + self.row6.0 .3.to_string(), + self.row6.0 .4.to_string(), + self.row6.0 .5.to_string(), + self.row6.0 .6.to_string(), + self.row6.1 .0.to_string(), + self.row6.1 .1.to_string(), + self.row6.1 .2.to_string(), + self.row6.1 .3.to_string(), + self.row6.1 .4.to_string(), + self.row6.1 .5.to_string(), + self.row6.1 .6.to_string(), + col0 = columns_max[0], + col1 = columns_max[1], + col2 = columns_max[2], + col3 = columns_max[3], + col4 = columns_max[4], + col5 = columns_max[5], + col6 = columns_max[6], + col7 = columns_max[7], + col8 = columns_max[8], + col9 = columns_max[9], + col10 = columns_max[10], + col11 = columns_max[11], + col12 = columns_max[12], + col13 = columns_max[13], + ) + .expect("Works"); + f + } +} + +#[must_use] +pub fn format_layer(input: String, c_column_max: &mut [usize; 14]) -> String { + let mut a = input + .lines() + .map(|val| val.to_owned()) + .collect::<Vec<String>>() + .join(" ") + .split_whitespace() + .map(|val| val.to_owned()) + .rev() + .collect::<Vec<String>>(); + let mut n = || a.pop().expect("This should exist"); + let e = || Emt(); + + let out = Output { + row1: ( + (n(), n(), n(), n(), n(), n(), n()), + (n(), n(), n(), n(), n(), n(), n()), + ), + row2: ( + (n(), n(), n(), n(), n(), n(), n()), + (n(), n(), n(), n(), n(), n(), n()), + ), + row3: ( + (n(), n(), n(), n(), n(), n(), n()), + (n(), n(), n(), n(), n(), n(), n()), + ), + row4: ( + (n(), n(), n(), n(), n(), n(), e()), + (e(), n(), n(), n(), n(), n(), n()), + ), + row5: ( + (n(), n(), n(), n(), n(), n(), e()), + (e(), n(), n(), n(), n(), n(), n()), + ), + row6: ( + (e(), e(), e(), n(), n(), n(), e()), + (e(), n(), n(), n(), e(), e(), e()), + ), + }; + + out.format(c_column_max) +} diff --git a/rust/format/src/format_layer/output_def.fs b/rust/format/src/format_layer/output_def.fs new file mode 100644 index 0000000..c7d46b1 --- /dev/null +++ b/rust/format/src/format_layer/output_def.fs @@ -0,0 +1,22 @@ +// This file is not a `rs` file, to avoid formatting it. +type Key = String; +type Thb = Key; + +#[derive(Default, Debug)] +struct Output { + row1: ((Key, Key, Key, Key, Key, Key, Key), (Key, Key, Key, Key, Key, Key, Key)), + row2: ((Key, Key, Key, Key, Key, Key, Key), (Key, Key, Key, Key, Key, Key, Key)), + row3: ((Key, Key, Key, Key, Key, Key, Key), (Key, Key, Key, Key, Key, Key, Key)), + row4: ((Key, Key, Key, Key, Key, Key, Emt), (Emt, Key, Key, Key, Key, Key, Key)), + row5: ((Key, Key, Key, Key, Key, Thb, Emt), (Emt, Thb, Key, Key, Key, Key, Key)), + row6: ((Emt, Emt, Emt, Thb, Thb, Thb, Emt), (Emt, Thb, Thb, Thb, Emt, Emt, Emt)), +} + +#[derive(Debug, Default)] +struct Emt (); + +impl Display for Emt { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("") + } +} diff --git a/rust/format/src/format_layer/prints.txt b/rust/format/src/format_layer/prints.txt new file mode 100644 index 0000000..f67b774 --- /dev/null +++ b/rust/format/src/format_layer/prints.txt @@ -0,0 +1,6 @@ + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} + {:<col0$} {:<col1$} {:<col2$} {:<col3$} {:<col4$} {:<col5$} {:<col6$} {:<col7$} {:<col8$} {:<col9$} {:<col10$} {:<col11$} {:<col12$} {:<col13$} 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 +} |