marustdown 0.1.2

A fast, configurable terminal markdown viewer with syntax highlighting, task toggling and keyboard link following
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
use std::collections::BTreeMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::{env, fs};

use serde::Deserialize;
use serde::de::{self, Deserializer, Visitor};
use toml::{Table, Value};

const DEFAULT: &str = include_str!("../assets/config.toml");
const ASCII: &str = include_str!("../assets/ascii.toml");
const NO_COLOR: &str = include_str!("../assets/nocolor.toml");
const PRESETS: [(&str, &str); 3] = [
    ("dark", include_str!("../assets/themes/dark.toml")),
    ("light", include_str!("../assets/themes/light.toml")),
    ("ansi", include_str!("../assets/themes/ansi.toml")),
];

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub layout: Layout,
    pub colors: BTreeMap<String, ColorValue>,
    pub styles: Styles<StyleSpec>,
    pub glyphs: Glyphs,
    pub keys: Keys,
}

#[derive(Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Layout {
    pub width: usize,
    pub margin: usize,
    pub center: bool,
    pub line_numbers: bool,
    pub tab_width: usize,
    pub scroll_off: usize,
    pub color: bool,
    pub icons: bool,
    pub status_bar: bool,
}

impl Layout {
    /// Content width and left margin for a terminal `cols` wide.
    pub fn fit(&self, cols: usize) -> (usize, usize) {
        let width = self.width.min(cols.saturating_sub(2 * self.margin)).max(1);
        let margin = if self.center {
            cols.saturating_sub(width) / 2
        } else {
            self.margin
        };
        (width, margin)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum ColorValue {
    Index(u8),
    Name(String),
}

impl<'de> Deserialize<'de> for ColorValue {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct V;
        impl Visitor<'_> for V {
            type Value = ColorValue;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("a color name, \"#rrggbb\", \"default\" or 0-255")
            }
            fn visit_i64<E: de::Error>(self, n: i64) -> Result<ColorValue, E> {
                u8::try_from(n)
                    .map(ColorValue::Index)
                    .map_err(|_| E::invalid_value(de::Unexpected::Signed(n), &self))
            }
            fn visit_str<E: de::Error>(self, s: &str) -> Result<ColorValue, E> {
                Ok(ColorValue::Name(s.to_owned()))
            }
        }
        d.deserialize_any(V)
    }
}

#[derive(Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct StyleSpec {
    pub fg: Option<ColorValue>,
    pub bg: Option<ColorValue>,
    pub bold: bool,
    pub dim: bool,
    pub italic: bool,
    pub underline: bool,
    pub strike: bool,
    pub reverse: bool,
}

/// Declares a struct with one field per name, generic over the field type,
/// so the same list serves config specs and resolved styles or key maps.
macro_rules! fields {
    ($name:ident { $($field:ident),* $(,)? }) => {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        pub struct $name<T> { $(pub $field: T,)* }

        #[allow(dead_code)]
        impl<T> $name<T> {
            pub fn entries(&self) -> impl Iterator<Item = (&'static str, &T)> {
                [$((stringify!($field), &self.$field),)*].into_iter()
            }

            pub fn try_map<U, E>(
                self,
                mut f: impl FnMut(&'static str, T) -> Result<U, E>,
            ) -> Result<$name<U>, E> {
                Ok($name { $($field: f(stringify!($field), self.$field)?,)* })
            }
        }
    };
}

fields!(Styles {
    text,
    h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    strong,
    emphasis,
    strike,
    code,
    link,
    link_icon,
    image,
    quote,
    quote_bar,
    alert_note,
    alert_tip,
    alert_important,
    alert_warning,
    alert_caution,
    bullet,
    number,
    task_done,
    task_todo,
    rule,
    code_block,
    code_border,
    code_label,
    line_number,
    table_border,
    table_header,
    syntax_keyword,
    syntax_string,
    syntax_number,
    syntax_comment,
    syntax_type,
    syntax_function,
    syntax_constant,
    syntax_operator,
    syntax_tag,
    syntax_attribute,
    syntax_inserted,
    syntax_deleted,
    cursor,
    status,
    search,
    outline_level,
    hint,
    link_selected,
});

fields!(KeyTable {
    down,
    up,
    page_down,
    page_up,
    half_down,
    half_up,
    top,
    bottom,
    next_heading,
    prev_heading,
    search,
    next_match,
    prev_match,
    toggle,
    next_link,
    prev_link,
    open,
    hints,
    back,
    copy,
    outline,
    edit,
    quit,
});

pub type Keys = KeyTable<Vec<String>>;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Glyphs {
    pub heading: [String; 6],
    pub heading_rule: [String; 6],
    pub bullets: Vec<String>,
    pub task_done: String,
    pub task_todo: String,
    pub quote: String,
    pub link: String,
    pub image: String,
    pub rule: String,
    pub ellipsis: String,
    pub code_copy: String,
    pub code_box: [String; 6],
    pub table_box: [String; 11],
    pub alert_note: String,
    pub alert_tip: String,
    pub alert_important: String,
    pub alert_warning: String,
    pub alert_caution: String,
    pub breadcrumb: String,
    pub separator: String,
}

/// Command-line settings that decide which built-in layers apply.
#[derive(Default)]
pub struct Overrides<'a> {
    pub theme: Option<&'a str>,
    pub no_icons: bool,
    pub no_color: bool,
}

/// Loads `path`, or the user config at the XDG location when there is one,
/// layered over the built-in defaults.
pub fn load(path: Option<&Path>, overrides: &Overrides) -> Result<Config, String> {
    let dir = config_dir();
    let path = path.map(Path::to_path_buf).or_else(|| {
        dir.as_ref()
            .map(|d| d.join("config.toml"))
            .filter(|p| p.is_file())
    });
    let user = path.as_deref().map(read).transpose()?;
    build(user, dir.as_deref(), overrides).map_err(|e| match &path {
        Some(p) => format!("{}: {e}", p.display()),
        None => e,
    })
}

pub fn config_dir() -> Option<PathBuf> {
    let base = env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .filter(|p| p.is_absolute())
        .or_else(|| env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?;
    Some(base.join("marustdown"))
}

fn build(user: Option<Table>, dir: Option<&Path>, overrides: &Overrides) -> Result<Config, String> {
    let setting = |section: Option<&str>, key: &str| {
        let table = user.as_ref()?;
        let table = match section {
            Some(s) => table.get(s)?.as_table()?,
            None => table,
        };
        table.get(key).cloned()
    };
    let enabled = |key| {
        setting(Some("layout"), key)
            .and_then(|v| v.as_bool())
            .unwrap_or(true)
    };
    let icons = !overrides.no_icons && enabled("icons");
    let color = !overrides.no_color && enabled("color");
    let theme = match (overrides.theme, setting(None, "theme")) {
        (Some(t), _) => t.to_owned(),
        (None, Some(Value::String(s))) => s,
        (None, Some(_)) => return Err("theme: expected a string".into()),
        (None, None) => "dark".into(),
    };

    let mut merged = parse(DEFAULT);
    merge(&mut merged, preset(&theme, dir)?);
    if !icons {
        merge(&mut merged, parse(ASCII));
    }
    if !color {
        merge(&mut merged, parse(NO_COLOR));
    }
    if let Some(user) = user {
        merge(&mut merged, user);
    }
    merged.remove("theme");
    let mut cfg: Config = merged.try_into().map_err(|e| e.to_string())?;
    cfg.layout.icons = icons;
    cfg.layout.color = color;
    Ok(cfg)
}

fn preset(name: &str, dir: Option<&Path>) -> Result<Table, String> {
    if let Some((_, text)) = PRESETS.iter().find(|(n, _)| *n == name) {
        return Ok(parse(text));
    }
    match dir.map(|d| d.join("themes").join(format!("{name}.toml"))) {
        Some(path) if path.is_file() => read(&path),
        _ => Err(format!(
            "unknown theme {name:?}; built-in themes are dark, light and ansi"
        )),
    }
}

fn read(path: &Path) -> Result<Table, String> {
    let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
    text.parse().map_err(|e| format!("{}: {e}", path.display()))
}

fn parse(text: &str) -> Table {
    text.parse().expect("built-in config is valid TOML")
}

/// Deep merge: tables merge recursively, every other value replaces.
fn merge(base: &mut Table, over: Table) {
    for (key, value) in over {
        match (base.get_mut(&key), value) {
            (Some(Value::Table(b)), Value::Table(o)) => merge(b, o),
            (_, value) => {
                base.insert(key, value);
            }
        }
    }
}

#[cfg(test)]
pub fn defaults() -> Config {
    build(None, None, &Overrides::default()).unwrap()
}

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

    fn user(text: &str) -> Result<Config, String> {
        build(Some(text.parse().unwrap()), None, &Overrides::default())
    }

    #[test]
    fn defaults_parse() {
        let cfg = defaults();
        assert_eq!(cfg.layout.width, 90);
        assert!(cfg.colors.contains_key("accent"));
    }

    #[test]
    fn user_values_merge_key_by_key() {
        let cfg = user("[styles.h1]\nfg = \"red\"\n[layout]\nwidth = 70").unwrap();
        assert_eq!(cfg.layout.width, 70);
        assert!(cfg.layout.center);
        assert_eq!(cfg.styles.h1.fg, Some(ColorValue::Name("red".into())));
        assert!(cfg.styles.h1.bold);
    }

    #[test]
    fn unknown_keys_are_errors() {
        assert!(user("[styles.h7]\nbold = true").is_err());
        assert!(user("[layout]\nwidht = 3").is_err());
    }

    #[test]
    fn presets_and_ascii() {
        let light = user("theme = \"light\"").unwrap();
        assert_eq!(light.colors["accent"], ColorValue::Name("#1e66f5".into()));
        assert!(user("theme = \"nope\"").is_err());
        let no_icons = Overrides {
            no_icons: true,
            ..Overrides::default()
        };
        let ascii = build(None, None, &no_icons).unwrap();
        assert_eq!(ascii.glyphs.task_done, "[x]");
        assert!(!ascii.layout.icons);
    }

    #[test]
    fn no_color_reverses_the_cursor() {
        assert!(!defaults().styles.cursor.reverse);
        let no_color = Overrides {
            no_color: true,
            ..Overrides::default()
        };
        let cfg = build(None, None, &no_color).unwrap();
        assert!(cfg.styles.cursor.reverse);
        assert!(!cfg.layout.color);
        let from_file = user("[layout]\ncolor = false").unwrap();
        assert!(from_file.styles.cursor.reverse);
        let overridden =
            user("[layout]\ncolor = false\n[styles.cursor]\nreverse = false\nbold = true").unwrap();
        assert!(!overridden.styles.cursor.reverse && overridden.styles.cursor.bold);
    }

    #[test]
    fn user_glyphs_win_over_ascii() {
        let cfg = user("[layout]\nicons = false\n[glyphs]\nrule = \"~\"").unwrap();
        assert_eq!(cfg.glyphs.rule, "~");
        assert_eq!(cfg.glyphs.task_todo, "[ ]");
    }

    #[test]
    fn fit_centers_and_clamps() {
        let l = defaults().layout;
        assert_eq!(l.fit(200), (90, 55));
        assert_eq!(l.fit(50), (46, 2));
    }
}