managing_grid/
managing_grid.rs

1use grid_engine::grid_engine::GridEngine;
2
3#[derive(Clone, Debug)]
4struct GridContent {
5    id: String,
6}
7
8impl Default for GridContent {
9    fn default() -> Self {
10        GridContent {
11            id: "0".to_string(),
12        }
13    }
14}
15
16impl std::fmt::Display for GridContent {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "{}", self.id)
19    }
20}
21
22fn print_grid(grid: &GridEngine) {
23    let mut grid_str_formatted = String::new();
24    grid_str_formatted.push_str("  ");
25    for i in 0..grid.get_inner_grid().cols() {
26        grid_str_formatted.push_str(&format!(" {} ", i));
27    }
28    grid_str_formatted.push('\n');
29
30    grid.get_inner_grid()
31        .iter_rows()
32        .enumerate()
33        .for_each(|(row_number, row)| {
34            row.enumerate().for_each(|(index, cell)| {
35                if index == 0 {
36                    grid_str_formatted.push_str(&format!("{:0>2}", row_number));
37                }
38                match cell {
39                    Some(item) => {
40                        grid_str_formatted.push_str(&format!("[{}]", item));
41                    }
42                    None => {
43                        grid_str_formatted.push_str(&format!("[{}]", " "));
44                    }
45                };
46            });
47            grid_str_formatted.push('\n');
48        });
49
50    println!("{}", grid_str_formatted);
51}
52
53fn main() {
54    println!("Grid App");
55
56    let mut grid = GridEngine::new(10, 12);
57
58    grid.events_mut().add_changes_listener(Box::new(|event| {
59        println!("Event triggered: {:#?}", event);
60    }));
61
62    grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63    print_grid(&grid);
64    grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65    print_grid(&grid);
66    grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67    print_grid(&grid);
68    grid.remove_item("b").unwrap();
69    print_grid(&grid);
70    grid.move_item("a", 1, 0).unwrap();
71    print_grid(&grid);
72}