pub struct HighlightingAssets { /* private fields */ }

Implementations§

source§

impl HighlightingAssets

source

pub fn default_theme() -> &'static str

The default theme.

Windows and Linux

Windows and most Linux distributions has a dark terminal theme by default. On these platforms, this function always returns a theme that looks good on a dark background.

macOS

On macOS the default terminal background is light, but it is common that Dark Mode is active, which makes the terminal background dark. On this platform, the default theme depends on

defaults read -globalDomain AppleInterfaceStyle

To avoid the overhead of the check on macOS, simply specify a theme explicitly via --theme, BAT_THEME, or ~/.config/syntect-assets.

See https://github.com/sharkdp/bat/issues/1746 and https://github.com/sharkdp/bat/issues/1928 for more context.

source

pub fn from_cache(cache_path: &Path) -> Result<Self, Error>

source

pub fn from_binary() -> Self

Examples found in repository?
examples/list_syntaxes_and_themes.rs (line 5)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let assets = HighlightingAssets::from_binary();

    println!("Syntaxes:");
    for syntax in assets.get_syntaxes().unwrap() {
        println!("- {} ({})", syntax.name, syntax.file_extensions.join(", "));
    }

    println!();

    println!("Themes:");
    for theme in assets.themes() {
        println!("- {}", theme);
    }
}
More examples
Hide additional examples
examples/line.rs (line 8)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    // Load these once at the start of your program
    let assets = HighlightingAssets::from_binary();
    let ss = assets.get_syntax_set().unwrap();
    let syntax = ss.find_syntax_by_extension("rs").unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut h = HighlightLines::new(syntax, theme);
    let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}\n";
    for line in LinesWithEndings::from(s) { // LinesWithEndings enables use of newlines mode
        let ranges: Vec<(Style, &str)> = h.highlight_line(line, ss).unwrap();
        let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
        print!("{}", escaped);
    }
}
examples/simple.rs (line 10)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let assets = HighlightingAssets::from_binary();

    let ss = assets.get_syntax_set().unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut highlighter = HighlightFile::new(file!(), ss, theme).unwrap();
    let mut line = String::new();
    while highlighter.reader.read_line(&mut line).unwrap() > 0 {
        {
            let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight_line(&line, &ss).unwrap();
            print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
        } // until NLL this scope is needed so we can clear the buffer after
        line.clear(); // read_line appends so we need to clear between lines
    }
}
source

pub fn set_fallback_theme(&mut self, theme: &'static str)

source

pub fn get_syntax_set(&self) -> Result<&SyntaxSet, Error>

Return the collection of syntect syntax definitions.

Examples found in repository?
examples/line.rs (line 9)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    // Load these once at the start of your program
    let assets = HighlightingAssets::from_binary();
    let ss = assets.get_syntax_set().unwrap();
    let syntax = ss.find_syntax_by_extension("rs").unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut h = HighlightLines::new(syntax, theme);
    let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}\n";
    for line in LinesWithEndings::from(s) { // LinesWithEndings enables use of newlines mode
        let ranges: Vec<(Style, &str)> = h.highlight_line(line, ss).unwrap();
        let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
        print!("{}", escaped);
    }
}
More examples
Hide additional examples
examples/simple.rs (line 12)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let assets = HighlightingAssets::from_binary();

    let ss = assets.get_syntax_set().unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut highlighter = HighlightFile::new(file!(), ss, theme).unwrap();
    let mut line = String::new();
    while highlighter.reader.read_line(&mut line).unwrap() > 0 {
        {
            let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight_line(&line, &ss).unwrap();
            print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
        } // until NLL this scope is needed so we can clear the buffer after
        line.clear(); // read_line appends so we need to clear between lines
    }
}
source

pub fn get_syntaxes(&self) -> Result<&[SyntaxReference], Error>

Examples found in repository?
examples/list_syntaxes_and_themes.rs (line 8)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let assets = HighlightingAssets::from_binary();

    println!("Syntaxes:");
    for syntax in assets.get_syntaxes().unwrap() {
        println!("- {} ({})", syntax.name, syntax.file_extensions.join(", "));
    }

    println!();

    println!("Themes:");
    for theme in assets.themes() {
        println!("- {}", theme);
    }
}
source

pub fn themes(&self) -> impl Iterator<Item = &str>

Examples found in repository?
examples/list_syntaxes_and_themes.rs (line 15)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let assets = HighlightingAssets::from_binary();

    println!("Syntaxes:");
    for syntax in assets.get_syntaxes().unwrap() {
        println!("- {} ({})", syntax.name, syntax.file_extensions.join(", "));
    }

    println!();

    println!("Themes:");
    for theme in assets.themes() {
        println!("- {}", theme);
    }
}
source

pub fn get_syntax_for_path( &self, path: impl AsRef<Path>, mapping: &SyntaxMapping<'_> ) -> Result<SyntaxReferenceInSet<'_>, Error>

Detect the syntax based on, in order:

  1. Syntax mappings with [MappingTarget::MapTo] and [MappingTarget::MapToUnknown] (e.g. /etc/profile -> Bourne Again Shell (bash))
  2. The file name (e.g. Dockerfile)
  3. Syntax mappings with [MappingTarget::MapExtensionToUnknown] (e.g. *.conf)
  4. The file name extension (e.g. .rs)

When detecting syntax based on syntax mappings, the full path is taken into account. When detecting syntax based on file name, no regard is taken to the path of the file. Only the file name itself matters. When detecting syntax based on file name extension, only the file name extension itself matters.

Returns [Error::UndetectedSyntax] if it was not possible detect syntax based on path/file name/extension (or if the path was mapped to [MappingTarget::MapToUnknown] or [MappingTarget::MapExtensionToUnknown]). In this case it is appropriate to fall back to other methods to detect syntax. Such as using the contents of the first line of the file.

Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped syntax does not exist.

source

pub fn get_theme(&self, theme: &str) -> &Theme

Look up a syntect theme by name.

Examples found in repository?
examples/line.rs (line 11)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    // Load these once at the start of your program
    let assets = HighlightingAssets::from_binary();
    let ss = assets.get_syntax_set().unwrap();
    let syntax = ss.find_syntax_by_extension("rs").unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut h = HighlightLines::new(syntax, theme);
    let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}\n";
    for line in LinesWithEndings::from(s) { // LinesWithEndings enables use of newlines mode
        let ranges: Vec<(Style, &str)> = h.highlight_line(line, ss).unwrap();
        let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
        print!("{}", escaped);
    }
}
More examples
Hide additional examples
examples/simple.rs (line 13)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let assets = HighlightingAssets::from_binary();

    let ss = assets.get_syntax_set().unwrap();
    let theme = assets.get_theme("OneHalfDark");

    let mut highlighter = HighlightFile::new(file!(), ss, theme).unwrap();
    let mut line = String::new();
    while highlighter.reader.read_line(&mut line).unwrap() > 0 {
        {
            let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight_line(&line, &ss).unwrap();
            print!("{}", as_24_bit_terminal_escaped(&regions[..], true));
        } // until NLL this scope is needed so we can clear the buffer after
        line.clear(); // read_line appends so we need to clear between lines
    }
}

Trait Implementations§

source§

impl Debug for HighlightingAssets

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.