termmd 0.1.4

A best-in-class Markdown viewer for the terminal: real typography, real tables, real images.
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
//! Command-line interface, and the settings resolution behind it.
//!
//! Three sources feed into one [`Settings`]: the flags, the config file, and
//! what we detected about the terminal. Flags win, then config, then detection.
//! Keeping that resolution in one place -- rather than checking `Option`s at each
//! use site -- is what keeps the rest of the crate free of "is this overridden?"
//! branches.

use std::path::PathBuf;

use clap::{Parser, ValueEnum};

use crate::config::Config;
use crate::markdown::ParseOptions;
use crate::render::{Glyphs, LinkMode, RenderOptions};
use crate::term::caps::{Capabilities, ColorDepth, GraphicsProtocol, UnicodeLevel};
use crate::theme::Theme;

/// A Markdown viewer for terminals that can do more than plain text.
#[derive(Debug, Parser)]
#[command(
    name = "termmd",
    version,
    about,
    long_about = None,
    after_help = "Reads from stdin when no file is given.\n\
                  Run `termmd --caps` to see what termmd detected about your terminal."
)]
pub struct Cli {
    /// Markdown files to view. Use `-` for stdin.
    #[arg(value_name = "FILE")]
    pub files: Vec<PathBuf>,

    /// Maximum text width in columns [default: terminal width, capped at 100].
    #[arg(short, long, value_name = "COLS")]
    pub width: Option<usize>,

    /// Left and right margin in columns.
    #[arg(long, value_name = "COLS")]
    pub margin: Option<usize>,

    /// Force the interactive pager, even when piping.
    #[arg(short, long, conflicts_with = "no_pager")]
    pub pager: bool,

    /// Print the document and exit, without the pager.
    #[arg(short = 'P', long)]
    pub no_pager: bool,

    /// Strip all styling and print plain text.
    #[arg(long)]
    pub plain: bool,

    /// Theme name (dark, light, mono) or path to a theme file.
    #[arg(short, long, value_name = "NAME|PATH")]
    pub theme: Option<String>,

    /// Syntax highlighting theme for code blocks.
    #[arg(long, value_name = "NAME")]
    pub syntax_theme: Option<String>,

    /// When to use colour.
    #[arg(long, value_name = "WHEN", default_value = "auto")]
    pub color: ColorChoice,

    /// Image protocol to use.
    #[arg(long, value_name = "PROTOCOL", default_value = "auto")]
    pub images: ImageChoice,

    /// Allow fetching images over http(s).
    #[arg(long)]
    pub remote_images: bool,

    /// Maximum height of an image, in terminal rows.
    #[arg(long, value_name = "ROWS")]
    pub max_image_rows: Option<u16>,

    /// How to present link destinations.
    #[arg(long, value_name = "MODE", default_value = "auto")]
    pub links: LinkChoice,

    /// Show line numbers beside code blocks.
    #[arg(short = 'n', long)]
    pub line_numbers: bool,

    /// Restrict drawing to ASCII characters.
    #[arg(long)]
    pub ascii: bool,

    /// Re-render when the file, directory or URL changes. Implies the pager.
    #[arg(long, conflicts_with = "no_pager")]
    pub watch: bool,

    /// Print the document's table of contents and exit.
    #[arg(long)]
    pub toc: bool,

    /// Print the document's YAML front matter and exit.
    #[arg(long)]
    pub front_matter: bool,

    /// Print what termmd detected about this terminal and exit.
    #[arg(long)]
    pub caps: bool,

    /// List the available syntax highlighting themes and exit.
    #[arg(long)]
    pub list_themes: bool,

    /// List the languages that can be highlighted and exit.
    #[arg(long)]
    pub list_languages: bool,

    /// Write a completion script for a shell to stdout and exit.
    #[arg(long, value_name = "SHELL")]
    pub completions: Option<clap_complete::Shell>,

    /// Write the man page, as roff, to stdout and exit.
    #[arg(long)]
    pub man: bool,

    /// Use a specific config file, or `none` to ignore any config.
    #[arg(long, value_name = "PATH")]
    pub config: Option<String>,

    /// Do not query the terminal for its capabilities at startup.
    #[arg(long)]
    pub no_probe: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default)]
pub enum ColorChoice {
    #[default]
    Auto,
    Always,
    Never,
    #[value(name = "16")]
    Ansi16,
    #[value(name = "256")]
    Ansi256,
    #[value(name = "truecolor", alias = "24bit")]
    TrueColor,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default)]
pub enum ImageChoice {
    #[default]
    Auto,
    Kitty,
    #[value(name = "iterm2")]
    ITerm2,
    Sixel,
    Blocks,
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default)]
pub enum LinkChoice {
    #[default]
    Auto,
    Hyperlink,
    Inline,
    Reference,
    Hide,
}

impl From<LinkChoice> for LinkMode {
    fn from(c: LinkChoice) -> Self {
        match c {
            LinkChoice::Auto => LinkMode::Auto,
            LinkChoice::Hyperlink => LinkMode::Hyperlink,
            LinkChoice::Inline => LinkMode::Inline,
            LinkChoice::Reference => LinkMode::Reference,
            LinkChoice::Hide => LinkMode::Hide,
        }
    }
}

/// Everything resolved: what to render, how, and where.
#[derive(Debug, Clone)]
pub struct Settings {
    pub caps: Capabilities,
    pub theme: Theme,
    pub render: RenderOptions,
    pub parse: ParseOptions,
    pub use_pager: bool,
    pub mouse: bool,
    /// Whether to emit escape sequences at all.
    ///
    /// Distinct from colour depth: a terminal with no colour still gets bold and
    /// underline, but a pipe gets neither. Redirected output should be text a
    /// script can read, which is why this is off unless stdout is a terminal or
    /// colour was explicitly forced.
    pub styled: bool,
    pub watch: bool,
    pub remote_images: bool,
    pub protocol: GraphicsProtocol,
    pub syntax_theme: String,
    /// Whether the pager was asked for explicitly, rather than chosen because
    /// stdout happens to be a terminal.
    pub pager_forced: bool,
    /// The width the user asked for, if any.
    ///
    /// Kept separate from the resolved width so that a resize can recompute
    /// from the original intent: a terminal that is widened again should get
    /// its full width back, which it cannot if the only record of the request
    /// is the value derived from the previous size.
    pub requested_width: Option<usize>,
}

/// The terminal width we will not exceed even on a very wide window.
///
/// Long lines are hard to read; newspapers settled on something near this for
/// the same reason. Users who disagree can pass `--width`.
const COMFORTABLE_WIDTH: usize = 100;

impl Settings {
    /// Resolves flags, config and detected capabilities into one settings value.
    pub fn resolve(cli: &Cli, config: &Config) -> anyhow::Result<Self> {
        let mut caps = Capabilities::detect(!cli.no_probe && config.probe != Some(false));

        // --- colour -------------------------------------------------------
        caps.color = match cli.color {
            ColorChoice::Auto => match config.color.as_deref() {
                Some("never") => ColorDepth::None,
                Some("always") => caps.color.max(ColorDepth::Ansi256),
                Some("16") => ColorDepth::Ansi16,
                Some("256") => ColorDepth::Ansi256,
                Some("truecolor" | "24bit") => ColorDepth::TrueColor,
                _ => caps.color,
            },
            ColorChoice::Never => ColorDepth::None,
            ColorChoice::Always => caps.color.max(ColorDepth::Ansi256),
            ColorChoice::Ansi16 => ColorDepth::Ansi16,
            ColorChoice::Ansi256 => ColorDepth::Ansi256,
            ColorChoice::TrueColor => ColorDepth::TrueColor,
        };
        if cli.plain {
            caps.color = ColorDepth::None;
        }

        // --- unicode ------------------------------------------------------
        if cli.ascii || config.unicode.as_deref() == Some("ascii") {
            caps.unicode = UnicodeLevel::Ascii;
        } else {
            match config.unicode.as_deref() {
                Some("extended") => caps.unicode = UnicodeLevel::Extended,
                Some("full") => caps.unicode = UnicodeLevel::Full,
                _ => {}
            }
        }

        // --- images -------------------------------------------------------
        let protocol = match cli.images {
            ImageChoice::Auto => match config.image_protocol.as_deref() {
                Some("kitty") => GraphicsProtocol::Kitty,
                Some("iterm2") => GraphicsProtocol::ITerm2,
                Some("sixel") => GraphicsProtocol::Sixel,
                Some("blocks") => GraphicsProtocol::Blocks,
                Some("none") => GraphicsProtocol::None,
                _ if config.images == Some(false) => GraphicsProtocol::None,
                _ => caps.graphics,
            },
            ImageChoice::Kitty => GraphicsProtocol::Kitty,
            ImageChoice::ITerm2 => GraphicsProtocol::ITerm2,
            ImageChoice::Sixel => GraphicsProtocol::Sixel,
            ImageChoice::Blocks => GraphicsProtocol::Blocks,
            ImageChoice::None => GraphicsProtocol::None,
        };
        let protocol = if cli.plain {
            GraphicsProtocol::None
        } else {
            protocol
        };
        caps.graphics = protocol;

        // --- theme --------------------------------------------------------
        let theme_name = cli
            .theme
            .clone()
            .or_else(|| {
                // A background-specific theme only applies when we know the
                // background, which needs a successful probe.
                if caps.prefers_dark() {
                    config.dark_theme.clone()
                } else {
                    config.light_theme.clone()
                }
            })
            .or_else(|| config.theme.clone())
            .unwrap_or_else(|| {
                if caps.prefers_dark() {
                    "dark".into()
                } else {
                    "light".into()
                }
            });

        let mut theme = load_theme(&theme_name)?;
        if caps.color == ColorDepth::None {
            theme = Theme::mono();
        }
        let syntax_theme = cli
            .syntax_theme
            .clone()
            .or_else(|| config.syntax_theme.clone())
            .unwrap_or_else(|| {
                if caps.color == ColorDepth::Ansi16 {
                    // A 16-colour terminal is better served by a theme built for
                    // the user's own palette than by squashed RGB.
                    "ansi".to_string()
                } else {
                    theme.syntax_theme.clone()
                }
            });

        // --- geometry -----------------------------------------------------
        let terminal_width = caps.cols as usize;
        let requested_width = cli.width.or(config.width);
        let width = requested_width
            .unwrap_or_else(|| terminal_width.min(COMFORTABLE_WIDTH))
            .clamp(8, 4096.max(terminal_width));
        let margin = cli.margin.or(config.margin).unwrap_or(1).min(width / 4);

        let links = if cli.links == LinkChoice::Auto {
            match config.links.as_deref() {
                Some("hyperlink") => LinkMode::Hyperlink,
                Some("inline") => LinkMode::Inline,
                Some("reference") => LinkMode::Reference,
                Some("hide") => LinkMode::Hide,
                _ => LinkMode::Auto,
            }
        } else {
            cli.links.into()
        };

        let render = RenderOptions {
            width,
            margin,
            links,
            images: protocol != GraphicsProtocol::None,
            max_image_rows: cli
                .max_image_rows
                .or(config.max_image_rows)
                .unwrap_or(24)
                .max(1),
            line_numbers: cli.line_numbers || config.line_numbers.unwrap_or(false),
            center_figures: config.center_figures.unwrap_or(true),
            tab_width: config.tab_width.unwrap_or(4).clamp(1, 16),
            glyphs: Glyphs::for_level(caps.unicode),
            heading_rules: config.heading_rules.unwrap_or(true),
        };

        let parse = ParseOptions {
            smart_punctuation: config.smart_punctuation.unwrap_or(true),
            ..Default::default()
        };

        // --- output mode --------------------------------------------------
        let use_pager = if cli.pager {
            true
        } else if cli.no_pager || cli.plain || cli.toc {
            false
        } else if cli.watch {
            // Watching without the pager would print once and then have nowhere
            // to put the updates.
            caps.is_tty
        } else {
            caps.is_tty && config.pager.unwrap_or(true)
        };

        // Forcing colour or a specific image protocol also forces styling, so
        // that `termmd --color=always doc.md | less -R` keeps its colour and
        // `termmd --images=kitty doc.md > frame` keeps its pictures. Left on
        // `auto`, a redirect still gets plain text.
        let images_forced = !matches!(cli.images, ImageChoice::Auto | ImageChoice::None);
        let styled = !cli.plain && (caps.is_tty || caps.color != ColorDepth::None || images_forced);

        Ok(Self {
            caps,
            theme,
            render,
            parse,
            use_pager,
            mouse: config.mouse.unwrap_or(false),
            styled,
            watch: cli.watch,
            remote_images: cli.remote_images || config.remote_images.unwrap_or(false),
            protocol,
            syntax_theme,
            pager_forced: cli.pager,
            requested_width,
        })
    }
}

/// Resolves a theme by built-in name, config-directory name, or path.
fn load_theme(name: &str) -> anyhow::Result<Theme> {
    if let Some(theme) = Theme::builtin(name) {
        return Ok(theme);
    }
    let path = PathBuf::from(name);
    if path.exists() {
        return Theme::load(&path);
    }
    if let Some(dir) = crate::config::theme_dir_for_lookup() {
        let candidate = dir.join(format!("{name}.toml"));
        if candidate.exists() {
            return Theme::load(&candidate);
        }
    }
    anyhow::bail!(
        "unknown theme {name:?}; built-in themes are {:?}, or give a path to a theme file",
        Theme::builtin_names()
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::CommandFactory;

    fn cli(args: &[&str]) -> Cli {
        Cli::parse_from(std::iter::once("termmd").chain(args.iter().copied()))
    }

    #[test]
    fn cli_definition_is_valid() {
        Cli::command().debug_assert();
    }

    #[test]
    fn parses_files_and_flags() {
        let c = cli(&["a.md", "b.md", "--width", "72", "-n"]);
        assert_eq!(c.files.len(), 2);
        assert_eq!(c.width, Some(72));
        assert!(c.line_numbers);
    }

    #[test]
    fn flags_beat_config() {
        let config = Config {
            width: Some(50),
            ..Default::default()
        };
        let s = Settings::resolve(&cli(&["--width", "72"]), &config).unwrap();
        assert_eq!(s.render.width, 72);
    }

    #[test]
    fn config_beats_defaults() {
        let config = Config {
            width: Some(50),
            margin: Some(3),
            ..Default::default()
        };
        let s = Settings::resolve(&cli(&[]), &config).unwrap();
        assert_eq!(s.render.width, 50);
        assert_eq!(s.render.margin, 3);
    }

    #[test]
    fn plain_disables_colour_and_images() {
        let s = Settings::resolve(&cli(&["--plain"]), &Config::default()).unwrap();
        assert_eq!(s.caps.color, ColorDepth::None);
        assert_eq!(s.protocol, GraphicsProtocol::None);
        assert!(!s.render.images);
        assert!(!s.use_pager);
        assert!(!s.styled);
    }

    #[test]
    fn forcing_an_image_protocol_keeps_images_through_a_pipe() {
        let s = Settings::resolve(&cli(&["--images", "kitty"]), &Config::default()).unwrap();
        assert!(s.styled, "an explicit protocol should survive a redirect");
        assert!(s.render.images);

        // `--images none` is a request for fewer escapes, not more.
        let s = Settings::resolve(&cli(&["--images", "none"]), &Config::default()).unwrap();
        assert!(!s.styled);
    }

    #[test]
    fn redirected_output_is_unstyled_unless_colour_is_forced() {
        // The test process has no terminal on stdout, which is exactly the case
        // this rule exists for.
        let s = Settings::resolve(&cli(&[]), &Config::default()).unwrap();
        assert!(!s.caps.is_tty, "test precondition");
        assert!(!s.styled, "a pipe should get plain text");

        let s = Settings::resolve(&cli(&["--color", "always"]), &Config::default()).unwrap();
        assert!(s.styled, "forcing colour should re-enable styling");
    }

    #[test]
    fn colour_can_be_forced_and_disabled() {
        assert_eq!(
            Settings::resolve(&cli(&["--color", "never"]), &Config::default())
                .unwrap()
                .caps
                .color,
            ColorDepth::None
        );
        assert_eq!(
            Settings::resolve(&cli(&["--color", "256"]), &Config::default())
                .unwrap()
                .caps
                .color,
            ColorDepth::Ansi256
        );
    }

    #[test]
    fn image_protocol_can_be_forced() {
        let s = Settings::resolve(&cli(&["--images", "sixel"]), &Config::default()).unwrap();
        assert_eq!(s.protocol, GraphicsProtocol::Sixel);
        assert!(s.render.images);

        let s = Settings::resolve(&cli(&["--images", "none"]), &Config::default()).unwrap();
        assert!(!s.render.images);
    }

    #[test]
    fn ascii_flag_downgrades_glyphs() {
        let s = Settings::resolve(&cli(&["--ascii"]), &Config::default()).unwrap();
        assert_eq!(s.caps.unicode, UnicodeLevel::Ascii);
        assert_eq!(s.render.glyphs.quote_bar, "|");
    }

    #[test]
    fn width_is_clamped_to_something_sane() {
        let s = Settings::resolve(&cli(&["--width", "1"]), &Config::default()).unwrap();
        assert!(s.render.width >= 8, "unusably narrow widths are clamped");
    }

    #[test]
    fn margin_never_eats_the_page() {
        let s = Settings::resolve(
            &cli(&["--width", "20", "--margin", "40"]),
            &Config::default(),
        )
        .unwrap();
        assert!(s.render.margin * 2 < s.render.width);
    }

    #[test]
    fn unknown_theme_is_a_helpful_error() {
        let err =
            Settings::resolve(&cli(&["--theme", "neon-dreams"]), &Config::default()).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("neon-dreams"), "{msg}");
        assert!(msg.contains("dark"), "should list the built-ins: {msg}");
    }

    #[test]
    fn an_explicit_pager_flag_is_recorded() {
        assert!(
            Settings::resolve(&cli(&["--pager"]), &Config::default())
                .unwrap()
                .pager_forced
        );
        assert!(
            !Settings::resolve(&cli(&[]), &Config::default())
                .unwrap()
                .pager_forced
        );
    }

    #[test]
    fn watch_turns_the_pager_on_when_there_is_a_terminal() {
        let config = Config {
            pager: Some(false),
            ..Default::default()
        };
        let s = Settings::resolve(&cli(&["--watch"]), &config).unwrap();
        // No terminal in the test process, so the pager stays off; what matters
        // is that the config's `pager = false` is no longer the deciding factor.
        assert_eq!(s.use_pager, s.caps.is_tty);
        assert!(s.watch);
    }

    #[test]
    fn an_explicit_width_is_remembered_separately() {
        let s = Settings::resolve(&cli(&["--width", "72"]), &Config::default()).unwrap();
        assert_eq!(s.requested_width, Some(72));

        let s = Settings::resolve(&cli(&[]), &Config::default()).unwrap();
        assert_eq!(s.requested_width, None, "an unset width must stay unset");
    }

    #[test]
    fn link_mode_is_resolved_from_either_source() {
        let s = Settings::resolve(&cli(&["--links", "reference"]), &Config::default()).unwrap();
        assert_eq!(s.render.links, LinkMode::Reference);

        let config = Config {
            links: Some("hide".into()),
            ..Default::default()
        };
        let s = Settings::resolve(&cli(&[]), &config).unwrap();
        assert_eq!(s.render.links, LinkMode::Hide);
    }
}