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
impl Highlighter
Sourcepub fn new(theme: Theme) -> Self
pub fn new(theme: Theme) -> Self
Creates a new Highlighter with the given Theme.
Examples found in repository?
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
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}Sourcepub fn override_background<C>(self, background: C) -> Self
pub fn override_background<C>(self, background: C) -> Self
Override the background with a different color.
Set this to Color::Reset to disable the background color.
Sourcepub fn line_numbers(self, line_numbers: bool) -> Self
pub fn line_numbers(self, line_numbers: bool) -> Self
Enable or disable line numbers in the left gutter.
Sourcepub fn line_number_padding(self, padding: usize) -> Self
pub fn line_number_padding(self, padding: usize) -> Self
Set the padding between the line number section and the rest of the code.
Sourcepub fn line_number_style<S>(self, style: S) -> Self
pub fn line_number_style<S>(self, style: S) -> Self
Set the Style for the line number section.
Sourcepub fn line_number_separator_style<S>(self, style: S) -> Self
pub fn line_number_separator_style<S>(self, style: S) -> Self
Set the Style for the separator between the line number section and the rest of the code.
Sourcepub fn line_number_separator<T>(self, separator: T) -> Self
pub fn line_number_separator<T>(self, separator: T) -> Self
Set the text used for the line number separator. | is used by default.
Sourcepub fn highlight_range(self, range: Range<usize>) -> Self
pub fn highlight_range(self, range: Range<usize>) -> Self
Highlight a specific range of code with a different style.
Sourcepub fn highlight_style(self, style: Style) -> Self
pub fn highlight_style(self, style: Style) -> Self
Set the style used for highlight_range. A yellow background is used by default.
Sourcepub fn gutter_template<F>(self, template: F) -> Self
pub fn gutter_template<F>(self, template: F) -> Self
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.
Sourcepub fn get_background_color(&self) -> Option<Color>
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?
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}Sourcepub fn get_line_number_style(&self) -> Style
pub fn get_line_number_style(&self) -> Style
Returns the configured line number style, accounting for both the theme and any overrides.
Sourcepub fn highlight_reader<R>(
&self,
reader: R,
syntax: &SyntaxReference,
syntaxes: &SyntaxSet,
) -> Result<Text<'static>, Error>where
R: Read,
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?
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
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}Sourcepub fn highlight_lines<'a, T>(
&self,
source: T,
syntax: &SyntaxReference,
syntaxes: &SyntaxSet,
) -> Result<Text<'static>, Error>where
T: IntoIterator<Item = &'a str>,
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.
Sourcepub fn highlight_line(
&self,
line: &str,
highlighter: &mut HighlightLines<'_>,
line_number: usize,
line_number_style: Style,
syntaxes: &SyntaxSet,
) -> Result<Line<'static>, Error>
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
impl Clone for Highlighter
Source§fn clone(&self) -> Highlighter
fn clone(&self) -> Highlighter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for Highlighter
impl !RefUnwindSafe for Highlighter
impl Send for Highlighter
impl Sync for Highlighter
impl Unpin for Highlighter
impl !UnwindSafe for Highlighter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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