use newt_core::agentic::{render_markdown, MarkdownStreamWriter, RenderOpts};
use std::io::Write;
const SAMPLE: &str = r#"# Newt Markdown renderer
A quick tour of what the **plain-scroller** renderer does — *inline* emphasis,
~~strikethrough~~, `inline code`, and a [link](https://example.com) all reflow
to the terminal width without any widget surface.
## Lists
- top-level bullet
- another, with **bold** inside
- nested item one
- nested item two
- [x] a finished task
- [ ] a pending task
1. first ordered
2. second ordered
## Blockquote
> The instrument is not the thing being observed.
> Dim bars, wrapped to width.
## Code
```rust
fn main() {
println!("dim, inset, never reflowed");
}
```
## Table
| Crate | Command | Status |
| :------ | :-----: | -----: |
| cargo | build | ok |
| just | check | green |
| 日本語テスト | cjk | wide |
---
That `---` above is a thematic break.
"#;
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
let raw = args.iter().any(|a| a == "--raw");
let path = args.iter().find(|a| !a.starts_with('-'));
let src = match path {
Some(p) => std::fs::read_to_string(p).unwrap_or_else(|e| {
eprintln!("render_md: cannot read {p}: {e}");
std::process::exit(1);
}),
None => SAMPLE.to_string(),
};
if raw {
print!("{src}");
return;
}
let cols = crossterm::terminal::size()
.map(|(c, _)| c as usize)
.unwrap_or(80)
.max(20);
if args.iter().any(|a| a == "--stream") {
stream_demo(&src, cols);
return;
}
println!("\n▸ (rendered at {cols} cols)\n");
println!(
"{}",
render_markdown(&src, RenderOpts { color: true, cols })
);
println!();
}
fn stream_demo(src: &str, cols: usize) {
let mut stdout = std::io::stdout();
print!("\n▸ ");
stdout.flush().ok();
let mut w = MarkdownStreamWriter::new(&mut stdout, RenderOpts { color: true, cols });
let mut chunk = String::new();
for ch in src.chars() {
chunk.push(ch);
if ch == ' ' || ch == '\n' {
w.push(&chunk).ok();
chunk.clear();
std::thread::sleep(std::time::Duration::from_millis(18));
}
}
w.push(&chunk).ok();
w.finish().ok();
println!();
}