blob: 9c167eb6dfe759f491fba56c6f2263d276c775a3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
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 lines: Vec<_> = keymap_h.lines().collect();
if lines[0] == "// clang-format off" {
lines.remove(0);
}
if lines.last().expect("Exists") == &"// clang-format on" {
lines.remove(lines.len() - 1);
}
keymap_h = lines.join("\n");
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!(
"// clang-format off\n{}\n// clang-format on",
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(),
"A new layer cannot start, when the current layer is not 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
}
|