1use std::collections::BTreeMap;
3use std::fs::{self, File};
7use syntect::dumps::{dump_to_file, from_binary, from_reader};
8use syntect::highlighting::{Theme, ThemeSet};
9use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
11use crate::assets_metadata::AssetsMetadata;
13use crate::input::{InputReader, OpenedInput, OpenedInputKind};
14use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
17pub struct HighlightingAssets {
18 pub(crate) syntax_set: SyntaxSet,
19 pub(crate) theme_set: ThemeSet,
20 fallback_theme: Option<&'static str>,
23impl HighlightingAssets {
24 pub fn default_theme() -> &'static str {
28 pub fn from_files(source_dir: &Path, include_integrated_assets: bool) -> Result<Self> {
29 let mut theme_set = if include_integrated_assets {
30 Self::get_integrated_themeset()
33 themes: BTreeMap::new(),
37 let theme_dir = source_dir.join("themes");
39 let res = theme_set.add_from_folder(&theme_dir);
42 "No themes were found in '{}', using the default set",
43 theme_dir.to_string_lossy()
47 let mut syntax_set_builder = if !include_integrated_assets {
48 let mut builder = SyntaxSetBuilder::new();
49 builder.add_plain_text_syntax();
52 Self::get_integrated_syntaxset().into_builder()
55 let syntax_dir = source_dir.join("syntaxes");
56 if syntax_dir.exists() {
57 syntax_set_builder.add_from_folder(syntax_dir, true)?;
60 "No syntaxes were found in '{}', using the default set.",
61 syntax_dir.to_string_lossy()
65 Ok(HighlightingAssets {
66 syntax_set: syntax_set_builder.build(),
72 pub fn from_cache(cache_path: &Path) -> Result<Self> {
73 let syntax_set_path = cache_path.join("syntaxes.bin");
74 let theme_set_path = cache_path.join("themes.bin");
76 let syntax_set_file = File::open(&syntax_set_path).chain_err(|| {
78 "Could not load cached syntax set '{}'",
79 syntax_set_path.to_string_lossy()
82 let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file))
83 .chain_err(|| "Could not parse cached syntax set")?;
85 let theme_set_file = File::open(&theme_set_path).chain_err(|| {
87 "Could not load cached theme set '{}'",
88 theme_set_path.to_string_lossy()
91 let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file))
92 .chain_err(|| "Could not parse cached theme set")?;
94 Ok(HighlightingAssets {
101 fn get_integrated_syntaxset() -> SyntaxSet {
102 from_binary(include_bytes!("../assets/syntaxes.bin"))
105 fn get_integrated_themeset() -> ThemeSet {
106 from_binary(include_bytes!("../assets/themes.bin"))
109 pub fn from_binary() -> Self {
110 let syntax_set = Self::get_integrated_syntaxset();
111 let theme_set = Self::get_integrated_themeset();
116 fallback_theme: None,
120 pub fn save_to_cache(&self, target_dir: &Path, current_version: &str) -> Result<()> {
121 let _ = fs::create_dir_all(target_dir);
122 let theme_set_path = target_dir.join("themes.bin");
123 let syntax_set_path = target_dir.join("syntaxes.bin");
126 "Writing theme set to {} ... ",
127 theme_set_path.to_string_lossy()
129 dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
131 "Could not save theme set to {}",
132 theme_set_path.to_string_lossy()
138 "Writing syntax set to {} ... ",
139 syntax_set_path.to_string_lossy()
141 dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
143 "Could not save syntax set to {}",
144 syntax_set_path.to_string_lossy()
150 "Writing metadata to folder {} ... ",
151 target_dir.to_string_lossy()
153 AssetsMetadata::new(current_version).save_to_folder(target_dir)?;
159 pub fn set_fallback_theme(&mut self, theme: &'static str) {
160 self.fallback_theme = Some(theme);
163 pub fn syntaxes(&self) -> &[SyntaxReference] {
164 self.syntax_set.syntaxes()
167 pub fn themes(&self) -> impl Iterator<Item = &str> {
168 self.theme_set.themes.keys().map(|s| s.as_ref())
171 pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
172 match self.theme_set.themes.get(theme) {
173 Some(theme) => theme,
176 use ansi_term::Colour::Yellow;
178 "{}: Unknown theme '{}', using default.",
179 Yellow.paint("[bat warning]"),
183 &self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
188 pub(crate) fn get_syntax(
190 language: Option<&str>,
191 input: &mut OpenedInput,
192 mapping: &SyntaxMapping,
193 ) -> &SyntaxReference {
194 let syntax = if let Some(language) = language {
195 self.syntax_set.find_syntax_by_token(language)
198 OpenedInputKind::OrdinaryFile(ref actual_path) => {
203 .unwrap_or(actual_path);
204 let path = Path::new(path_str);
205 let line_syntax = self.get_first_line_syntax(&mut input.reader);
207 let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());
208 match mapping.get_syntax_for(absolute_path) {
209 Some(MappingTarget::MapTo(syntax_name)) => {
210 // TODO: we should probably return an error here if this syntax can not be
211 // found. Currently, we just fall back to 'plain'.
212 self.syntax_set.find_syntax_by_name(syntax_name)
214 Some(MappingTarget::MapToUnknown) => line_syntax,
216 let file_name = path.file_name().unwrap_or_default();
217 self.get_extension_syntax(file_name).or(line_syntax)
221 OpenedInputKind::StdIn | OpenedInputKind::CustomReader => {
222 if let Some(ref name) = input.metadata.user_provided_name {
223 self.get_extension_syntax(&name)
224 .or_else(|| self.get_first_line_syntax(&mut input.reader))
226 self.get_first_line_syntax(&mut input.reader)
229 OpenedInputKind::ThemePreviewFile => self.syntax_set.find_syntax_by_name("Rust"),
233 syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
236 fn get_extension_syntax(&self, file_name: &OsStr) -> Option<&SyntaxReference> {
238 .find_syntax_by_extension(file_name.to_str().unwrap_or_default())
240 self.syntax_set.find_syntax_by_extension(
243 .and_then(|x| x.to_str())
244 .unwrap_or_default(),
249 fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> {
250 String::from_utf8(reader.first_line.clone())
252 .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
264 use tempdir::TempDir;
266 use crate::input::Input;
268 struct SyntaxDetectionTest<'a> {
269 assets: HighlightingAssets,
270 pub syntax_mapping: SyntaxMapping<'a>,
274 impl<'a> SyntaxDetectionTest<'a> {
276 SyntaxDetectionTest {
277 assets: HighlightingAssets::from_binary(),
278 syntax_mapping: SyntaxMapping::builtin(),
279 temp_dir: TempDir::new("bat_syntax_detection_tests")
280 .expect("creation of temporary directory"),
284 fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String {
285 let file_path = self.temp_dir.path().join(file_name);
287 let mut temp_file = File::create(&file_path).unwrap();
288 writeln!(temp_file, "{}", first_line).unwrap();
291 let input = Input::ordinary_file(file_path.as_os_str());
292 let dummy_stdin: &[u8] = &[];
293 let mut opened_input = input.open(dummy_stdin).unwrap();
296 .get_syntax(None, &mut opened_input, &self.syntax_mapping);
301 fn syntax_for_file_os(&self, file_name: &OsStr) -> String {
302 self.syntax_for_file_with_content_os(file_name, "")
305 fn syntax_for_file_with_content(&self, file_name: &str, first_line: &str) -> String {
306 self.syntax_for_file_with_content_os(OsStr::new(file_name), first_line)
309 fn syntax_for_file(&self, file_name: &str) -> String {
310 self.syntax_for_file_with_content(file_name, "")
313 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
314 let input = Input::stdin().with_name(Some(OsStr::new(file_name)));
315 let mut opened_input = input.open(content).unwrap();
319 .get_syntax(None, &mut opened_input, &self.syntax_mapping);
325 fn syntax_detection_basic() {
326 let test = SyntaxDetectionTest::new();
328 assert_eq!(test.syntax_for_file("test.rs"), "Rust");
329 assert_eq!(test.syntax_for_file("test.cpp"), "C++");
330 assert_eq!(test.syntax_for_file("test.build"), "NAnt Build File");
332 test.syntax_for_file("PKGBUILD"),
333 "Bourne Again Shell (bash)"
335 assert_eq!(test.syntax_for_file(".bashrc"), "Bourne Again Shell (bash)");
336 assert_eq!(test.syntax_for_file("Makefile"), "Makefile");
341 fn syntax_detection_invalid_utf8() {
342 use std::os::unix::ffi::OsStrExt;
344 let test = SyntaxDetectionTest::new();
347 test.syntax_for_file_os(OsStr::from_bytes(b"invalid_\xFEutf8_filename.rs")),
353 fn syntax_detection_well_defined_mapping_for_duplicate_extensions() {
354 let test = SyntaxDetectionTest::new();
356 assert_eq!(test.syntax_for_file("test.h"), "C++");
357 assert_eq!(test.syntax_for_file("test.sass"), "Sass");
358 assert_eq!(test.syntax_for_file("test.hs"), "Haskell (improved)");
359 assert_eq!(test.syntax_for_file("test.js"), "JavaScript (Babel)");
363 fn syntax_detection_first_line() {
364 let test = SyntaxDetectionTest::new();
367 test.syntax_for_file_with_content("my_script", "#!/bin/bash"),
368 "Bourne Again Shell (bash)"
371 test.syntax_for_file_with_content("build", "#!/bin/bash"),
372 "Bourne Again Shell (bash)"
375 test.syntax_for_file_with_content("my_script", "<?php"),
381 fn syntax_detection_with_custom_mapping() {
382 let mut test = SyntaxDetectionTest::new();
384 assert_eq!(test.syntax_for_file("test.h"), "C++");
386 .insert("*.h", MappingTarget::MapTo("C"))
388 assert_eq!(test.syntax_for_file("test.h"), "C");
392 fn syntax_detection_is_case_sensitive() {
393 let mut test = SyntaxDetectionTest::new();
395 assert_ne!(test.syntax_for_file("README.MD"), "Markdown");
397 .insert("*.MD", MappingTarget::MapTo("Markdown"))
399 assert_eq!(test.syntax_for_file("README.MD"), "Markdown");
403 fn syntax_detection_stdin_filename() {
404 let test = SyntaxDetectionTest::new();
406 // from file extension
407 assert_eq!(test.syntax_for_stdin_with_content("test.cpp", b"a"), "C++");
408 // from first line (fallback)
410 test.syntax_for_stdin_with_content("my_script", b"#!/bin/bash"),
411 "Bourne Again Shell (bash)"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list