limner 0.2.0

A ratatui markdown renderer with image placeholders, code blocks, and styled headings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Interactive demo: tests all per-section alignment modes with edge cases.
//!
//! Run: `cargo run --example demo`
//!
//! Controls: Up/Down to scroll, Q/Esc to quit.

use std::io::stdout;

use crossterm::event::{self, Event, KeyCode};
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
use ratatui::Terminal;

use limner::{render_markdown, Alignment, MarkdownStyle};

// ── section runner ──────────────────────────────────────────────────────────

struct Section {
    label: &'static str,
    text: &'static str,
    style: MarkdownStyle,
}

fn run(
    sections: &[Section],
    width: u16,
) -> (
    Vec<Line<'static>>,
    Vec<limner::ImageInfo>,
    Vec<limner::LinkInfo>,
) {
    let mut out = Vec::new();
    let mut images = Vec::new();
    let mut links = Vec::new();

    for (i, s) in sections.iter().enumerate() {
        if i > 0 {
            // separator
            let sep = format!("── {} ", s.label);
            out.push(Line::from(Span::styled(
                sep,
                Style::new().fg(Color::Rgb(100, 100, 100)),
            )));
        }

        let result = render_markdown(s.text, &s.style, width);
        let offset = out.len();
        out.extend(result.lines);
        for img in result.images {
            images.push(limner::ImageInfo {
                line_index: img.line_index + offset,
                ..img
            });
        }
        for link in result.links {
            links.push(limner::LinkInfo {
                line_index: link.line_index + offset,
                ..link
            });
        }
    }

    (out, images, links)
}

// ── test sections ───────────────────────────────────────────────────────────

fn sections(
    width: u16,
) -> (
    Vec<Line<'static>>,
    Vec<limner::ImageInfo>,
    Vec<limner::LinkInfo>,
) {
    let base = MarkdownStyle::default();

    fn txt(s: &str) -> String {
        s.to_string()
    }

    let sections = vec![
        // 1 ── Left (default baseline) ──────────────────────────
        Section {
            label: "Left (default)",
            text: txt(
                "This paragraph is **left-aligned** (the default). It contains *italic*, \
                 `inline code`, and ~~strikethrough~~. This is just the baseline to confirm \
                 nothing broke.\n\n\
                 Short para.",
            )
            .leak(),
            style: MarkdownStyle { ..base.clone() },
        },
        // 2 ── Center ──────────────────────────────────────────
        Section {
            label: "Center",
            text: txt("# Centered Title\n\n\
                 This paragraph is **centered**. Multiple sentences should all appear \
                 centered on screen.")
            .leak(),
            style: MarkdownStyle {
                heading_1_alignment: Alignment::Center,
                paragraph_alignment: Alignment::Center,
                ..base.clone()
            },
        },
        // 3 ── Right ───────────────────────────────────────────
        Section {
            label: "Right",
            text: txt("## Right-Aligned Heading\n\n\
                 This whole paragraph hugs the right edge of the terminal. \
                 Every line should be flush right.")
            .leak(),
            style: MarkdownStyle {
                heading_2_alignment: Alignment::Right,
                paragraph_alignment: Alignment::Right,
                ..base.clone()
            },
        },
        // 4 ── Justify (multi-line) ────────────────────────────
        Section {
            label: "Justify",
            text: txt(
                "This long paragraph is justified. Every line except the last is padded with \
                 extra spaces so that both the left and right edges are perfectly aligned. \
                 This is the classic newspaper-style typesetting. The last line stays \
                 left-aligned as is conventional. Make sure this wraps to at least three or \
                 four lines so the space distribution is clearly visible.",
            )
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 5 ── Justify + inline styles ─────────────────────────
        Section {
            label: "Justify + inline styles",
            text: txt(
                "This **justified** paragraph contains *inline* styling like `bold`, \
                 *italic*, and `inline code`. The words themselves carry their own styling \
                 while the extra padding spaces between them use the base paragraph style. \
                 This sentence is specifically written so that it wraps across multiple lines.",
            )
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 6 ── Justify + edge: single word ─────────────────────
        Section {
            label: "Justify — single word (edge case)",
            text: txt("Hello").leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 7 ── Justify + edge: single short line ────────────────
        Section {
            label: "Justify — single short line (edge case)",
            text: txt("Hello world, this fits on one line.").leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 8 ── Justify + hard break ─────────────────────────────
        Section {
            label: "Justify — hard break",
            text: txt("This line has a hard break.  \
                 This is the line after the hard break. Both should be handled properly.")
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 9 ── Blockquote centered ──────────────────────────────
        Section {
            label: "Blockquote — Center",
            text: txt(
                "> This blockquote is centered. The quote indicator should appear at the \
                 start of each line.",
            )
            .leak(),
            style: MarkdownStyle {
                quote_alignment: Alignment::Center,
                ..base.clone()
            },
        },
        // 10 ── Blockquote justified + continuation indicators ──
        Section {
            label: "Blockquote — Justify",
            text: txt(
                "> This blockquote uses justified alignment. Every line except the last is \
                 padded with extra spaces to fill the full width. The quote indicator \
                 should appear at the start of every wrapped line, showing proper \
                 blockquote continuation rendering. This sentence adds more length.",
            )
            .leak(),
            style: MarkdownStyle {
                quote_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 11 ── Nested blockquote ───────────────────────────────
        Section {
            label: "Blockquote — nested",
            text: txt("> Outer level\n\
                 >> Inner level with a bit more text so we can see the double indicator\n\
                 > Back to outer.")
            .leak(),
            style: MarkdownStyle { ..base.clone() },
        },
        // 12 ── Code block centered ─────────────────────────────
        Section {
            label: "Code block — Center",
            text: txt("```rust\nfn greet() {\n    println!(\"hello\");\n}\n```").leak(),
            style: MarkdownStyle {
                code_block_alignment: Alignment::Center,
                ..base.clone()
            },
        },
        // 13 ── Code block right ────────────────────────────────
        Section {
            label: "Code block — Right",
            text: txt("```\nfn greet() {\n    println!(\"hello\");\n}\n```").leak(),
            style: MarkdownStyle {
                code_block_alignment: Alignment::Right,
                ..base.clone()
            },
        },
        // 14 ── HR centered ─────────────────────────────────────
        Section {
            label: "HR — Center",
            text: txt("---").leak(),
            style: MarkdownStyle { ..base.clone() },
        },
        // 15 ── HR right ────────────────────────────────────────
        Section {
            label: "HR — Right",
            text: txt("---").leak(),
            style: MarkdownStyle {
                hr_style: Style::new().fg(Color::Rgb(140, 140, 140)),
                ..base.clone()
            },
        },
        // 16 ── List justified ──────────────────────────────────
        Section {
            label: "List — Justify",
            text: txt(
                "1. First item with extra explanatory text so this wraps across multiple \
                 lines and shows justification behavior in list items.\n\
                 2. Second item that also wraps to demonstrate continuation alignment.",
            )
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
        // 17 ── Unordered list center ───────────────────────────
        Section {
            label: "List unordered — Center",
            text: txt("- First bullet that has some text to make it wrap.\n\
                 - Second bullet with even more content to fill the available width.")
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Center,
                ..base.clone()
            },
        },
        // 18 ── Mixed: link + image inside justified ────────────
        Section {
            label: "Justify — with link and image",
            text: txt(
                "This justified paragraph contains a [link](https://example.com) and an \
                 inline image: ![Rust logo](https://rust-lang.org/logos/rust-logo-512x512.png). \
                 Both should appear inline with proper alignment. The placeholder image \
                 text is part of the justified flow.",
            )
            .leak(),
            style: MarkdownStyle {
                paragraph_alignment: Alignment::Justify,
                ..base.clone()
            },
        },
    ];

    run(&sections, width)
}

// ── main ────────────────────────────────────────────────────────────────────

fn main() -> std::io::Result<()> {
    crossterm::terminal::enable_raw_mode()?;
    crossterm::execute!(stdout(), crossterm::terminal::EnterAlternateScreen)?;
    let mut terminal = Terminal::new(ratatui::backend::CrosstermBackend::new(stdout()))?;

    let mut scroll: u16 = 0;

    #[cfg(feature = "image-protocol")]
    let mut state = {
        use limner::render_image::Picker;
        use std::collections::HashMap;

        ImageDemoState {
            image_cache: HashMap::new(),
            protocol_cache: HashMap::new(),
            picker: Picker::from_query_stdio()
                .unwrap_or_else(|_| limner::render_image::halfblock_picker()),
        }
    };
    #[cfg(feature = "image-protocol")]
    terminal.draw(|_| {})?;

    loop {
        let size = terminal.size()?;
        let area: Rect = size.into();
        let content_width = area.width.saturating_sub(2);

        #[allow(unused_mut)]
        let (mut lines, images, links) = sections(content_width);
        let line_count = lines.len();
        scroll = scroll.min(line_count.saturating_sub(1) as u16);
        let img_count = images.len();
        let link_count = links.len();

        #[cfg(feature = "image-protocol")]
        let placements = {
            for img in &images {
                if !state.image_cache.contains_key(&img.url) {
                    let url = img.url.clone();
                    if let Some(bytes) = fetch_image(&url) {
                        use limner::render_image::img_crate;
                        if let Ok(dyn_img) = img_crate::load_from_memory(&bytes) {
                            state.image_cache.insert(url, dyn_img);
                        }
                    }
                }
            }
            let font_size = state.picker.font_size();
            limner::render_image::prepare_inline_images(
                &mut lines,
                &images,
                &state.image_cache,
                &mut state.protocol_cache,
                &state.picker,
                &font_size,
                content_width,
                10,
            )
        };

        let block = Block::default()
            .title(" limner alignment demo ")
            .borders(Borders::ALL)
            .title_bottom(format!(
                " {scroll}/{line_count} lines · {img_count} images · {link_count} links ",
            ));
        let inner = block.inner(area);

        terminal.draw(|f| {
            f.render_widget(block, area);
            f.render_widget(
                Paragraph::new(lines.clone())
                    .wrap(Wrap { trim: false })
                    .scroll((scroll, 0)),
                inner,
            );

            #[cfg(feature = "image-protocol")]
            {
                let content_top = inner.y as i32;
                let content_bottom = (inner.y + inner.height) as i32;
                for p in &placements {
                    let Some(protocol) = state.protocol_cache.get(&p.url) else {
                        continue;
                    };
                    let visual_y = if p.line_start == 0 {
                        0
                    } else {
                        let end = p.line_start.min(lines.len());
                        Paragraph::new(lines[..end].to_vec())
                            .wrap(Wrap { trim: false })
                            .line_count(inner.width)
                            .max(1) as u16
                    };
                    let y0 = content_top + visual_y as i32 - scroll as i32;
                    let y1 = y0 + p.cell_rows as i32;
                    if y0 < content_top || y1 > content_bottom {
                        continue;
                    }
                    f.render_widget(
                        limner::render_image::Image::new(protocol),
                        Rect {
                            x: inner.x,
                            y: y0 as u16,
                            width: p.cell_cols,
                            height: p.cell_rows,
                        },
                    );
                }
            }
        })?;

        if let Event::Key(key) = event::read()? {
            match key.code {
                KeyCode::Char('q') | KeyCode::Esc => break,
                KeyCode::Up => scroll = scroll.saturating_sub(1),
                KeyCode::Down => scroll = scroll.saturating_add(1),
                KeyCode::PageUp => scroll = scroll.saturating_sub(10),
                KeyCode::PageDown => scroll = scroll.saturating_add(10),
                KeyCode::Home => scroll = 0,
                KeyCode::End => scroll = line_count.saturating_sub(1) as u16,
                _ => {}
            }
        }
    }

    crossterm::terminal::disable_raw_mode()?;
    crossterm::execute!(std::io::stdout(), crossterm::terminal::LeaveAlternateScreen)?;
    Ok(())
}

#[cfg(feature = "image-protocol")]
struct ImageDemoState {
    image_cache: std::collections::HashMap<String, limner::render_image::img_crate::DynamicImage>,
    protocol_cache: std::collections::HashMap<String, limner::render_image::Protocol>,
    picker: limner::render_image::Picker,
}

#[cfg(feature = "image-protocol")]
fn fetch_image(url: &str) -> Option<Vec<u8>> {
    use std::io::Read;
    let resp = ureq::get(url)
        .set("User-Agent", "limner-demo/0.1")
        .timeout(std::time::Duration::from_secs(15))
        .call()
        .ok()?;
    let mut reader = resp.into_reader();
    let mut bytes = Vec::new();
    reader.read_to_end(&mut bytes).ok()?;
    Some(bytes)
}