1 stable release

1.0.0 May 24, 2023
0.1.10 May 24, 2023
0.1.4 Apr 25, 2023
0.0.7 May 9, 2023

#67 in Template engine

Download history 92/week @ 2023-04-21 28/week @ 2023-04-28 126/week @ 2023-05-05 9/week @ 2023-05-12 68/week @ 2023-05-19 38/week @ 2023-05-26

242 downloads per month

MIT license

61KB
1K SLoC

rtile

rtile provides a way to work with rectangular areas of text as atomic units which can be used for code generation

works with nightly build, as it is dependent on the feature 'local_key_cell_methods'

How to use

use rtile::*;
    
fn main() {
    kp!(main_tile_one, "Welcome to RTile!     ");
    tp!(main_tile_two, "     Have a great day!     ");

    kp!(main_combined_tiles, "@{main_tile_one}@{main_tile_two}");

    // apply top bottom spacing,
    kp!(
        combined_tiles_with_top_bottom_spacing,
        "@{main_top_bottom_spaces}\n@{main_combined_tiles}\n@{main_top_bottom_spaces}"
    );
    // apply left right unit spacing, now we have the main_result_tile
    kp!(
        main_result_tile,
        "@{main_left_right_spaces}@{combined_tiles_with_top_bottom_spacing}@{main_left_right_spaces}"
    );

    tp!(f1, frame_tile(gtp!(main_result_tile).unwrap(), 5, 2));

    // set new values for main_tile_one and main_tile_two, to reuse the template with different values
    kp!(
        main_tile_one,
        "Reusing the template with different values.     "
    );
    tp!(main_tile_two, "{:?}", vec![1, 2, 3, 4]);
    tp!(f2, frame_tile(gtp!(main_result_tile).unwrap(), 1, 1));

    // set new values for main_tile_one and main_tile_two, to reuse the template with different values
    kp!(
        main_tile_one,
        "{} ",
        t!(",").vjoin(&vec!["1", "2", "3", "4", "5"], true, Some(t!(",")))
    );
    tp!(main_tile_two, vec!["one", "two", "three", "four", "five"]);
    let result = t!(gtp!(main_result_tile).unwrap());
    tp!(f3, frame_tile(result.clone(), 0, 0));

    // set new values for main_tile_one and main_tile_two, to reuse the template with different values
    let dimensions = result.dimensions();
    kp!(main_tile_two, result);
    kp!(
        main_tile_one,
        k!(vec![23, 4, 5, 7, 10]
                .iter()
                .map(|x| if x % 2 == 0 { format!("Some({x})") } else { "None".to_string() })
                .collect::<Vec<String>>()) 
        + k!(vec!["  ==>   "; dimensions.1])
    );
    kp!(arrows,"\n{}\n",t!(vec!["------>"; dimensions.1]));
    tp!(f4, frame_tile(gtp!(main_result_tile).unwrap(), 5, 0));

    let result = t!(r#"
        @{f1}
        @{f2}
        @{f3} @{arrows} @{f4}
    "#);
    println!("{}", result);
}

fn set_spacing(width_spacing: usize, height_spacing: usize) {
    //set the spacing if required - maintain unit spacing, that is sufficient to create the frame
    let left_right_spaces = vec![" "; width_spacing];
    kp!(main_left_right_spaces, vec![left_right_spaces.join(""); 1]);
    let top_bottom_spaces = vec![" "; 1];
    kp!(
        main_top_bottom_spaces,
        vec![top_bottom_spaces.join(""); height_spacing]
    );
}

fn frame_tile(input: RTile, width_spacing: usize, height_spacing: usize) -> RTile {
    set_spacing(width_spacing, height_spacing);
    if height_spacing > 0 {
        // apply top bottom spacing,
        kp!(
            combined_tiles_with_top_bottom_spacing,
            "@{main_top_bottom_spaces}\n@{main_combined_tiles}\n@{main_top_bottom_spaces}"
        );
    } else {
        // remove the top bottom spacing,
        tp!(
            combined_tiles_with_top_bottom_spacing,
            "@{main_top_bottom_spaces}\n@{main_combined_tiles}\n@{main_top_bottom_spaces}"
        );
    }

    let (width, height) = kp!(frame_tile, input).dimensions();

    tp!(main_height, vec!["|"; height]);
    tp!(main_width, vec!["="; width + 2].join(""));

    t!(r#"
            @{main_width}
            @{main_height}@{frame_tile}@{main_height}
            @{main_width}
    "#)
}

Output

===================================================
|                                                 |
|                                                 |
|     Welcome to RTile!     Have a great day!     |
|                                                 |
|                                                 |
===================================================
================================================================
|                                                              |
| Reusing the template with different values.     [1, 2, 3, 4] |
|                                                              |
================================================================
==========         ====================================
|1, one  | ------> |     None      ==>   1, one       |
|2, two  | ------> |     Some(4)   ==>   2, two       |
|3, three| ------> |     None      ==>   3, three     |
|4, four | ------> |     None      ==>   4, four      |
|5, five | ------> |     Some(10)  ==>   5, five      |
==========         ====================================

No runtime deps