1//! Highlight text.
2use crate::Color;
34use std::ops::Range;
56/// A type capable of highlighting text.
7///
8/// A [`Highlighter`] highlights lines in sequence. When a line changes,
9/// it must be notified and the lines after the changed one must be fed
10/// again to the [`Highlighter`].
11pub trait Highlighter: 'static {
12/// The settings to configure the [`Highlighter`].
13type Settings: PartialEq + Clone;
1415/// The output of the [`Highlighter`].
16type Highlight;
1718/// The highlight iterator type.
19type Iterator<'a>: Iterator<Item = (Range<usize>, Self::Highlight)>
20where
21Self: 'a;
2223/// Creates a new [`Highlighter`] from its [`Self::Settings`].
24fn new(settings: &Self::Settings) -> Self;
2526/// Updates the [`Highlighter`] with some new [`Self::Settings`].
27fn update(&mut self, new_settings: &Self::Settings);
2829/// Notifies the [`Highlighter`] that the line at the given index has changed.
30fn change_line(&mut self, line: usize);
3132/// Highlights the given line.
33 ///
34 /// If a line changed prior to this, the first line provided here will be the
35 /// line that changed.
36fn highlight_line(&mut self, line: &str) -> Self::Iterator<'_>;
3738/// Returns the current line of the [`Highlighter`].
39 ///
40 /// If `change_line` has been called, this will normally be the least index
41 /// that changed.
42fn current_line(&self) -> usize;
43}
4445/// A highlighter that highlights nothing.
46#[derive(Debug, Clone, Copy)]
47pub struct PlainText;
4849impl Highlighter for PlainText {
50type Settings = ();
51type Highlight = ();
5253type Iterator<'a> = std::iter::Empty<(Range<usize>, Self::Highlight)>;
5455fn new(_settings: &Self::Settings) -> Self {
56Self
57}
5859fn update(&mut self, _new_settings: &Self::Settings) {}
6061fn change_line(&mut self, _line: usize) {}
6263fn highlight_line(&mut self, _line: &str) -> Self::Iterator<'_> {
64 std::iter::empty()
65 }
6667fn current_line(&self) -> usize {
68 usize::MAX
69 }
70}
7172/// The format of some text.
73#[derive(Debug, Clone, Copy, PartialEq)]
74pub struct Format<Font> {
75/// The [`Color`] of the text.
76pub color: Option<Color>,
77/// The `Font` of the text.
78pub font: Option<Font>,
79}
8081impl<Font> Default for Format<Font> {
82fn default() -> Self {
83Self {
84 color: None,
85 font: None,
86 }
87 }
88}