Highlighter

Struct Highlighter 

Source
pub struct Highlighter { /* private fields */ }
Expand description

A syntax highlighter that produces styled Text output. The output style can be changed using the configuration methods provided in this struct.

Implementations§

Source§

impl Highlighter

Source

pub fn new(theme: Theme) -> Self

Creates a new Highlighter with the given Theme.

Examples found in repository?
examples/highlight_file.rs (line 25)
22fn main() -> Result<()> {
23    let mut terminal = setup_terminal()?;
24    let theme = ASSETS.with(|a| a.get_theme("Nord").clone());
25    let highlighter = Highlighter::new(theme);
26    let syntaxes = ASSETS.with(|a| a.get_syntax_set().cloned())?;
27    let syntax = syntaxes
28        .find_syntax_by_name("Rust")
29        .expect("syntax missing");
30    let highlighted_text = highlighter.highlight_reader(
31        File::open("./examples/sqlite_custom/build.rs")?,
32        syntax,
33        &syntaxes,
34    )?;
35    terminal.draw(|frame| {
36        frame.render_widget(highlighted_text, frame.area());
37    })?;
38    read()?;
39    restore_terminal(terminal)?;
40    Ok(())
41}
More examples
Hide additional examples
examples/background_color.rs (line 28)
25fn main() -> Result<()> {
26    let mut terminal = setup_terminal()?;
27    let theme = ASSETS.with(|a| a.get_theme("Nord").clone());
28    let highlighter = Highlighter::new(theme);
29    let syntaxes = ASSETS.with(|a| a.get_syntax_set().cloned())?;
30    let syntax = syntaxes
31        .find_syntax_by_name("Rust")
32        .expect("syntax missing");
33    let highlight = highlighter.highlight_reader(
34        File::open("./examples/sqlite_custom/build.rs")?,
35        syntax,
36        &syntaxes,
37    )?;
38
39    let bg = highlighter.get_background_color().unwrap_or_default();
40    // Set the background on the text container so it matches.
41    let paragraph = Paragraph::new(highlight).bg(bg).block(
42        Block::bordered()
43            .border_type(BorderType::Rounded)
44            .padding(Padding::uniform(0))
45            .title("Syntax Highlight!")
46            .title_alignment(Alignment::Center),
47    );
48    terminal.draw(|frame| {
49        frame.render_widget(paragraph, frame.area());
50    })?;
51    read()?;
52    restore_terminal(terminal)?;
53    Ok(())
54}
Source

pub fn override_background<C>(self, background: C) -> Self
where C: Into<Color>,

Override the background with a different color. Set this to Color::Reset to disable the background color.

Source

pub fn line_numbers(self, line_numbers: bool) -> Self

Enable or disable line numbers in the left gutter.

Source

pub fn line_number_padding(self, padding: usize) -> Self

Set the padding between the line number section and the rest of the code.

Source

pub fn line_number_style<S>(self, style: S) -> Self
where S: Into<Style>,

Set the Style for the line number section.

Source

pub fn line_number_separator_style<S>(self, style: S) -> Self
where S: Into<Style>,

Set the Style for the separator between the line number section and the rest of the code.

Source

pub fn line_number_separator<T>(self, separator: T) -> Self
where T: Into<String>,

Set the text used for the line number separator. | is used by default.

Source

pub fn highlight_range(self, range: Range<usize>) -> Self

Highlight a specific range of code with a different style.

Source

pub fn highlight_style(self, style: Style) -> Self

Set the style used for highlight_range. A yellow background is used by default.

Source

pub fn gutter_template<F>(self, template: F) -> Self
where F: Fn(usize, Style) -> Vec<Span<'static>> + Send + Sync + 'static,

Set a template function to configure the gutter section. This is an alternative to using line_number_style, line_number_separator_style, and line_number_padding if you need more flexibility.

Source

pub fn get_background_color(&self) -> Option<Color>

Returns the configured background color, accounting for both the theme and any overrides. This is useful if you want to render the code block into a larger section and you need the background colors to match.

Examples found in repository?
examples/background_color.rs (line 39)
25fn main() -> Result<()> {
26    let mut terminal = setup_terminal()?;
27    let theme = ASSETS.with(|a| a.get_theme("Nord").clone());
28    let highlighter = Highlighter::new(theme);
29    let syntaxes = ASSETS.with(|a| a.get_syntax_set().cloned())?;
30    let syntax = syntaxes
31        .find_syntax_by_name("Rust")
32        .expect("syntax missing");
33    let highlight = highlighter.highlight_reader(
34        File::open("./examples/sqlite_custom/build.rs")?,
35        syntax,
36        &syntaxes,
37    )?;
38
39    let bg = highlighter.get_background_color().unwrap_or_default();
40    // Set the background on the text container so it matches.
41    let paragraph = Paragraph::new(highlight).bg(bg).block(
42        Block::bordered()
43            .border_type(BorderType::Rounded)
44            .padding(Padding::uniform(0))
45            .title("Syntax Highlight!")
46            .title_alignment(Alignment::Center),
47    );
48    terminal.draw(|frame| {
49        frame.render_widget(paragraph, frame.area());
50    })?;
51    read()?;
52    restore_terminal(terminal)?;
53    Ok(())
54}
Source

pub fn get_line_number_style(&self) -> Style

Returns the configured line number style, accounting for both the theme and any overrides.

Source

pub fn highlight_reader<R>( &self, reader: R, syntax: &SyntaxReference, syntaxes: &SyntaxSet, ) -> Result<Text<'static>, Error>
where R: Read,

Highlights text from any io::Read source.

Examples found in repository?
examples/highlight_file.rs (lines 30-34)
22fn main() -> Result<()> {
23    let mut terminal = setup_terminal()?;
24    let theme = ASSETS.with(|a| a.get_theme("Nord").clone());
25    let highlighter = Highlighter::new(theme);
26    let syntaxes = ASSETS.with(|a| a.get_syntax_set().cloned())?;
27    let syntax = syntaxes
28        .find_syntax_by_name("Rust")
29        .expect("syntax missing");
30    let highlighted_text = highlighter.highlight_reader(
31        File::open("./examples/sqlite_custom/build.rs")?,
32        syntax,
33        &syntaxes,
34    )?;
35    terminal.draw(|frame| {
36        frame.render_widget(highlighted_text, frame.area());
37    })?;
38    read()?;
39    restore_terminal(terminal)?;
40    Ok(())
41}
More examples
Hide additional examples
examples/background_color.rs (lines 33-37)
25fn main() -> Result<()> {
26    let mut terminal = setup_terminal()?;
27    let theme = ASSETS.with(|a| a.get_theme("Nord").clone());
28    let highlighter = Highlighter::new(theme);
29    let syntaxes = ASSETS.with(|a| a.get_syntax_set().cloned())?;
30    let syntax = syntaxes
31        .find_syntax_by_name("Rust")
32        .expect("syntax missing");
33    let highlight = highlighter.highlight_reader(
34        File::open("./examples/sqlite_custom/build.rs")?,
35        syntax,
36        &syntaxes,
37    )?;
38
39    let bg = highlighter.get_background_color().unwrap_or_default();
40    // Set the background on the text container so it matches.
41    let paragraph = Paragraph::new(highlight).bg(bg).block(
42        Block::bordered()
43            .border_type(BorderType::Rounded)
44            .padding(Padding::uniform(0))
45            .title("Syntax Highlight!")
46            .title_alignment(Alignment::Center),
47    );
48    terminal.draw(|frame| {
49        frame.render_widget(paragraph, frame.area());
50    })?;
51    read()?;
52    restore_terminal(terminal)?;
53    Ok(())
54}
Source

pub fn highlight_lines<'a, T>( &self, source: T, syntax: &SyntaxReference, syntaxes: &SyntaxSet, ) -> Result<Text<'static>, Error>
where T: IntoIterator<Item = &'a str>,

Highlights text from an iterator.

Source

pub fn highlight_line( &self, line: &str, highlighter: &mut HighlightLines<'_>, line_number: usize, line_number_style: Style, syntaxes: &SyntaxSet, ) -> Result<Line<'static>, Error>

Highlights a single line.

Trait Implementations§

Source§

impl Clone for Highlighter

Source§

fn clone(&self) -> Highlighter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Highlighter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.