on master help sign in
Separate syntax set and theme set

This commit separates the handling of syntax sets and theme sets. It also changes the way how new syntax definitions are loaded from `bat`'s configuration folder. New syntax definitions are now loaded *in addition* to the ones that are stored in the `bat` binary by default. This fixes #172

commit1dddce3aa11818fd641542f5f79c20e5ecb5b837 authorsharkdp <davidpeter@web.de> dateSun 19 Aug 2018, 10:49 (8 years ago) parent76be0d3
2 files changed+62−44
README.md+34−25 src/assets.rs+28−19
README.md+34−25
@@ -140,50 +140,33 @@ You can use the `--style` option to control the appearance of `bat`s output.
140140You can use `--style=numbers,changes`, for example, to show only Git changes
141141and line numbers but no grid and no file header.
142142
143−### Add new syntaxes and highlighting themes
143+### Adding new syntaxes / language definitions
144144
145145`bat` uses the excellent [`syntect`](https://github.com/trishume/syntect/)
146146library for syntax highlighting. `syntect` can read any
147147[Sublime Text `.sublime-syntax` file](https://www.sublimetext.com/docs/3/syntax.html)
148−and theme.
148+and theme. To add new syntax definitions, do the following.
149149
150−To build your own language-set and theme, follow these steps:
151−
152−Create a folder with a syntax highlighting theme:
150+Create a folder with syntax definition files:
153151
154152```bash
155153BAT_CONFIG_DIR="$(bat cache --config-dir)"
156154
157−mkdir -p "$BAT_CONFIG_DIR/themes"
158−cd "$BAT_CONFIG_DIR/themes"
159−
160−# Download a theme, for example:
161−git clone https://github.com/greggb/sublime-snazzy
162−
163−# Create a link for the default theme
164−ln -sf "sublime-snazzy/Sublime Snazzy.tmTheme" Default.tmTheme
165−```
166−
167−Create a folder with language definition files:
168−
169−```bash
170155mkdir -p "$BAT_CONFIG_DIR/syntaxes"
171156cd "$BAT_CONFIG_DIR/syntaxes"
172157
173−# Download some language definition files, for example:
174−git clone https://github.com/sublimehq/Packages
175−git clone https://github.com/danro/LESS-sublime
158+# Put new '.sublime-syntax' language definition files
159+# in this folder (or its subdirectories), for example:
160+git clone https://github.com/tellnobody1/sublime-purescript-syntax
176161```
177162
178−Finally, use the following command to parse all these files into a binary
179−cache:
163+Now use the following command to parse these files into a binary cache:
180164
181165```bash
182166bat cache --init
183167```
184168
185−Use `bat --list-languages` and `bat --list-themes` to check if all languages and themes are
186−available.
169+Finally, use `bat --list-languages` to check if the new languages are available.
187170
188171If you ever want to go back to the default settings, call:
189172
@@ -191,6 +174,32 @@ If you ever want to go back to the default settings, call:
191174bat cache --clear
192175```
193176
177+### Adding new themes
178+
179+This works very similar to how we add new syntax definitions.
180+
181+First, create a folder with the new syntax highlighting themes:
182+```bash
183+BAT_CONFIG_DIR="$(bat cache --config-dir)"
184+
185+mkdir -p "$BAT_CONFIG_DIR/themes"
186+cd "$BAT_CONFIG_DIR/themes"
187+
188+# Download a theme in '.tmTheme' format, for example:
189+git clone https://github.com/greggb/sublime-snazzy
190+
191+# Create a link to specify the new default theme
192+ln -sf "sublime-snazzy/Sublime Snazzy.tmTheme" Default.tmTheme
193+
194+# Update the binary cache
195+bat cache --init
196+```
197+
198+Finally, use `bat --list-themes` to check if the new themes are available.
199+
200+**Note:** Unlike for syntax definitions, adding custom themes currently *removes all default
201+themes*. If you want to go back to the default themes, call `bat cache --clear`.
202+
194203### Using a different pager
195204
196205`bat` uses the pager that is specified in the `PAGER` environment variable. If this variable is not
src/assets.rs+28−19
@@ -28,28 +28,32 @@ impl HighlightingAssets {
2828 pub fn from_files(dir: Option<&Path>) -> Result<Self> {
2929 let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
3030
31+ let mut assets = Self::from_binary_unlinked();
32+
3133 let theme_dir = source_dir.join("themes");
32− let theme_set = ThemeSet::load_from_folder(&theme_dir).chain_err(|| {
33− format!(
34− "Could not load themes from '{}'",
34+
35+ if let Ok(theme_set) = ThemeSet::load_from_folder(&theme_dir) {
36+ // TODO: If syntect would support this, it would be great to
37+ // load the new themes in addition to the ones in the binary.
38+ assets.theme_set = theme_set;
39+ } else {
40+ println!(
41+ "No themes were found in '{}', using the default set",
3542 theme_dir.to_string_lossy()
36− )
37− })?;
38− let mut syntax_set = SyntaxSet::new();
43+ );
44+ }
45+
3946 let syntax_dir = source_dir.join("syntaxes");
40− if !syntax_dir.exists() {
41− return Err(format!(
42− "Could not load syntaxes from '{}'",
47+ if syntax_dir.exists() {
48+ assets.syntax_set.load_syntaxes(syntax_dir, true)?;
49+ } else {
50+ println!(
51+ "No syntaxes were found in '{}', using the default set.",
4352 syntax_dir.to_string_lossy()
44− ).into());
53+ );
4554 }
46− syntax_set.load_syntaxes(syntax_dir, true)?;
47− syntax_set.load_plain_text_syntax();
4855
49− Ok(HighlightingAssets {
50− syntax_set,
51− theme_set,
52− })
56+ Ok(assets)
5357 }
5458
5559 fn from_cache() -> Result<Self> {
@@ -79,9 +83,8 @@ impl HighlightingAssets {
7983 })
8084 }
8185
82− fn from_binary() -> Self {
83− let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntaxes.bin"));
84− syntax_set.link_syntaxes();
86+ fn from_binary_unlinked() -> Self {
87+ let syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntaxes.bin"));
8588 let theme_set: ThemeSet = from_binary(include_bytes!("../assets/themes.bin"));
8689
8790 HighlightingAssets {
@@ -90,6 +93,12 @@ impl HighlightingAssets {
9093 }
9194 }
9295
96+ fn from_binary() -> Self {
97+ let mut assets = Self::from_binary_unlinked();
98+ assets.syntax_set.link_syntaxes();
99+ assets
100+ }
101+
93102 pub fn save(&self, dir: Option<&Path>) -> Result<()> {
94103 let target_dir = dir.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
95104 let _ = fs::create_dir(target_dir);