Skip to main content

streaming_demo/
streaming_demo.rs

1use smart_markdown::{StreamRenderer, ThemeMode};
2use std::io::{self, Write};
3use std::thread;
4use std::time::Duration;
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let mut sr = StreamRenderer::new(80, ThemeMode::Dark);
8
9    println!(
10        "Streaming Markdown Demo with Table Updates\n----------------------------------------\n"
11    );
12
13    // Stream some headings and text
14    for line in sr.push("# Live Data Dashboard\n\n") {
15        println!("{line}");
16    }
17    io::stdout().flush()?;
18    thread::sleep(Duration::from_millis(200));
19
20    for line in sr.push("## System Metrics\n\n") {
21        println!("{line}");
22    }
23    io::stdout().flush()?;
24    thread::sleep(Duration::from_millis(200));
25
26    // Stream table header and separator
27    for line in sr.push("| Component | Status | Value | Trend |\n") {
28        println!("{line}");
29    }
30    io::stdout().flush()?;
31    thread::sleep(Duration::from_millis(200));
32
33    for line in sr.push("|-----------|--------|-------|-------|\n") {
34        println!("{line}");
35    }
36    io::stdout().flush()?;
37    thread::sleep(Duration::from_millis(200));
38
39    // Stream first data row
40    let lines = sr.push("| CPU Usage | Normal | 42%   | →     |\n");
41    for line in lines {
42        println!("{line}");
43    }
44    io::stdout().flush()?;
45    thread::sleep(Duration::from_millis(500));
46
47    // Stream second data row
48    let lines = sr.push("| Memory    | Normal | 67%   | ↗     |\n");
49    for line in lines {
50        println!("{line}");
51    }
52    io::stdout().flush()?;
53    thread::sleep(Duration::from_millis(500));
54
55    // Stream third data row
56    let lines = sr.push("| Disk I/O  | High   | 89%   | ↗↗    |\n");
57    for line in lines {
58        println!("{line}");
59    }
60    io::stdout().flush()?;
61    thread::sleep(Duration::from_millis(500));
62
63    // Add some text after the table
64    for line in sr.push("\n### Alerts\n\n") {
65        println!("{line}");
66    }
67    io::stdout().flush()?;
68    thread::sleep(Duration::from_millis(200));
69
70    // Stream a list
71    for line in sr.push("- High disk I/O detected\n") {
72        println!("{line}");
73    }
74    io::stdout().flush()?;
75    thread::sleep(Duration::from_millis(200));
76
77    for line in sr.push("- Memory usage trending upward\n") {
78        println!("{line}");
79    }
80    io::stdout().flush()?;
81    thread::sleep(Duration::from_millis(200));
82
83    // Flush any remaining content
84    for line in sr.flush_remaining() {
85        println!("{line}");
86    }
87
88    println!();
89    Ok(())
90}