leotui 0.2.0

A terminal front end for leolib.
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
//! Drawing: the outline, the body, the status line and the help overlay.

use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use std::rc::Rc;

use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
use ratatui::Frame;

use crate::app::{App, Focus, Mode};
use crate::bindings::{self, BINDINGS};
use crate::commands;
use crate::highlight::{self, Class};
use crate::theme::{Colour, Depth, Theme};

pub fn draw(f: &mut Frame, app: &mut App) {
    let area = f.area();
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Min(3),
            Constraint::Length(1),
        ])
        .split(area);
    let panes = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(app.tree_percent),
            Constraint::Percentage(100 - app.tree_percent),
        ])
        .split(chunks[1]);

    draw_breadcrumb(f, app, chunks[0]);
    draw_outline(f, app, panes[0]);
    draw_body(f, app, panes[1]);
    draw_status(f, app, chunks[2]);
    if app.mode == Mode::Help {
        draw_help(f, app, area);
    }
    draw_menu(f, app, area);
}

/// The path to the current node, so a deep node says where it is.
fn draw_breadcrumb(f: &mut Frame, app: &App, area: Rect) {
    let text = truncate(&format!(" {}", app.breadcrumb()), area.width as usize);
    f.render_widget(
        Paragraph::new(Line::from(Span::styled(
            text,
            Style::default().fg(Color::Cyan),
        ))),
        area,
    );
}

/// The border of the focused pane is thick; the other is plain.
fn pane_block(title: String, focused: bool) -> Block<'static> {
    let block = Block::default().borders(Borders::ALL).title(title);
    if focused {
        block
            .border_type(BorderType::Thick)
            .border_style(Style::default().fg(Color::Cyan))
    } else {
        block.border_style(Style::default().fg(Color::DarkGray))
    }
}

fn draw_outline(f: &mut Frame, app: &mut App, area: Rect) {
    let inner_height = area.height.saturating_sub(2) as usize;
    app.tree_height = inner_height.max(1);
    let (rows, current) = app.rows_and_current();
    // Keep the selected row on screen without recentring on every keypress.
    if current < app.top {
        app.top = current;
    } else if inner_height > 0 && current >= app.top + inner_height {
        app.top = current + 1 - inner_height;
    }
    let width = area.width.saturating_sub(2) as usize;
    let focused = app.focus == Focus::Tree;

    let mut lines: Vec<Line> = Vec::new();
    for (i, row) in rows.iter().enumerate().skip(app.top).take(inner_height) {
        // A fold marker Leo users know, and a cursor column so the view is
        // still readable where reverse video is not, as in --dump.
        let marker = if !row.has_children {
            "  "
        } else if row.expanded {
            "- "
        } else {
            "+ "
        };
        let flags = format!(
            "{}{}{}{}",
            if i == current { ">" } else { " " },
            if row.marked { "*" } else { " " },
            if row.cloned { "C" } else { " " },
            if row.dirty { "~" } else { " " },
        );
        let indent = "  ".repeat(row.depth);
        let prefix = format!("{flags}{indent}{marker}");
        let text = truncate(&format!("{prefix}{}", row.headline), width);
        let style = if i == current {
            let bg = if focused {
                Color::Blue
            } else {
                Color::DarkGray
            };
            Style::default()
                .bg(bg)
                .fg(Color::White)
                .add_modifier(Modifier::BOLD)
        } else if row.marked {
            Style::default().fg(Color::Yellow)
        } else if row.is_file {
            Style::default().fg(Color::Green)
        } else {
            Style::default()
        };
        let matches: Vec<std::ops::Range<usize>> =
            app.hlsearch.as_ref().map_or_else(Vec::new, |re| {
                crate::search::ranges(re, &row.headline)
                    .into_iter()
                    .map(|r| r.start + prefix.len()..r.end + prefix.len())
                    .collect()
            });
        let cells: Vec<Span> = cut(&text, &matches)
            .into_iter()
            .map(|(piece, hit)| {
                let style = if hit {
                    style.patch(match_style())
                } else {
                    style
                };
                Span::styled(piece.to_string(), style)
            })
            .collect();
        lines.push(Line::from(cells));
    }

    let title = format!(" outline {}/{} ", current + 1, rows.len());
    f.render_widget(
        Paragraph::new(lines).block(pane_block(title, focused)),
        area,
    );
}

fn draw_body(f: &mut Frame, app: &mut App, area: Rect) {
    // The body is the model's text, unless a change is being typed.
    let lines = app.body_buffer();
    let editing = app.focus == Focus::Body;
    let cursor = editing.then_some(app.editor.cursor);
    let selection = app.editor.visual_range(&lines);
    let inner_height = area.height.saturating_sub(2) as usize;
    let inner_width = area.width.saturating_sub(2) as usize;
    app.body_height = inner_height.max(1);

    // Scroll to the cursor while editing, and by hand otherwise.
    let max_top = lines.len().saturating_sub(1);
    let top = match cursor {
        Some((row, _)) if inner_height > 0 => {
            if row < app.body_scroll {
                row
            } else if row >= app.body_scroll + inner_height {
                row + 1 - inner_height
            } else {
                app.body_scroll
            }
        }
        _ => app.body_scroll.min(max_top),
    };
    app.body_scroll = top;

    let number_width = if app.options.number {
        format!("{} ", lines.len()).len()
    } else {
        0
    };
    let text_width = inner_width.saturating_sub(number_width);
    // The language comes from the model, and the body may change it partway
    // through: see `highlight`. A node nothing declares a language for is left
    // plain rather than coloured as whatever the outline's default is.
    let language = match app.options.syntax {
        true => highlight::language_of(app.outline(), &app.current),
        false => None,
    };
    let spans = match language {
        Some(language) => app.colouring.of(&lines, &language),
        None => Rc::default(),
    };
    let palette = palette(&app.theme, app.depth);
    let hlsearch = app.hlsearch.clone();
    let shown: Vec<Line> = lines
        .iter()
        .enumerate()
        .skip(top)
        .take(inner_height)
        .map(|(i, l)| {
            // A selected line is shown reversed. The exact columns matter less
            // than seeing what an operator would take.
            let selected = matches!(selection, Some((a, b, _)) if i >= a.0 && i <= b.0);
            let mut cells: Vec<Span> = Vec::new();
            if number_width > 0 {
                cells.push(Span::styled(
                    format!("{:>w$} ", i + 1, w = number_width.saturating_sub(1)),
                    Style::default().fg(Color::DarkGray),
                ));
            }
            let base = if selected {
                Style::default().bg(Color::DarkGray).fg(Color::White)
            } else {
                Style::default()
            };
            let matches = hlsearch
                .as_ref()
                .map_or_else(Vec::new, |re| crate::search::ranges(re, l));
            // The runs are consecutive slices of the line; `offset` is where
            // each starts, so a match can be cut out of whichever it crosses.
            let mut offset = 0;
            for (text, class) in split_line(l, spans.get(i).map(|v| v.as_slice()).unwrap_or(&[])) {
                let local: Vec<std::ops::Range<usize>> = matches
                    .iter()
                    .map(|r| r.start.saturating_sub(offset)..r.end.saturating_sub(offset))
                    .collect();
                offset += text.len();
                for (piece, hit) in cut(text, &local) {
                    let piece = truncate(&piece.replace('\t', "    "), text_width);
                    if piece.is_empty() {
                        continue;
                    }
                    let style = style_for(class, base, &palette);
                    let style = if hit {
                        style.patch(match_style())
                    } else {
                        style
                    };
                    cells.push(Span::styled(piece, style));
                }
            }
            Line::from(cells)
        })
        .collect();
    let more = lines.len().saturating_sub(top + inner_height);
    let title = match app.mode {
        Mode::Insert => " body -- INSERT ".to_string(),
        Mode::Visual => " body -- VISUAL ".to_string(),
        _ if more > 0 => format!(" body ({more} more) "),
        _ => " body ".to_string(),
    };
    let focused = app.focus == Focus::Body;
    let paragraph = Paragraph::new(shown).block(pane_block(title, focused));
    let paragraph = if app.options.wrap {
        paragraph.wrap(ratatui::widgets::Wrap { trim: false })
    } else {
        paragraph
    };
    f.render_widget(paragraph, area);

    if let Some((row, col)) = cursor {
        let x = area.x + 1 + number_width as u16 + col.min(text_width.saturating_sub(1)) as u16;
        let y = area.y + 1 + (row.saturating_sub(top)) as u16;
        f.set_cursor_position((x, y));
    }
}

fn draw_status(f: &mut Frame, app: &mut App, area: Rect) {
    let mode = app.mode.label();
    let mode_style = Style::default()
        .bg(match app.mode {
            Mode::Normal => Color::Blue,
            Mode::Insert => Color::Green,
            Mode::Visual => Color::Magenta,
            Mode::Headline => Color::Magenta,
            Mode::Help => Color::Cyan,
            Mode::Confirm => Color::Red,
            Mode::Command | Mode::Search => Color::Yellow,
        })
        .fg(Color::Black)
        .add_modifier(Modifier::BOLD);
    let base = Style::default().bg(Color::DarkGray).fg(Color::White);

    // The minibuffer owns the whole line, so the answer is where the eye is.
    //
    // It is drawn plain, with no background: this is a line being typed into,
    // not a status bar, and the status bar's colours make an input look like a
    // readout. vim draws its command line the same way.
    if let Some(mini) = &app.mini {
        let label = app.mini_label();
        let text = truncate(&format!("{label}{}", mini.buffer), area.width as usize);
        f.render_widget(Paragraph::new(Line::from(Span::raw(text))), area);
        let x = area.x + (label.chars().count() + mini.cursor) as u16;
        f.set_cursor_position((x.min(area.x + area.width.saturating_sub(1)), area.y));
        return;
    }

    let right = if !app.message.is_empty() {
        app.message.clone()
    } else {
        let pending = app.pending_keys();
        let hint = match (pending.is_empty(), app.mode) {
            (false, _) => pending,
            (true, Mode::Help) => "q closes".to_string(),
            (true, Mode::Insert) => "Esc commits, Ctrl-c abandons".to_string(),
            (true, Mode::Visual) => "d c y > < to operate, Esc cancels".to_string(),
            _ => "F1 help".to_string(),
        };
        format!("{}  {hint}", app.status())
    };
    let mode_text = format!(" {mode} ");
    let width = area.width as usize;
    let rest = truncate(&format!(" {right}"), width.saturating_sub(mode_text.len()));
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(mode_text, mode_style),
            Span::styled(
                format!("{rest:<w$}", w = width.saturating_sub(mode.len() + 2)),
                base,
            ),
        ])),
        area,
    );
}

/// The help overlay, generated from the binding table.
fn draw_help(f: &mut Frame, app: &mut App, area: Rect) {
    let lines = help_lines(app.focus);
    // Never wider than 80, never narrower than the terminal allows.
    let w = area.width.saturating_sub(8).clamp(20, 80);
    let h = area.height.saturating_sub(4).max(6);
    let x = area.x + (area.width.saturating_sub(w)) / 2;
    let y = area.y + (area.height.saturating_sub(h)) / 2;
    let popup = Rect::new(x, y, w, h);
    f.render_widget(Clear, popup);

    let inner = h.saturating_sub(2) as usize;
    let max_top = lines.len().saturating_sub(inner);
    app.help_scroll = app.help_scroll.min(max_top);
    let shown: Vec<Line> = lines
        .iter()
        .skip(app.help_scroll)
        .take(inner)
        .map(|l| Line::from(truncate(l, w.saturating_sub(2) as usize)))
        .collect();
    let title = format!(
        " keys: {} pane  {}-{}/{}  q closes ",
        if app.focus == Focus::Tree {
            "outline"
        } else {
            "body"
        },
        app.help_scroll + 1,
        (app.help_scroll + inner).min(lines.len()),
        lines.len()
    );
    f.render_widget(
        Paragraph::new(shown).block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Thick)
                .border_style(Style::default().fg(Color::Cyan))
                .title(title),
        ),
        popup,
    );
}

/// One line per command, with every key bound to it. The single source is the
/// binding table, so the help cannot drift from what the keys do.
pub fn help_lines(focus: Focus) -> Vec<String> {
    let mut out = Vec::new();
    let mut seen: Vec<&str> = Vec::new();
    for binding in bindings::for_context(Mode::Normal, focus) {
        if seen.contains(&binding.command) {
            continue;
        }
        seen.push(binding.command);
        let keys = bindings::keys_for(binding.command);
        let keys: Vec<&&str> = keys
            .iter()
            .filter(|k| {
                BINDINGS.iter().any(|x| {
                    x.keys == **k
                        && x.command == binding.command
                        && x.mode == Mode::Normal
                        && (x.focus.is_none() || x.focus == Some(focus))
                })
            })
            .collect();
        let keys: Vec<String> = keys.iter().map(|k| k.to_string()).collect();
        let summary = commands::find(binding.command)
            .map(|x| x.summary)
            .unwrap_or("");
        out.push(format!(
            "{:22} {:24} {}",
            keys.join(" "),
            binding.command,
            summary
        ));
    }
    out
}

/// A search match: vim's default `Search` colours.
fn match_style() -> Style {
    Style::default().bg(Color::Yellow).fg(Color::Black)
}

/// `text` cut at the edges of `ranges`, each piece marked if it lies in one.
/// Ranges are byte offsets, sorted and apart, as `Regex::find_iter` gives
/// them; any part past the end of `text` is dropped.
fn cut<'a>(text: &'a str, ranges: &[std::ops::Range<usize>]) -> Vec<(&'a str, bool)> {
    let mut out = Vec::new();
    let mut at = 0;
    for r in ranges {
        let (start, end) = (r.start.min(text.len()), r.end.min(text.len()));
        if start < at || start >= end {
            continue;
        }
        if start > at {
            out.push((&text[at..start], false));
        }
        out.push((&text[start..end], true));
        at = end;
    }
    if at < text.len() {
        out.push((&text[at..], false));
    }
    out
}

/// A line split into its classified runs, plain text included.
fn split_line<'a>(line: &'a str, spans: &[highlight::Span]) -> Vec<(&'a str, Class)> {
    let end = line.trim_end_matches('\n').len();
    let mut out = Vec::new();
    let mut at = 0usize;
    for span in spans {
        if span.start > at {
            out.push((&line[at..span.start.min(end)], Class::Plain));
        }
        out.push((&line[span.start.min(end)..span.end.min(end)], span.class));
        at = span.end.min(end);
    }
    if at < end {
        out.push((&line[at..end], Class::Plain));
    }
    out
}

/// The completion drop-down, above the `:` line.
///
/// Only for a command's argument. A bare command name completes in place, as
/// vim's does, and a list over the whole command table would cover the outline
/// every time `:` is pressed.
fn draw_menu(f: &mut Frame, app: &App, area: Rect) {
    let Some(mini) = &app.mini else {
        return;
    };
    let menu = mini.menu(&app.theme_names);
    if !menu.is_open() || area.height < 3 {
        return;
    }

    // Columns wide enough for the longest name, as many as the width allows.
    let width = menu
        .items
        .iter()
        .map(|s| s.chars().count())
        .max()
        .unwrap_or(1)
        + 2;
    let inner_width = area.width.saturating_sub(2) as usize;
    let columns = (inner_width / width).max(1);
    let rows = menu.items.len().div_ceil(columns);
    let height = (rows as u16 + 2).min(area.height.saturating_sub(1)).min(12);
    let visible = height.saturating_sub(2) as usize;

    // Scroll by whole columns, so the selected name is on screen.
    let first = match menu.selected {
        Some(i) if visible > 0 => (i % rows) / visible * visible,
        _ => 0,
    };

    let lines: Vec<Line> = (first..(first + visible).min(rows))
        .map(|row| {
            let mut cells: Vec<Span> = Vec::new();
            for column in 0..columns {
                let Some(name) = menu.items.get(column * rows + row) else {
                    continue;
                };
                let text = format!("{name:<width$}");
                let style = match menu.selected == Some(column * rows + row) {
                    true => Style::default().bg(Color::Blue).fg(Color::White),
                    false => Style::default(),
                };
                cells.push(Span::styled(text, style));
            }
            Line::from(cells)
        })
        .collect();

    let popup = Rect {
        x: area.x,
        y: area.y + area.height.saturating_sub(1 + height),
        width: area.width,
        height,
    };
    f.render_widget(Clear, popup);
    f.render_widget(
        Paragraph::new(lines).block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(BorderType::Rounded)
                .title(format!(" {} themes ", menu.items.len())),
        ),
        popup,
    );
}

/// The Helix scope each class is drawn as.
///
/// A theme names scopes, not classes, and resolves `type.builtin` to `type`
/// when it defines only the second. `Plain` is absent, and keeps the
/// terminal's own foreground.
const SCOPES: &[(Class, &str)] = &[
    (Class::Directive, "keyword.directive"),
    (Class::Section, "markup.link.text"),
    (Class::Comment, "comment"),
    (Class::Str, "string"),
    (Class::Number, "constant.numeric"),
    (Class::Keyword, "keyword"),
    (Class::BuiltinFunction, "function.builtin"),
    (Class::BuiltinType, "type.builtin"),
    (Class::BuiltinConstant, "constant.builtin"),
    (Class::Function, "function"),
    (Class::Type, "type"),
    (Class::Property, "variable.other.member"),
    (Class::Attribute, "attribute"),
];

/// Every class's style, resolved once for the frame.
///
/// Resolving a scope walks its prefixes, and reducing a colour searches 240
/// candidates in CIELAB. A body holds thousands of runs and neither answer
/// changes between them.
fn palette(theme: &Theme, depth: Depth) -> Vec<(Class, Style)> {
    SCOPES
        .iter()
        .map(|&(class, scope)| {
            let face = theme.face(scope);
            let mut style = Style::default();
            if let Some(colour) = face.fg {
                style = style.fg(terminal_colour(colour.reduce(depth)));
            }
            if face.bold {
                style = style.add_modifier(Modifier::BOLD);
            }
            if face.italic {
                style = style.add_modifier(Modifier::ITALIC);
            }
            (class, style)
        })
        .collect()
}

/// The style for one class, over whatever the line's own style is.
///
/// A class the theme says nothing about is left plain rather than guessed at.
fn style_for(class: Class, base: Style, palette: &[(Class, Style)]) -> Style {
    match palette.iter().find(|(c, _)| *c == class) {
        Some((_, style)) => base.patch(*style),
        None => base,
    }
}

/// A reduced colour as ratatui names it.
///
/// The sixteen keep their names rather than becoming `Indexed`, so the
/// terminal's own palette decides them.
fn terminal_colour(colour: Colour) -> Color {
    let n = match colour {
        Colour::Rgb(r, g, b) => return Color::Rgb(r, g, b),
        Colour::Ansi(n) => n,
    };
    match n {
        0 => Color::Black,
        1 => Color::Red,
        2 => Color::Green,
        3 => Color::Yellow,
        4 => Color::Blue,
        5 => Color::Magenta,
        6 => Color::Cyan,
        7 => Color::Gray,
        8 => Color::DarkGray,
        9 => Color::LightRed,
        10 => Color::LightGreen,
        11 => Color::LightYellow,
        12 => Color::LightBlue,
        13 => Color::LightMagenta,
        14 => Color::LightCyan,
        15 => Color::White,
        n => Color::Indexed(n),
    }
}

/// Cut a line to `width` display cells, counting characters, not bytes.
fn truncate(s: &str, width: usize) -> String {
    if width == 0 {
        return String::new();
    }
    s.chars().take(width).collect()
}

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

    #[test]
    fn the_terminals_own_sixteen_keep_their_names() {
        // `Indexed(3)` and `Yellow` paint the same cell, but only the name
        // follows a terminal whose palette has been changed.
        assert_eq!(terminal_colour(Colour::Ansi(3)), Color::Yellow);
        assert_eq!(terminal_colour(Colour::Ansi(8)), Color::DarkGray);
        assert_eq!(terminal_colour(Colour::Ansi(200)), Color::Indexed(200));
        assert_eq!(terminal_colour(Colour::Rgb(1, 2, 3)), Color::Rgb(1, 2, 3));
    }

    #[test]
    fn every_class_but_plain_has_a_scope() {
        // A class with no entry is drawn plain, which for anything but
        // `Plain` would be a colour silently lost.
        for class in [
            Class::Directive,
            Class::Section,
            Class::Comment,
            Class::Str,
            Class::Number,
            Class::Keyword,
            Class::BuiltinFunction,
            Class::BuiltinType,
            Class::BuiltinConstant,
            Class::Function,
            Class::Type,
            Class::Property,
            Class::Attribute,
        ] {
            assert!(
                SCOPES.iter().any(|(c, _)| *c == class),
                "{class:?} has no scope"
            );
        }
        assert!(!SCOPES.iter().any(|(c, _)| *c == Class::Plain));
    }

    #[test]
    fn a_reduced_theme_reaches_the_style() {
        let palette = palette(&Theme::builtin(), Depth::Ansi16);
        let base = Style::default();
        assert_eq!(
            style_for(Class::Keyword, base, &palette).fg,
            Some(Color::Yellow)
        );
        assert_eq!(style_for(Class::Plain, base, &palette).fg, None);
        assert!(style_for(Class::Directive, base, &palette)
            .add_modifier
            .contains(Modifier::BOLD));
    }
}