Struct syntect_assets::assets::HighlightingAssets
source · pub struct HighlightingAssets { /* private fields */ }Implementations§
source§impl HighlightingAssets
impl HighlightingAssets
sourcepub fn default_theme() -> &'static str
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.
pub fn from_cache(cache_path: &Path) -> Result<Self, Error>
sourcepub fn from_binary() -> Self
pub fn from_binary() -> Self
Examples found in repository?
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
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);
}
}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(®ions[..], 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
}
}pub fn set_fallback_theme(&mut self, theme: &'static str)
sourcepub fn get_syntax_set(&self) -> Result<&SyntaxSet, Error>
pub fn get_syntax_set(&self) -> Result<&SyntaxSet, Error>
Return the collection of syntect syntax definitions.
Examples found in repository?
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
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(®ions[..], 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
}
}sourcepub fn get_syntaxes(&self) -> Result<&[SyntaxReference], Error>
pub fn get_syntaxes(&self) -> Result<&[SyntaxReference], Error>
Examples found in repository?
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);
}
}sourcepub fn themes(&self) -> impl Iterator<Item = &str>
pub fn themes(&self) -> impl Iterator<Item = &str>
Examples found in repository?
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);
}
}sourcepub fn get_syntax_for_path(
&self,
path: impl AsRef<Path>,
mapping: &SyntaxMapping<'_>
) -> Result<SyntaxReferenceInSet<'_>, Error>
pub fn get_syntax_for_path( &self, path: impl AsRef<Path>, mapping: &SyntaxMapping<'_> ) -> Result<SyntaxReferenceInSet<'_>, Error>
Detect the syntax based on, in order:
- Syntax mappings with [MappingTarget::MapTo] and [MappingTarget::MapToUnknown]
(e.g.
/etc/profile->Bourne Again Shell (bash)) - The file name (e.g.
Dockerfile) - Syntax mappings with [MappingTarget::MapExtensionToUnknown]
(e.g.
*.conf) - 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.
sourcepub fn get_theme(&self, theme: &str) -> &Theme
pub fn get_theme(&self, theme: &str) -> &Theme
Look up a syntect theme by name.
Examples found in repository?
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
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(®ions[..], 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
}
}