mant 0.9.0

Local-first TUI, structured CLI, and MCP server for manuals and Markdown
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Deterministic rendering of already materialized query views.

use std::fmt::Write as _;

use anstyle::{AnsiColor, Style};
use mant_engine::QueryViewResult;
use mant_ir::{
    Block, DefinitionRole, DocumentMeta, Inline, ResolvedContent, Section, SourceFormat,
};
use mant_protocol::{
    ExcerptSelection, OutlineNode, QueryExcerpt, QueryOutline, QuerySearch, ScopeQueryResponse,
    ScopeQueryResult, sanitize_terminal_text,
};
use serde::Serialize;

use crate::{arguments::QueryFormat, error::Failure};

/// Physical destination characteristics that may affect terminal safety only.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum OutputTarget {
    Stream,
    Terminal,
}

/// Complete rendering policy after command-line defaults have been resolved.
#[derive(Debug, Clone, Copy)]
pub(super) struct RenderOptions {
    pub(super) format: QueryFormat,
    pub(super) pretty: bool,
    pub(super) preserve_anchors: bool,
    pub(super) color: bool,
    pub(super) target: OutputTarget,
}

impl RenderOptions {
    const fn terminal(self) -> bool {
        matches!(self.target, OutputTarget::Terminal)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalRole {
    Document,
    Heading,
    Option,
    Command,
    Environment,
    Variable,
    Match,
    Coordinate,
    Path,
    TreeGuide,
    Muted,
}

struct TerminalText {
    value: String,
    color: bool,
}

impl TerminalText {
    fn new(color: bool) -> Self {
        Self {
            value: String::new(),
            color,
        }
    }

    fn plain(&mut self, value: &str) {
        self.value.push_str(&sanitize_terminal_text(value));
    }

    fn styled(&mut self, role: TerminalRole, value: &str) {
        let value = sanitize_terminal_text(value);
        if !self.color || value.is_empty() {
            self.plain(&value);
            return;
        }
        let style = terminal_style(role);
        let _ = write!(self.value, "{style}{value}{style:#}");
    }

    fn line(&mut self) {
        self.value.push('\n');
    }

    fn finish(self) -> String {
        self.value.trim_end().to_owned()
    }
}

fn render_terminal_outline(outline: &QueryOutline, color: bool) -> String {
    let mut output = TerminalText::new(color);
    output.styled(
        TerminalRole::Document,
        &document_label(
            &outline.label,
            outline
                .meta
                .as_ref()
                .and_then(|meta| meta.manual_section.as_deref()),
        ),
    );
    if !outline.nodes.is_empty() {
        output.line();
        render_outline_nodes(&outline.nodes, "", &mut output);
    }
    output.finish()
}

fn render_outline_nodes(nodes: &[OutlineNode], prefix: &str, output: &mut TerminalText) {
    for (index, node) in nodes.iter().enumerate() {
        let last = index + 1 == nodes.len();
        output.styled(TerminalRole::TreeGuide, prefix);
        output.styled(TerminalRole::TreeGuide, if last { "└─" } else { "├─" });
        output.plain(" ");
        output.styled(TerminalRole::Path, node.path());
        output.styled(TerminalRole::TreeGuide, " [");
        output.styled(TerminalRole::Coordinate, node.id());
        output.styled(TerminalRole::TreeGuide, "] ");
        output.styled(outline_node_role(node), node.title());
        if index + 1 < nodes.len() || !node.children().is_empty() {
            output.line();
        }
        let child_prefix = format!("{prefix}{}", if last { "  " } else { "" });
        render_outline_nodes(node.children(), &child_prefix, output);
        if !node.children().is_empty() && index + 1 < nodes.len() {
            output.line();
        }
    }
}

fn render_terminal_excerpt(excerpt: &QueryExcerpt, color: bool) -> String {
    let excerpt = terminal_excerpt(excerpt);
    let plain = mant_engine::render_excerpt_text(&excerpt);
    if !color || plain.is_empty() {
        return plain;
    }

    let mut headings = Vec::new();
    let mut terms = Vec::new();
    for selection in &excerpt.selections {
        collect_excerpt_semantics(selection, &mut headings, &mut terms);
    }
    terms.sort_by_key(|term| std::cmp::Reverse(term.0.len()));

    let mut output = TerminalText::new(true);
    for (index, line) in plain.split('\n').enumerate() {
        if index > 0 {
            output.line();
        }
        render_excerpt_line(line, index == 0, &headings, &terms, &mut output);
    }
    output.finish()
}

fn render_terminal_search(search: &QuerySearch, color: bool) -> String {
    mant_engine::render_search_text_with(search, |role, value| {
        let value = sanitize_terminal_text(value);
        if !color {
            return value.into_owned();
        }
        let role = match role {
            mant_engine::SearchTextRole::Plain => return value.into_owned(),
            mant_engine::SearchTextRole::Document => TerminalRole::Document,
            mant_engine::SearchTextRole::Coordinate => TerminalRole::Coordinate,
            mant_engine::SearchTextRole::Path => TerminalRole::Path,
            mant_engine::SearchTextRole::Heading => TerminalRole::Heading,
            mant_engine::SearchTextRole::Definition(role) => definition_role(role),
            mant_engine::SearchTextRole::Match => TerminalRole::Match,
            mant_engine::SearchTextRole::Muted => TerminalRole::Muted,
        };
        let style = terminal_style(role);
        format!("{style}{value}{style:#}")
    })
}

fn render_excerpt_line(
    line: &str,
    document_line: bool,
    headings: &[String],
    terms: &[(String, DefinitionRole)],
    output: &mut TerminalText,
) {
    if document_line {
        output.styled(TerminalRole::Document, line);
        return;
    }
    if line == "TLDR" {
        output.styled(TerminalRole::Heading, line);
        return;
    }
    if let Some(rest) = line.strip_prefix("Outline ")
        && let Some((path, breadcrumb)) = rest.split_once(": ")
    {
        output.styled(TerminalRole::Muted, "Outline ");
        output.styled(TerminalRole::Path, path);
        output.styled(TerminalRole::Muted, ": ");
        for (index, title) in breadcrumb.split(" > ").enumerate() {
            if index > 0 {
                output.styled(TerminalRole::TreeGuide, " > ");
            }
            output.styled(TerminalRole::Heading, title);
        }
        return;
    }

    let trimmed = line.trim_start();
    let indent = line.len().saturating_sub(trimmed.len());
    if headings.iter().any(|heading| heading == trimmed) {
        output.plain(&line[..indent]);
        output.styled(TerminalRole::Heading, trimmed);
        return;
    }
    if let Some((term, role)) = terms
        .iter()
        .find(|(term, _)| !term.is_empty() && trimmed.starts_with(term))
    {
        output.plain(&line[..indent]);
        output.styled(definition_role(*role), term);
        output.plain(&trimmed[term.len()..]);
        return;
    }
    output.plain(line);
}

fn collect_excerpt_semantics(
    selection: &ExcerptSelection,
    headings: &mut Vec<String>,
    terms: &mut Vec<(String, DefinitionRole)>,
) {
    match selection {
        ExcerptSelection::Tldr { .. } | ExcerptSelection::DocumentRoot { .. } => {}
        ExcerptSelection::DocumentSection { section, .. } => {
            collect_section_semantics(section, headings, terms);
        }
        ExcerptSelection::DocumentEntry { entry, .. } => collect_definition(entry, terms),
    }
}

fn collect_section_semantics(
    section: &Section,
    headings: &mut Vec<String>,
    terms: &mut Vec<(String, DefinitionRole)>,
) {
    headings.push(section.title.clone());
    collect_block_semantics(&section.blocks, terms);
    for child in &section.children {
        collect_section_semantics(child, headings, terms);
    }
}

fn collect_block_semantics(blocks: &[Block], terms: &mut Vec<(String, DefinitionRole)>) {
    for block in blocks {
        match block {
            Block::DefinitionList { items, .. } => {
                for item in items {
                    collect_definition(item, terms);
                    collect_block_semantics(&item.description, terms);
                }
            }
            Block::List { items, .. } => {
                for item in items {
                    collect_block_semantics(&item.blocks, terms);
                }
            }
            Block::Table { rows, .. } => {
                for cell in rows.iter().flat_map(|row| &row.cells) {
                    collect_block_semantics(&cell.blocks, terms);
                }
            }
            Block::Paragraph { .. }
            | Block::Preformatted { .. }
            | Block::Equation { .. }
            | Block::VerticalSpace { .. }
            | Block::ThematicBreak { .. }
            | Block::Unsupported { .. } => {}
        }
    }
}

fn collect_definition(item: &mant_ir::DefinitionItem, terms: &mut Vec<(String, DefinitionRole)>) {
    let Some(identity) = &item.identity else {
        return;
    };
    terms.extend(
        item.terms
            .iter()
            .map(|term| (inline_text(term), identity.role)),
    );
}

fn inline_text(inlines: &[Inline]) -> String {
    let mut output = String::new();
    for inline in inlines {
        match inline {
            Inline::Text { value } | Inline::Code { value } => output.push_str(value),
            Inline::Strong { children }
            | Inline::Emphasis { children }
            | Inline::Link { children, .. } => output.push_str(&inline_text(children)),
            Inline::Anchor { .. } => {}
            Inline::LineBreak => output.push('\n'),
        }
    }
    output
}

const fn outline_node_role(node: &OutlineNode) -> TerminalRole {
    match node {
        OutlineNode::DocumentEntry { role, .. } => definition_role(*role),
        OutlineNode::Tldr { .. }
        | OutlineNode::DocumentRoot { .. }
        | OutlineNode::DocumentSection { .. } => TerminalRole::Heading,
    }
}

const fn definition_role(role: DefinitionRole) -> TerminalRole {
    match role {
        DefinitionRole::Option => TerminalRole::Option,
        DefinitionRole::Command => TerminalRole::Command,
        DefinitionRole::EnvironmentVariable => TerminalRole::Environment,
        DefinitionRole::Variable => TerminalRole::Variable,
    }
}

const fn terminal_style(role: TerminalRole) -> Style {
    match role {
        TerminalRole::Document => AnsiColor::BrightBlue.on_default().bold(),
        TerminalRole::Heading => AnsiColor::BrightCyan.on_default().bold(),
        TerminalRole::Option => AnsiColor::BrightGreen.on_default().bold(),
        TerminalRole::Command | TerminalRole::Match => AnsiColor::BrightYellow.on_default().bold(),
        TerminalRole::Environment => AnsiColor::BrightCyan.on_default(),
        TerminalRole::Variable | TerminalRole::Path => AnsiColor::BrightMagenta.on_default(),
        TerminalRole::Coordinate | TerminalRole::TreeGuide | TerminalRole::Muted => {
            AnsiColor::BrightBlack.on_default()
        }
    }
}

fn document_label(document: &str, section: Option<&str>) -> String {
    section.map_or_else(
        || document.to_owned(),
        |section| format!("{document}({section})"),
    )
}

/// Copy a complete document with its terminal-visible identity made safe.
///
/// Engine text renderers produce structural newlines themselves, so sanitizing
/// their finished string would erase layout. The identity is the only
/// terminal-visible direct-input field that bypasses the parsed text-safety
/// boundary; sanitize it before rendering instead.
fn terminal_content(query: &ResolvedContent) -> ResolvedContent {
    let mut query = query.clone();
    query.label = sanitize_terminal_text(&query.label).into_owned();
    if let Some(document) = query.document.as_mut() {
        sanitize_terminal_meta(&mut document.meta);
    }
    query
}

fn terminal_outline(outline: &QueryOutline) -> QueryOutline {
    let mut outline = outline.clone();
    outline.label = sanitize_terminal_text(&outline.label).into_owned();
    if let Some(meta) = outline.meta.as_mut() {
        sanitize_terminal_meta(meta);
    }
    outline
}

/// Copy an excerpt with its terminal-visible document identity made safe.
fn terminal_excerpt(excerpt: &QueryExcerpt) -> QueryExcerpt {
    let mut excerpt = excerpt.clone();
    excerpt.label = sanitize_terminal_text(&excerpt.label).into_owned();
    if let Some(meta) = excerpt.meta.as_mut() {
        sanitize_terminal_meta(meta);
    }
    excerpt
}

fn terminal_search(search: &QuerySearch) -> QuerySearch {
    let mut search = search.clone();
    search.label = sanitize_terminal_text(&search.label).into_owned();
    if let Some(meta) = search.meta.as_mut() {
        sanitize_terminal_meta(meta);
    }
    search
}

fn sanitize_terminal_meta(meta: &mut DocumentMeta) {
    for value in [
        &mut meta.title,
        &mut meta.date,
        &mut meta.volume,
        &mut meta.os,
        &mut meta.arch,
        &mut meta.alias_target,
    ]
    .into_iter()
    .flatten()
    {
        *value = sanitize_terminal_text(value).into_owned();
    }
    if let Some(section) = meta.manual_section.as_mut() {
        *section = sanitize_terminal_text(section).into_owned();
    }
    for name in &mut meta.names {
        *name = sanitize_terminal_text(name).into_owned();
    }
}

pub(super) fn render_query_result(
    result: &QueryViewResult,
    options: RenderOptions,
) -> Result<String, Failure> {
    let RenderOptions {
        format,
        pretty,
        color,
        ..
    } = options;
    let output_terminal = options.terminal();
    match result {
        QueryViewResult::Full(query) => render_full_query(query, options),
        QueryViewResult::Outline(outline) => match format {
            QueryFormat::Markdown if output_terminal => Ok(mant_engine::render_outline_markdown(
                &terminal_outline(outline),
            )),
            QueryFormat::Markdown => Ok(mant_engine::render_outline_markdown(outline)),
            QueryFormat::Text => Ok(render_terminal_outline(outline, color)),
            QueryFormat::Man => Err(Failure::usage(
                "--format man applies only to full documents",
            )),
            QueryFormat::Json => {
                mant_engine::render_outline_json(outline, pretty).map_err(Failure::operational)
            }
        },
        QueryViewResult::Excerpt(excerpt) => render_excerpt(excerpt, options),
        QueryViewResult::Search(search) => match format {
            QueryFormat::Markdown if output_terminal => Ok(mant_engine::render_search_markdown(
                &terminal_search(search),
            )),
            QueryFormat::Markdown => Ok(mant_engine::render_search_markdown(search)),
            QueryFormat::Text => Ok(render_terminal_search(search, color)),
            QueryFormat::Man => Err(Failure::usage(
                "--format man applies only to full documents",
            )),
            QueryFormat::Json => {
                mant_engine::render_search_json(search, pretty).map_err(Failure::operational)
            }
        },
    }
}

pub(super) fn render_scope_query_result(
    response: &ScopeQueryResponse,
    options: RenderOptions,
) -> Result<String, Failure> {
    let RenderOptions {
        format,
        pretty,
        preserve_anchors,
        color,
        ..
    } = options;
    let output_terminal = options.terminal();
    if format == QueryFormat::Json {
        return render_json(response, pretty);
    }
    if format == QueryFormat::Man {
        return Err(Failure::usage(
            "--format man applies only to one full native manual",
        ));
    }
    let mut output = String::new();
    match &response.result {
        ScopeQueryResult::Explain {
            matches, failures, ..
        } => {
            for (index, found) in matches.iter().enumerate() {
                if index > 0 {
                    output.push_str("\n\n");
                }
                write_scope_heading(
                    &mut output,
                    &found.address.catalog_path(),
                    format,
                    color,
                    output_terminal,
                );
                output.push('\n');
                let rendered = match format {
                    QueryFormat::Markdown => {
                        let excerpt = output_terminal.then(|| terminal_excerpt(&found.excerpt));
                        mant_engine::render_excerpt_markdown_with_options(
                            excerpt.as_ref().unwrap_or(&found.excerpt),
                            mant_engine::MarkdownOptions { preserve_anchors },
                        )
                    }
                    QueryFormat::Text => render_terminal_excerpt(&found.excerpt, color),
                    QueryFormat::Json | QueryFormat::Man => unreachable!(),
                };
                output.push_str(rendered.trim());
            }
            for failure in failures {
                if !output.is_empty() {
                    output.push_str("\n\n");
                }
                write_scope_heading(
                    &mut output,
                    &failure.address.catalog_path(),
                    format,
                    color,
                    output_terminal,
                );
                output.push('\n');
                if format == QueryFormat::Text || output_terminal {
                    output.push_str(&sanitize_terminal_text(&failure.reason));
                } else {
                    output.push_str(&failure.reason);
                }
            }
        }
        ScopeQueryResult::Search { search } => {
            for (index, found) in search.documents.iter().enumerate() {
                if index > 0 {
                    output.push_str("\n\n");
                }
                write_scope_heading(
                    &mut output,
                    &found.address.catalog_path(),
                    format,
                    color,
                    output_terminal,
                );
                output.push('\n');
                let rendered = match format {
                    QueryFormat::Markdown => {
                        let search = output_terminal.then(|| terminal_search(&found.search));
                        mant_engine::render_search_markdown(
                            search.as_ref().unwrap_or(&found.search),
                        )
                    }
                    QueryFormat::Text => render_terminal_search(&found.search, color),
                    QueryFormat::Json | QueryFormat::Man => unreachable!(),
                };
                output.push_str(rendered.trim());
            }
        }
    }
    Ok(output)
}

fn write_scope_heading(
    output: &mut String,
    address: &str,
    format: QueryFormat,
    color: bool,
    output_terminal: bool,
) {
    let address = if format == QueryFormat::Text || output_terminal {
        sanitize_terminal_text(address)
    } else {
        std::borrow::Cow::Borrowed(address)
    };
    match format {
        QueryFormat::Markdown => {
            output.push_str("## ");
            output.push_str(&address);
        }
        QueryFormat::Text if color => {
            let style = terminal_style(TerminalRole::Document);
            write!(output, "{style}{address}{style:#}").expect("writing to String cannot fail");
        }
        QueryFormat::Text => output.push_str(&address),
        QueryFormat::Json | QueryFormat::Man => unreachable!(),
    }
}

fn render_excerpt(
    excerpt: &mant_protocol::QueryExcerpt,
    options: RenderOptions,
) -> Result<String, Failure> {
    let RenderOptions {
        format,
        pretty,
        preserve_anchors,
        color,
        ..
    } = options;
    let output_terminal = options.terminal();
    match format {
        QueryFormat::Markdown => {
            let terminal_copy = output_terminal.then(|| terminal_excerpt(excerpt));
            Ok(mant_engine::render_excerpt_markdown_with_options(
                terminal_copy.as_ref().unwrap_or(excerpt),
                mant_engine::MarkdownOptions { preserve_anchors },
            ))
        }
        QueryFormat::Text => Ok(render_terminal_excerpt(excerpt, color)),
        QueryFormat::Man => Err(Failure::usage(
            "--format man applies only to full documents",
        )),
        QueryFormat::Json => {
            mant_engine::render_excerpt_json(excerpt, pretty).map_err(Failure::operational)
        }
    }
}

fn render_full_query(query: &ResolvedContent, options: RenderOptions) -> Result<String, Failure> {
    let RenderOptions {
        format,
        pretty,
        preserve_anchors,
        ..
    } = options;
    let output_terminal = options.terminal();
    match format {
        QueryFormat::Markdown => {
            let terminal_copy = output_terminal.then(|| terminal_content(query));
            Ok(mant_engine::render_markdown_with_options(
                terminal_copy.as_ref().unwrap_or(query),
                mant_engine::MarkdownOptions { preserve_anchors },
            ))
        }
        QueryFormat::Text => {
            let query = terminal_content(query);
            Ok(mant_engine::render_query_text(&query))
        }
        QueryFormat::Man => {
            let Some(document) = query.document.as_ref() else {
                return Err(Failure::operational(
                    "manual page is unavailable; --format man cannot render tldr-only content",
                ));
            };
            if document.source.format == SourceFormat::Markdown {
                return Err(Failure::usage(
                    "--format man applies only to roff manual pages",
                ));
            }
            let query = terminal_content(query);
            Ok(mant_engine::render_query_man(&query))
        }
        QueryFormat::Json => {
            mant_engine::render_query_json(query, pretty).map_err(Failure::operational)
        }
    }
}

pub(super) fn render_json(value: &impl Serialize, pretty: bool) -> Result<String, Failure> {
    if pretty {
        serde_json::to_string_pretty(value).map_err(Failure::operational)
    } else {
        serde_json::to_string(value).map_err(Failure::operational)
    }
}

#[cfg(test)]
mod tests {
    use mant_engine::{project_query_view, query_markdown_text};
    use mant_protocol::{OutlineDetail, QueryView};

    use super::{OutputTarget, QueryFormat, RenderOptions, render_query_result};

    const PAGE: &str = r"# demo

## Options

<!-- mant:entries role=option -->
- `--color WHEN`: Select terminal colour behavior.

The selected color is visible in terminal output.
";

    #[test]
    fn terminal_styles_do_not_change_visible_query_text() {
        for view in [
            QueryView::Outline {
                detail: OutlineDetail::Entries,
            },
            QueryView::Explain {
                entry: "--color".to_owned(),
            },
            QueryView::Search {
                pattern: "color".to_owned(),
                syntax: mant_protocol::SearchSyntax::Literal,
                case: mant_protocol::SearchCase::Insensitive,
                scope: mant_protocol::SearchScope::Visible,
                word: false,
                context_lines: 1,
                limit: 100,
                offset: 0,
            },
        ] {
            let query = query_markdown_text(PAGE, None).expect("Markdown query");
            let result = project_query_view(query, &view).expect("query projection");
            let plain = render_query_result(
                &result,
                options(QueryFormat::Text, false, OutputTarget::Stream),
            )
            .expect("plain terminal text");
            let colored = render_query_result(
                &result,
                options(QueryFormat::Text, true, OutputTarget::Terminal),
            )
            .expect("colored terminal text");
            assert!(colored.contains("\x1b["));
            assert_eq!(strip_ansi(&colored), plain);
        }
    }

    #[test]
    fn structured_and_markdown_formats_never_receive_terminal_styles() {
        let query = query_markdown_text(PAGE, None).expect("Markdown query");
        let result = project_query_view(
            query,
            &QueryView::Explain {
                entry: "--color".to_owned(),
            },
        )
        .expect("explanation");
        for format in [QueryFormat::Markdown, QueryFormat::Json] {
            let rendered =
                render_query_result(&result, options(format, true, OutputTarget::Stream))
                    .expect("deterministic output");
            assert!(!rendered.contains("\x1b["));
        }
    }

    #[test]
    fn uncoloured_terminal_presentations_mask_controls_in_direct_input_labels() {
        let source_path = "ev\u{1b}[31mil.md".to_owned();
        let views = [
            QueryView::Full {},
            QueryView::Outline {
                detail: OutlineDetail::Entries,
            },
            QueryView::Explain {
                entry: "--color".to_owned(),
            },
            QueryView::Search {
                pattern: "color".to_owned(),
                syntax: mant_protocol::SearchSyntax::Literal,
                case: mant_protocol::SearchCase::Insensitive,
                scope: mant_protocol::SearchScope::Visible,
                word: false,
                context_lines: 1,
                limit: 100,
                offset: 0,
            },
        ];

        for view in views {
            let query = query_markdown_text(PAGE, Some(source_path.clone()))
                .expect("Markdown query with hostile label");
            let result = project_query_view(query, &view).expect("query projection");
            let rendered = render_query_result(
                &result,
                options(QueryFormat::Text, false, OutputTarget::Terminal),
            )
            .expect("plain terminal text");

            assert!(!rendered.contains('\u{1b}'));
            assert!(rendered.contains("ev�[31mil.md"));
        }
    }

    #[test]
    fn terminal_markdown_masks_dynamic_controls_without_rewriting_redirected_data() {
        for view in [
            QueryView::Full {},
            QueryView::Outline {
                detail: OutlineDetail::Entries,
            },
            QueryView::Explain {
                entry: "--color".to_owned(),
            },
            QueryView::Search {
                pattern: "color".to_owned(),
                syntax: mant_protocol::SearchSyntax::Literal,
                case: mant_protocol::SearchCase::Insensitive,
                scope: mant_protocol::SearchScope::Visible,
                word: false,
                context_lines: 1,
                limit: 100,
                offset: 0,
            },
        ] {
            let mut query = query_markdown_text(PAGE, Some("ris\u{1b}c.md".to_owned()))
                .expect("Markdown query with hostile label");
            query.document.as_mut().expect("parsed document").meta.title =
                Some("ris\u{1b}c".to_owned());
            let result = project_query_view(query, &view).expect("query projection");
            let redirected = render_query_result(
                &result,
                options(QueryFormat::Markdown, false, OutputTarget::Stream),
            )
            .expect("redirected Markdown");
            let terminal = render_query_result(
                &result,
                options(QueryFormat::Markdown, false, OutputTarget::Terminal),
            )
            .expect("terminal Markdown");

            assert!(redirected.contains('\u{1b}'), "{view:?}: {redirected:?}");
            assert!(!terminal.contains('\u{1b}'), "{view:?}: {terminal:?}");
            assert!(terminal.contains("ris�c"));
        }
    }

    const fn options(format: QueryFormat, color: bool, target: OutputTarget) -> RenderOptions {
        RenderOptions {
            format,
            pretty: true,
            preserve_anchors: false,
            color,
            target,
        }
    }

    fn strip_ansi(value: &str) -> String {
        let mut output = String::with_capacity(value.len());
        let bytes = value.as_bytes();
        let mut index = 0;
        while index < bytes.len() {
            if bytes[index] == 0x1b && bytes.get(index + 1) == Some(&b'[') {
                index += 2;
                while index < bytes.len() {
                    let byte = bytes[index];
                    index += 1;
                    if byte.is_ascii_alphabetic() {
                        break;
                    }
                }
            } else {
                let character = value[index..].chars().next().expect("UTF-8 character");
                output.push(character);
                index += character.len_utf8();
            }
        }
        output
    }
}