Skip to main content

table_layouts/
table_layouts.rs

1//! Table layout variants — Full, Compact, and Stacked.
2//!
3//! Demonstrates all three layout styles for rendering tabular data in different
4//! scenarios. Full is the default; Compact minimizes spacing; Stacked shows
5//! each row as key-value pairs, ideal for narrow terminals.
6//!
7//! Run with:
8//! ```sh
9//! cargo run --example table_layouts
10//! ```
11
12use scriba::{Format, Output, Table, Ui};
13
14fn main() -> scriba::Result<()> {
15    let headers = vec!["Product".into(), "Price".into(), "Stock".into()];
16    let rows = vec![
17        vec!["Widget A".into(), "$9.99".into(), "42".into()],
18        vec!["Widget B".into(), "$14.50".into(), "18".into()],
19        vec!["Widget C".into(), "$24.99".into(), "0".into()],
20    ];
21
22    let ui = Ui::new().with_format(Format::Text);
23
24    // Full layout (default) — bordered, full width
25    println!("=== FULL LAYOUT ===");
26    println!("(Bordered, full width — best for normal terminals)\n");
27
28    let table_full = Table::new(headers.clone(), rows.clone()).with_layout_full();
29    let output_full = Output::new()
30        .heading(2, "Inventory")
31        .table(None, table_full);
32
33    ui.print(&output_full)?;
34
35    // Compact layout — minimal spacing, no borders
36    println!("\n=== COMPACT LAYOUT ===");
37    println!("(Minimal spacing, no borders — dense display)\n");
38
39    let table_compact = Table::new(headers.clone(), rows.clone()).with_layout_compact();
40    let output_compact = Output::new()
41        .heading(2, "Inventory")
42        .table(None, table_compact);
43
44    ui.print(&output_compact)?;
45
46    // Stacked layout — key-value per row
47    println!("\n=== STACKED LAYOUT ===");
48    println!("(Key-value per row — great for narrow terminals)\n");
49
50    let table_stacked = Table::new(headers.clone(), rows.clone()).with_layout_stacked();
51    let output_stacked = Output::new()
52        .heading(2, "Inventory")
53        .table(None, table_stacked);
54
55    ui.print(&output_stacked)?;
56
57    // Stacked with index
58    println!("\n=== STACKED LAYOUT WITH INDEX ===");
59    println!("(Row numbers for reference)\n");
60
61    let table_stacked_idx = Table::new(headers.clone(), rows.clone())
62        .with_index()
63        .with_layout_stacked();
64    let output_idx = Output::new()
65        .heading(2, "Inventory")
66        .table(None, table_stacked_idx);
67
68    ui.print(&output_idx)?;
69
70    // Compact with index
71    println!("\n=== COMPACT LAYOUT WITH INDEX ===");
72    println!("(Minimal display with row numbers)\n");
73
74    let table_compact_idx = Table::new(headers.clone(), rows.clone())
75        .with_index()
76        .with_layout_compact();
77    let output_compact_idx = Output::new()
78        .heading(2, "Inventory")
79        .table(None, table_compact_idx);
80
81    ui.print(&output_compact_idx)?;
82
83    Ok(())
84}