Skip to main content

rusty_rich/
scope.rs

1//! Variable scope inspection and rendering.
2
3use crate::console::Console;
4use crate::panel::Panel;
5use crate::style::Style;
6use crate::table::Table;
7use crate::text::Text;
8
9/// Render a variable scope (name → value mapping) as a table.
10pub fn render_scope(
11    variables: &[(&str, &dyn std::fmt::Display)],
12    title: Option<&str>,
13    console: &mut Console,
14) {
15    let mut table = Table::new();
16    table.add_column(crate::table::Column::new("Variable"));
17    table.add_column(crate::table::Column::new("Value"));
18    table.add_column(crate::table::Column::new("Type"));
19
20    for (name, value) in variables {
21        let type_name = std::any::type_name_of_val(value);
22        table.add_row_str(vec![
23            name.to_string(),
24            value.to_string(),
25            type_name.to_string(),
26        ]);
27    }
28
29    let styled_table = table;
30    if let Some(t) = title {
31        let panel = Panel::new(styled_table)
32            .title(t)
33            .border_style(Style::new().color(crate::color::Color::parse("blue").unwrap()));
34        console.println(&panel);
35    } else {
36        console.println(&styled_table);
37    }
38}
39
40/// Create a scope summary as Text.
41pub fn scope_summary(variables: &[(&str, &dyn std::fmt::Display)]) -> Text {
42    let mut text = Text::new("");
43    for (i, (name, value)) in variables.iter().enumerate() {
44        if i > 0 {
45            text.plain.push('\n');
46        }
47        text.append_styled(
48            format!("  {}: ", name),
49            Style::new()
50                .bold(true)
51                .color(crate::color::Color::parse("cyan").unwrap()),
52        );
53        text.append_styled(value.to_string(), Style::new());
54    }
55    text
56}