Skip to main content

basic/
basic.rs

1//! Basic example demonstrating ascii-petgraph usage.
2
3use ascii_petgraph::{BoxBorder, RenderedGraph};
4use petgraph::graph::DiGraph;
5use ratatui::style::Color;
6
7fn main() {
8    // Create a simple state machine graph
9    let mut graph: DiGraph<&str, &str> = DiGraph::new();
10
11    let start = graph.add_node("Start");
12    let loading = graph.add_node("Loading");
13    let ready = graph.add_node("Ready");
14    let error = graph.add_node("Error");
15    let done = graph.add_node("Done");
16
17    graph.add_edge(start, loading, "init");
18    graph.add_edge(loading, ready, "success");
19    graph.add_edge(loading, error, "fail");
20    graph.add_edge(ready, done, "complete");
21    graph.add_edge(error, loading, "retry");
22    graph.add_edge(error, done, "abort");
23
24    // Create rendered graph with custom settings
25    let mut rendered = RenderedGraph::builder()
26        .graph(graph)
27        .border_style(BoxBorder::Rounded)
28        .gravity(1.0)
29        .build();
30
31    // Run physics simulation
32    println!("Running physics simulation...");
33    rendered.run_simulation();
34    println!("Converged after {} iterations", rendered.iterations());
35
36    // Customize colors
37    rendered.set_node_border_color(start, Color::Green);
38    rendered.set_node_border_color(error, Color::Red);
39    rendered.set_node_text_color(error, Color::Red);
40    rendered.set_node_border_color(done, Color::Blue);
41
42    // Render to grid and print
43    let grid = rendered.render_to_grid();
44    let (width, height) = grid.size();
45
46    println!("\nRendered graph ({}x{}):\n", width, height);
47
48    // Print grid using the built-in print method
49    grid.print();
50}