mdv 4.2.0

Terminal Markdown Viewer
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
use assert_cmd::Command;
use std::fs;
use tempfile::NamedTempFile;

fn mdv_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("mdv"))
}

/// Build the markdown checkbox demo used across tests.
fn checkbox_markdown() -> String {
    [
        "- [ ] unchecked",
        "- [x] done",
        "- [-] canceled",
        "- [?] question",
        "- [!] important",
        "- [/] in progress",
        "- [|] alt progress",
        "- [\\] backslash state",
    ]
    .join("\n")
        + "\n"
}

fn run(args: &[&str], markdown: &str) -> String {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, markdown).unwrap();
    let mut cmd = mdv_cmd();
    cmd.arg("--no-colors");
    for arg in args {
        cmd.arg(arg);
    }
    cmd.arg(temp_file.path());
    let output = cmd.output().expect("mdv executed");
    assert!(output.status.success(), "mdv failed: {:?}", output.status);
    String::from_utf8(output.stdout).expect("stdout utf8")
}

fn run_with_colors(args: &[&str], markdown: &str) -> String {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, markdown).unwrap();
    let mut cmd = mdv_cmd();
    for arg in args {
        cmd.arg(arg);
    }
    cmd.arg(temp_file.path());
    let output = cmd.output().expect("mdv executed");
    assert!(output.status.success(), "mdv failed: {:?}", output.status);
    String::from_utf8(output.stdout).expect("stdout utf8")
}

#[test]
fn test_pretty_checkbox_square_icons() {
    let stdout = run(&["--pretty-checkbox", "square"], &checkbox_markdown());
    let icons = [
        ('\u{F0131}', "unchecked"),
        ('\u{F0132}', "done"),
        ('\u{F0375}', "canceled"),
        ('\u{F078B}', "question"),
        ('\u{F0027}', "important"),
        ('\u{F0856}', "in progress"),
        ('\u{F0856}', "alt progress"),
        ('\u{F0856}', "backslash state"),
    ];
    for (icon, label) in icons {
        let line = stdout
            .lines()
            .find(|l| l.contains(label))
            .unwrap_or_else(|| panic!("missing line for {label}"));
        assert!(
            line.contains(icon),
            "square icon {icon:?} not rendered for [{label}] in line: {line:?}"
        );
    }
}

#[test]
fn test_pretty_checkbox_circle_icons() {
    let stdout = run(&["--pretty-checkbox", "circle"], &checkbox_markdown());
    let expected = [
        ('\u{F0130}', "unchecked"),
        ('\u{F0133}', "done"),
        ('\u{F0376}', "canceled"),
        ('\u{F02D7}', "question"),
        ('\u{F0028}', "important"),
        ('\u{F0AA2}', "in progress"),
        ('\u{F0AA1}', "alt progress"),
        ('\u{F0AA0}', "backslash state"),
    ];
    for (icon, label) in expected {
        let line = stdout
            .lines()
            .find(|l| l.contains(label))
            .unwrap_or_else(|| panic!("missing line for {label}"));
        assert!(
            line.contains(icon),
            "circle icon not rendered for [{label}] in line: {line:?}"
        );
    }
}

#[test]
fn test_custom_checkbox_overrides_default() {
    // Override the unchecked icon and confirm it replaces the default.
    let md = "- [ ] overridden\n";
    let stdout = run(
        &[
            "--pretty-checkbox",
            "square",
            "--custom-checkbox",
            " :\u{F0026}",
        ],
        md,
    );
    let line = stdout.lines().find(|l| l.contains("overridden")).unwrap();
    assert!(line.contains('\u{F0026}'), "override not applied: {line:?}");
    // The default square unchecked icon must NOT appear anymore.
    assert!(!line.contains('\u{F0130}'), "default icon leaked: {line:?}");
}

#[test]
fn test_custom_checkbox_adds_new_state() {
    let md = "- [*] starred\n";
    let stdout = run(
        &[
            "--pretty-checkbox",
            "square",
            "--custom-checkbox",
            "*:\u{F078B}",
        ],
        md,
    );
    let line = stdout.lines().find(|l| l.contains("starred")).unwrap();
    assert!(
        line.contains('\u{F078B}'),
        "new state not rendered: {line:?}"
    );
}

#[test]
fn test_custom_checkbox_ignored_without_pretty() {
    // Without --pretty-checkbox, custom overrides must have no effect:
    // `[*]` stays a literal marker, `[x]` stays `[✓]`.
    let md = "- [*] starred\n- [x] done\n";
    let stdout = run(&["--custom-checkbox", "*:\u{F078B}"], md);
    let starred = stdout.lines().find(|l| l.contains("starred")).unwrap();
    assert!(
        starred.contains("[*]"),
        "custom state should stay literal without pretty mode: {starred:?}"
    );
    let done = stdout.lines().find(|l| l.contains("done")).unwrap();
    assert!(
        done.contains("[✓]"),
        "default checked marker changed: {done:?}"
    );
}

#[test]
fn test_backslash_checkbox_both_writings() {
    // Both `- [\]` (single backslash) and `- [\\]` (escaped) must render the icon.
    let md = "- [\\] single\n- [\\\\] double\n";
    let stdout = run(&["--pretty-checkbox", "square"], md);
    let single = stdout.lines().find(|l| l.contains("single")).unwrap();
    let double = stdout.lines().find(|l| l.contains("double")).unwrap();
    assert!(
        single.contains('\u{F0856}'),
        "single backslash not normalized: {single:?}"
    );
    assert!(
        double.contains('\u{F0856}'),
        "double backslash not rendered: {double:?}"
    );
}

#[test]
fn test_default_checkbox_unchanged_without_pretty() {
    // Backward compatibility: no flag -> `[ ]`, `[✓]`, literal `[c]`.
    let stdout = run(&[], &checkbox_markdown());
    let unchecked = stdout.lines().find(|l| l.contains("unchecked")).unwrap();
    assert!(unchecked.contains("[ ]"));
    let done = stdout.lines().find(|l| l.contains("done")).unwrap();
    assert!(done.contains("[✓]"));
    let canceled = stdout.lines().find(|l| l.contains("canceled")).unwrap();
    assert!(canceled.contains("[-]"));
}

#[test]
fn test_custom_checkbox_color_override() {
    // Color override: yellow for [*], custom RGB for [!]
    let md = "- [*] starred\n- [!] important\n";
    let stdout = run_with_colors(
        &[
            "--pretty-checkbox",
            "square",
            "--custom-checkbox",
            "*:\u{F078B}:yellow;!:\u{F0027}:128,1,1",
        ],
        md,
    );
    let starred = stdout.lines().find(|l| l.contains("starred")).unwrap();
    assert!(
        starred.contains("\x1b[33m")
            || starred.contains("\x1b[93m")
            || starred.contains("\x1b[38;5;3m"),
        "yellow color not applied to [*]: {starred:?}"
    );

    let important = stdout.lines().find(|l| l.contains("important")).unwrap();
    // RGB 128,1,1 = 38;2;128;1;1
    assert!(
        important.contains("\x1b[38;2;128;1;1m"),
        "RGB color not applied to [!]: {important:?}"
    );
}

#[test]
fn test_custom_checkbox_hex_color() {
    let md = "- [ ] test\n";
    let stdout = run_with_colors(
        &[
            "--pretty-checkbox",
            "square",
            "--custom-checkbox",
            " :\u{F0131}:#ff5500",
        ],
        md,
    );
    let line = stdout.lines().find(|l| l.contains("test")).unwrap();
    assert!(
        line.contains("\x1b[38;2;255;85;0m"),
        "hex color not applied: {line:?}"
    );
}

#[test]
fn test_custom_checkbox_no_color_still_works() {
    // Without color part, should still render the icon with default color
    let md = "- [*] starred\n";
    let stdout = run(
        &[
            "--pretty-checkbox",
            "square",
            "--custom-checkbox",
            "*:\u{F078B}",
        ],
        md,
    );
    let starred = stdout.lines().find(|l| l.contains("starred")).unwrap();
    assert!(
        starred.contains('\u{F078B}'),
        "icon without color should still render: {starred:?}"
    );
}

#[test]
fn test_pretty_checkbox_nested_indent() {
    // Nested checkboxes must preserve list-level indentation.
    let md = "- [ ] top\n  - [x] child\n    - [-] deep\n  - [?] back\n";
    let stdout = run(&["--pretty-checkbox", "square"], md);
    let lines: Vec<&str> = stdout.lines().collect();

    // top: 1 leading space (content_indent=0, list level 0, but icon replaces "- ")
    let top = lines.iter().find(|l| l.contains("top")).unwrap();
    let top_indent = top.len() - top.trim_start().len();
    // child: 3 leading spaces (list level 1 = 2 spaces + icon shift)
    let child = lines.iter().find(|l| l.contains("child")).unwrap();
    let child_indent = child.len() - child.trim_start().len();
    // deep: 5 leading spaces (list level 2 = 4 spaces + icon shift)
    let deep = lines.iter().find(|l| l.contains("deep")).unwrap();
    let deep_indent = deep.len() - deep.trim_start().len();

    assert!(
        top_indent < child_indent,
        "child should be more indented than top: top={top_indent} child={child_indent}"
    );
    assert!(
        child_indent < deep_indent,
        "deep should be more indented than child: child={child_indent} deep={deep_indent}"
    );
    // back should be at same level as child (list level 1)
    let back = lines.iter().find(|l| l.contains("back")).unwrap();
    let back_indent = back.len() - back.trim_start().len();
    assert_eq!(
        back_indent, child_indent,
        "back should match child indent: child={child_indent} back={back_indent}"
    );
}

#[test]
fn test_pretty_checkbox_heading_indent() {
    // Checkboxes under H2 should have +1 content indent vs H1.
    let md = "# H1\n\n- [ ] under h1\n\n## H2\n\n- [ ] under h2\n";
    let stdout = run(&["--pretty-checkbox", "square"], md);
    let h1_line = stdout.lines().find(|l| l.contains("under h1")).unwrap();
    let h2_line = stdout.lines().find(|l| l.contains("under h2")).unwrap();
    let h1_indent = h1_line.len() - h1_line.trim_start().len();
    let h2_indent = h2_line.len() - h2_line.trim_start().len();
    assert!(
        h2_indent > h1_indent,
        "H2 checkbox should be more indented than H1: h1={h1_indent} h2={h2_indent}"
    );
}

#[test]
fn test_pretty_checkbox_bullet_removed_not_regular_items() {
    // Pretty mode removes "-" only for checkbox items, not regular list items.
    let md = "- [ ] checkbox item\n- regular item\n";
    let stdout = run(&["--pretty-checkbox", "square"], md);
    let checkbox_line = stdout
        .lines()
        .find(|l| l.contains("checkbox item"))
        .unwrap();
    let regular_line = stdout.lines().find(|l| l.contains("regular item")).unwrap();
    // Checkbox line must NOT contain "- " prefix (bullet removed)
    let checkbox_stripped = checkbox_line.trim_start();
    assert!(
        !checkbox_stripped.starts_with("- "),
        "bullet should be removed for checkbox: {checkbox_line:?}"
    );
    // Regular item must still have "- " prefix
    let regular_stripped = regular_line.trim_start();
    assert!(
        regular_stripped.starts_with("- "),
        "bullet should remain for regular items: {regular_line:?}"
    );
}

#[test]
fn test_pretty_list_and_pretty_checkbox_coexist() {
    let md = "- [ ] checkbox item\n- regular item\n";
    let stdout = run(&["--pretty-list", "--pretty-checkbox", "square"], md);
    let checkbox_line = stdout
        .lines()
        .find(|l| l.contains("checkbox item"))
        .unwrap();
    let regular_line = stdout.lines().find(|l| l.contains("regular item")).unwrap();
    assert!(
        checkbox_line.contains('\u{F0131}'),
        "checkbox icon missing: {checkbox_line:?}"
    );
    assert!(
        !checkbox_line.contains('\u{F444}'),
        "pretty-list bullet icon should be stripped for checkbox item: {checkbox_line:?}"
    );
    assert!(
        regular_line.contains('\u{F444}'),
        "pretty-list bullet icon should remain for regular item: {regular_line:?}"
    );
}

#[test]
fn test_custom_list_marker_stripped_for_checkbox() {
    let md = "- [ ] checkbox\n- regular\n";
    for icon in ["*", ">>>", "* *"] {
        let marker = format!("1:{icon}");
        let stdout = run(
            &[
                "--pretty-list",
                "--custom-list",
                marker.as_str(),
                "--pretty-checkbox",
                "square",
            ],
            md,
        );
        let checkbox_line = stdout.lines().find(|l| l.contains("checkbox")).unwrap();
        let regular_line = stdout.lines().find(|l| l.contains("regular")).unwrap();
        assert!(
            !checkbox_line.contains(icon),
            "custom marker {icon:?} should be stripped for checkbox: {checkbox_line:?}"
        );
        assert!(
            regular_line.contains(icon),
            "custom marker {icon:?} should remain for regular: {regular_line:?}"
        );
    }
}

#[test]
fn test_custom_checkbox_color_only_existing_state() {
    // `?:yellow` — color-only override for existing [?], icon stays default.
    let md = "- [?] question\n";
    let stdout = run_with_colors(&["--pretty-checkbox", "square", "-B", "?:yellow"], md);
    let line = stdout.lines().find(|l| l.contains("question")).unwrap();
    // Default square [?] icon should still be present.
    assert!(
        line.contains('\u{F078B}'),
        "default icon should remain for color-only override: {line:?}"
    );
    // Yellow color should be applied.
    assert!(
        line.contains("\x1b[93m") || line.contains("\x1b[33m") || line.contains("\x1b[38;5;3m"),
        "yellow color not applied for color-only override: {line:?}"
    );
}

#[test]
fn test_custom_checkbox_color_only_new_state() {
    // `*:yellow` — new [*] state with color only, no icon specified.
    // Should use the default unchecked icon + yellow color.
    let md = "- [*] starred\n";
    let stdout = run_with_colors(&["--pretty-checkbox", "square", "-B", "*:yellow"], md);
    let line = stdout.lines().find(|l| l.contains("starred")).unwrap();
    // Should use the default unchecked icon (F0131 for square).
    assert!(
        line.contains('\u{F0131}'),
        "new state with no icon should use default unchecked icon: {line:?}"
    );
    // Yellow color should be applied.
    assert!(
        line.contains("\x1b[93m") || line.contains("\x1b[33m") || line.contains("\x1b[38;5;3m"),
        "yellow color not applied for new state: {line:?}"
    );
}

#[test]
fn test_custom_checkbox_icon_and_color_together() {
    // `*:icon:color` — full override with icon + color.
    let md = "- [*] starred\n";
    let stdout = run_with_colors(
        &["--pretty-checkbox", "square", "-B", "*:\u{F078B}:red"],
        md,
    );
    let line = stdout.lines().find(|l| l.contains("starred")).unwrap();
    assert!(
        line.contains('\u{F078B}'),
        "custom icon should be used: {line:?}"
    );
    assert!(
        line.contains("\x1b[31m") || line.contains("\x1b[91m") || line.contains("\x1b[38;5;1m"),
        "red color not applied: {line:?}"
    );
}