Skip to content

Commit 1403fe2

Browse files
authored
Use assets API from bat library instead of vendored code (#903)
Use assets API from bat library Fixes #895 Ref sharkdp/bat#2026 Thanks @Enselic
1 parent e44435c commit 1403fe2

File tree

12 files changed

+357
-139
lines changed

12 files changed

+357
-139
lines changed

Cargo.lock

Lines changed: 316 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ name = "delta"
1515
path = "src/main.rs"
1616

1717
[dependencies]
18+
bat = "0.20.0"
1819
chrono = "0.4.19"
1920
chrono-humanize = "0.2.1"
2021
ansi_colours = "1.0.4"

etc/assets/syntaxes.bin

-685 KB
Binary file not shown.

etc/assets/themes.bin

-20.9 KB
Binary file not shown.

src/cli.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
22
use std::ffi::OsString;
33
use std::path::PathBuf;
44

5+
use bat::assets::HighlightingAssets;
56
use clap::{AppSettings, ColorChoice, FromArgMatches, IntoApp, Parser};
67
use lazy_static::lazy_static;
78
use syntect::highlighting::Theme as SyntaxTheme;
@@ -10,7 +11,7 @@ use syntect::parsing::SyntaxSet;
1011
use crate::config::delta_unreachable;
1112
use crate::git_config::{GitConfig, GitConfigEntry};
1213
use crate::options;
13-
use crate::utils::bat::assets::HighlightingAssets;
14+
use crate::utils;
1415
use crate::utils::bat::output::PagingMode;
1516

1617
#[derive(Parser)]
@@ -1130,7 +1131,7 @@ impl Opt {
11301131
I: IntoIterator,
11311132
I::Item: Into<OsString> + Clone,
11321133
{
1133-
let assets = HighlightingAssets::new();
1134+
let assets = utils::bat::assets::load_highlighting_assets();
11341135
Self::from_clap_and_git_config(Self::into_app().get_matches_from(iter), git_config, assets)
11351136
}
11361137

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::process;
3535
use bytelines::ByteLinesReader;
3636

3737
use crate::delta::delta;
38-
use crate::utils::bat::assets::{list_languages, HighlightingAssets};
38+
use crate::utils::bat::assets::list_languages;
3939
use crate::utils::bat::output::OutputType;
4040

4141
pub fn fatal<T>(errmsg: T) -> !
@@ -85,7 +85,7 @@ fn main() -> std::io::Result<()> {
8585
// report that two files differ when delta is called with two positional
8686
// arguments and without standard input; 2 is used to report a real problem.
8787
fn run_app() -> std::io::Result<i32> {
88-
let assets = HighlightingAssets::new();
88+
let assets = utils::bat::assets::load_highlighting_assets();
8989
let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets);
9090

9191
let subcommand_result = if opt.list_languages {

src/options/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::convert::TryInto;
33
use std::result::Result;
44
use std::str::FromStr;
55

6+
use bat::assets::HighlightingAssets;
67
use console::Term;
78

89
use crate::cli;
@@ -14,7 +15,6 @@ use crate::features;
1415
use crate::git_config::{GitConfig, GitConfigEntry};
1516
use crate::options::option_value::{OptionValue, ProvenancedOptionValue};
1617
use crate::options::theme;
17-
use crate::utils::bat::assets::HighlightingAssets;
1818
use crate::utils::bat::output::PagingMode;
1919

2020
macro_rules! set_options {

src/options/theme.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
/// by the user, it is determined by the classification of the syntax theme into light-background
77
/// vs dark-background syntax themes. If the user didn't choose a syntax theme, a dark-background
88
/// default is selected.
9-
use syntect::highlighting::ThemeSet;
9+
use bat;
10+
use bat::assets::HighlightingAssets;
1011

1112
use crate::cli;
1213
use crate::env;
13-
use crate::utils::bat::assets::HighlightingAssets;
1414

1515
#[allow(non_snake_case)]
1616
pub fn set__is_light_mode__syntax_theme__syntax_set(
@@ -22,16 +22,15 @@ pub fn set__is_light_mode__syntax_theme__syntax_set(
2222
opt.syntax_theme.as_ref(),
2323
syntax_theme_name_from_bat_theme.as_ref(),
2424
opt.light,
25-
&assets.theme_set,
2625
);
2726
opt.computed.is_light_mode = is_light_mode;
2827

2928
opt.computed.syntax_theme = if is_no_syntax_highlighting_syntax_theme_name(&syntax_theme_name) {
3029
None
3130
} else {
32-
Some(assets.theme_set.themes[&syntax_theme_name].clone())
31+
Some(assets.get_theme(&syntax_theme_name).clone())
3332
};
34-
opt.computed.syntax_set = assets.syntax_set;
33+
opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone();
3534
}
3635

3736
pub fn is_light_syntax_theme(theme: &str) -> bool {
@@ -86,34 +85,16 @@ fn get_is_light_mode_and_syntax_theme_name(
8685
theme_arg: Option<&String>,
8786
bat_theme_env_var: Option<&String>,
8887
light_mode_arg: bool,
89-
theme_set: &ThemeSet,
9088
) -> (bool, String) {
91-
let theme_arg = valid_syntax_theme_name_or_none(theme_arg, theme_set);
92-
let bat_theme_env_var = valid_syntax_theme_name_or_none(bat_theme_env_var, theme_set);
9389
match (theme_arg, bat_theme_env_var, light_mode_arg) {
9490
(None, None, false) => (false, DEFAULT_DARK_SYNTAX_THEME.to_string()),
95-
(Some(theme_name), _, false) => (is_light_syntax_theme(&theme_name), theme_name),
96-
(None, Some(theme_name), false) => (is_light_syntax_theme(&theme_name), theme_name),
97-
(None, None, true) => (true, DEFAULT_LIGHT_SYNTAX_THEME.to_string()),
98-
(Some(theme_name), _, is_light_mode) => (is_light_mode, theme_name),
99-
(None, Some(theme_name), is_light_mode) => (is_light_mode, theme_name),
100-
}
101-
}
102-
103-
// At this stage the theme name is considered valid if it is either a real theme name or the special
104-
// no-syntax-highlighting name.
105-
fn valid_syntax_theme_name_or_none(
106-
theme_name: Option<&String>,
107-
theme_set: &ThemeSet,
108-
) -> Option<String> {
109-
match theme_name {
110-
Some(name)
111-
if is_no_syntax_highlighting_syntax_theme_name(name)
112-
|| theme_set.themes.contains_key(name) =>
113-
{
114-
Some(name.to_string())
91+
(Some(theme_name), _, false) => (is_light_syntax_theme(theme_name), theme_name.to_string()),
92+
(None, Some(theme_name), false) => {
93+
(is_light_syntax_theme(theme_name), theme_name.to_string())
11594
}
116-
_ => None,
95+
(None, None, true) => (true, DEFAULT_LIGHT_SYNTAX_THEME.to_string()),
96+
(Some(theme_name), _, is_light_mode) => (is_light_mode, theme_name.to_string()),
97+
(None, Some(theme_name), is_light_mode) => (is_light_mode, theme_name.to_string()),
11798
}
11899
}
119100

src/subcommands/list_syntax_themes.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::io::{self, Write};
22

33
use itertools::Itertools;
44

5-
use crate::options::theme::is_light_syntax_theme;
6-
use crate::utils::bat::assets::HighlightingAssets;
5+
use crate::{options::theme::is_light_syntax_theme, utils};
76

87
#[cfg(not(tarpaulin_include))]
98
pub fn list_syntax_themes() -> std::io::Result<()> {
@@ -17,15 +16,14 @@ pub fn list_syntax_themes() -> std::io::Result<()> {
1716
}
1817

1918
pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result<()> {
20-
let assets = HighlightingAssets::new();
21-
let themes = &assets.theme_set.themes;
19+
let assets = utils::bat::assets::load_highlighting_assets();
2220

2321
writeln!(writer, "Light syntax themes:")?;
24-
for (theme, _) in themes.iter().filter(|(t, _)| is_light_syntax_theme(*t)) {
22+
for theme in assets.themes().filter(|t| is_light_syntax_theme(*t)) {
2523
writeln!(writer, " {}", theme)?;
2624
}
2725
writeln!(writer, "\nDark syntax themes:")?;
28-
for (theme, _) in themes.iter().filter(|(t, _)| !is_light_syntax_theme(*t)) {
26+
for theme in assets.themes().filter(|t| !is_light_syntax_theme(*t)) {
2927
writeln!(writer, " {}", theme)?;
3028
}
3129
writeln!(
@@ -36,12 +34,8 @@ pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result
3634
}
3735

3836
pub fn _list_syntax_themes_for_machines(writer: &mut dyn Write) -> std::io::Result<()> {
39-
let assets = HighlightingAssets::new();
40-
let themes = &assets.theme_set.themes;
41-
for (theme, _) in themes
42-
.iter()
43-
.sorted_by_key(|(t, _)| is_light_syntax_theme(*t))
44-
{
37+
let assets = utils::bat::assets::load_highlighting_assets();
38+
for theme in assets.themes().sorted_by_key(|t| is_light_syntax_theme(*t)) {
4539
writeln!(
4640
writer,
4741
"{}\t{}",

src/subcommands/show_colors.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ use crate::git_config;
77
use crate::paint;
88
use crate::paint::BgShouldFill;
99
use crate::style;
10-
use crate::utils::bat::assets::HighlightingAssets;
1110
use crate::utils::bat::output::{OutputType, PagingMode};
1211

1312
#[cfg(not(tarpaulin_include))]
1413
pub fn show_colors() -> std::io::Result<()> {
1514
use itertools::Itertools;
1615

17-
use crate::delta::DiffType;
16+
use crate::{delta::DiffType, utils};
1817

19-
let assets = HighlightingAssets::new();
18+
let assets = utils::bat::assets::load_highlighting_assets();
2019
let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets);
2120
let config = config::Config::from(opt);
2221

0 commit comments

Comments
 (0)