@@ -11,6 +11,60 @@ pub mod ir;
1111pub mod qmk;
1212pub mod zmk;
1313
14+ /// Write all layers of `keyboard` as a formatted table, `cols` keys per row.
15+ ///
16+ /// If `cols` is `None`, defaults to 10 (a common 40% keyboard row width).
17+ /// Output is written to `out`; I/O errors are silently ignored (same as
18+ /// `println!`).
19+ pub fn print_layout_to ( keyboard : & ir:: Keyboard , cols : Option < usize > , out : & mut impl std:: io:: Write ) {
20+ for layer in & keyboard. layers {
21+ let _ = writeln ! ( out, "Layer {}: {}" , layer. index, layer. name) ;
22+ let labels: Vec < String > = layer. keys . iter ( ) . map ( key_label) . collect ( ) ;
23+ if labels. is_empty ( ) {
24+ let _ = writeln ! ( out) ;
25+ continue ;
26+ }
27+ let col_count = cols. unwrap_or ( 10 ) . min ( labels. len ( ) ) ;
28+ let width = labels. iter ( ) . map ( String :: len) . max ( ) . unwrap_or ( 1 ) ;
29+ for row in labels. chunks ( col_count) {
30+ let parts: Vec < String > = row. iter ( ) . map ( |l| format ! ( "{l:<width$}" ) ) . collect ( ) ;
31+ let _ = writeln ! ( out, " {}" , parts. join( " " ) ) ;
32+ }
33+ let _ = writeln ! ( out) ;
34+ }
35+ }
36+
37+ /// Print all layers of `keyboard` as a formatted table to stdout.
38+ pub fn print_layout ( keyboard : & ir:: Keyboard , cols : Option < usize > ) {
39+ print_layout_to ( keyboard, cols, & mut std:: io:: stdout ( ) ) ;
40+ }
41+
42+ fn key_label ( key : & ir:: Key ) -> String {
43+ match key {
44+ ir:: Key :: Kp ( e) => e. to_string ( ) ,
45+ ir:: Key :: Mo ( n) => format ! ( "MO({n})" ) ,
46+ ir:: Key :: Lt ( n, e) => format ! ( "LT({n},{e})" ) ,
47+ ir:: Key :: Mt ( m, e) => format ! ( "MT({m},{e})" ) ,
48+ ir:: Key :: Tog ( n) => format ! ( "TG({n})" ) ,
49+ ir:: Key :: Sk ( m) => format ! ( "SK({m})" ) ,
50+ ir:: Key :: Sl ( n) => format ! ( "SL({n})" ) ,
51+ ir:: Key :: To ( n) => format ! ( "TO({n})" ) ,
52+ ir:: Key :: Df ( n) => format ! ( "DF({n})" ) ,
53+ ir:: Key :: Mmv ( m) => m. to_string ( ) ,
54+ ir:: Key :: Mkp ( b) => b. to_string ( ) ,
55+ ir:: Key :: Msc ( s) => s. to_string ( ) ,
56+ ir:: Key :: Trans => "_____" . to_string ( ) ,
57+ ir:: Key :: None => "XXXXX" . to_string ( ) ,
58+ ir:: Key :: CapsWord => "CAPS_WORD" . to_string ( ) ,
59+ ir:: Key :: Bootloader => "BOOT" . to_string ( ) ,
60+ ir:: Key :: SysReset => "RESET" . to_string ( ) ,
61+ ir:: Key :: RgbUg ( a) => a. to_string ( ) ,
62+ ir:: Key :: Macro ( name) => format ! ( "M({name})" ) ,
63+ ir:: Key :: TapDance ( n) => format ! ( "TD({n})" ) ,
64+ ir:: Key :: Unknown ( s) => format ! ( "?({s})" ) ,
65+ }
66+ }
67+
1468/// Print a stderr warning for every `Key::Unknown` in the parsed keyboard.
1569pub fn warn_unknowns ( keyboard : & ir:: Keyboard ) {
1670 for layer in & keyboard. layers {
0 commit comments