diff --git a/Cargo.lock b/Cargo.lock index 7a460881b..f5652d7fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,11 +19,11 @@ dependencies = [ [[package]] name = "ansi_colours" -version = "1.0.4" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60e2fb6138a49ad9f1cb3c6d8f8ccbdd5e62b4dab317c1b435a47ecd7da1d28f" +checksum = "32678233b67f9056b0c144b39d46dc3218637e8d84ad6038ded339e08b19620d" dependencies = [ - "cc", + "rgb", ] [[package]] @@ -73,6 +73,42 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bat" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f59cdca60e52d7c8ea28dd2b627ab0478e4d4a2df7cc30a13322aadb21035217" +dependencies = [ + "ansi_colours", + "ansi_term", + "atty", + "bincode", + "bugreport", + "bytesize", + "clap 2.34.0", + "clircle", + "console", + "content_inspector", + "dirs-next", + "encoding", + "flate2", + "git2", + "globset", + "grep-cli", + "once_cell", + "path_abs", + "regex", + "semver", + "serde", + "serde_yaml", + "shell-words", + "syntect", + "thiserror", + "unicode-width", + "walkdir", + "wild", +] + [[package]] name = "bincode" version = "1.3.1" @@ -106,18 +142,41 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "bugreport" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0014b4b2b4f63bfe69c3838470121290cc437fdc79785d408a761a21e8b2404c" +dependencies = [ + "git-version", + "shell-escape", + "sys-info", +] + [[package]] name = "bytelines" version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "791e4e40d13e1463dee537b254225c12c46ec7328f1817c6264873bc166f615f" +[[package]] +name = "bytemuck" +version = "1.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f" + [[package]] name = "byteorder" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +[[package]] +name = "bytesize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" + [[package]] name = "cc" version = "1.0.66" @@ -161,6 +220,22 @@ dependencies = [ "chrono", ] +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim 0.8.0", + "term_size", + "textwrap 0.11.0", + "unicode-width", + "vec_map", +] + [[package]] name = "clap" version = "3.0.7" @@ -173,9 +248,9 @@ dependencies = [ "indexmap", "lazy_static", "os_str_bytes", - "strsim", + "strsim 0.10.0", "termcolor", - "textwrap", + "textwrap 0.14.2", ] [[package]] @@ -191,6 +266,18 @@ dependencies = [ "syn", ] +[[package]] +name = "clircle" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e68bbd985a63de680ab4d1ad77b6306611a8f961b282c8b5ab513e6de934e396" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "serde", + "winapi", +] + [[package]] name = "console" version = "0.15.0" @@ -206,6 +293,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "content_inspector" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38" +dependencies = [ + "memchr", +] + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -284,6 +380,70 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "encoding" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +dependencies = [ + "encoding-index-japanese", + "encoding-index-korean", + "encoding-index-simpchinese", + "encoding-index-singlebyte", + "encoding-index-tradchinese", +] + +[[package]] +name = "encoding-index-japanese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-korean" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-simpchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-singlebyte" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding-index-tradchinese" +version = "1.20141219.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" +dependencies = [ + "encoding_index_tests", +] + +[[package]] +name = "encoding_index_tests" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" + [[package]] name = "error-chain" version = "0.12.4" @@ -359,12 +519,13 @@ dependencies = [ "ansi_colours", "ansi_term", "atty", + "bat", "bitflags", "box_drawing", "bytelines", "chrono", "chrono-humanize", - "clap", + "clap 3.0.7", "console", "ctrlc", "dirs-next", @@ -388,6 +549,28 @@ dependencies = [ "xdg", ] +[[package]] +name = "git-version" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b0decc02f4636b9ccad390dcbe77b722a77efedfa393caf8379a51d5c61899" +dependencies = [ + "git-version-macro", + "proc-macro-hack", +] + +[[package]] +name = "git-version-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe69f1cbdb6e28af2bac214e943b99ce8a0a06b447d15d3e61161b0423139f3f" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "git2" version = "0.13.23" @@ -401,6 +584,12 @@ dependencies = [ "url", ] +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + [[package]] name = "globset" version = "0.4.8" @@ -640,9 +829,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" [[package]] name = "onig" @@ -699,6 +888,15 @@ dependencies = [ "syn", ] +[[package]] +name = "path_abs" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05ef02f6342ac01d8a93b65f96db53fe68a92a15f41144f97fb00a9e669633c3" +dependencies = [ + "std_prelude", +] + [[package]] name = "pathdiff" version = "0.2.1" @@ -927,6 +1125,15 @@ version = "0.6.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" +[[package]] +name = "rgb" +version = "0.8.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a374af9a0e5fdcdd98c1c7b64f05004f9ea2555b6c75f211daa81268a3c50f1" +dependencies = [ + "bytemuck", +] + [[package]] name = "ryu" version = "1.0.5" @@ -948,6 +1155,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "semver" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a3381e03edd24287172047536f20cabde766e2cd3e65e6b00fb3af51c4f38d" + [[package]] name = "serde" version = "1.0.118" @@ -979,6 +1192,24 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + +[[package]] +name = "shell-escape" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" + [[package]] name = "shell-words" version = "1.0.0" @@ -1000,6 +1231,18 @@ dependencies = [ "serde", ] +[[package]] +name = "std_prelude" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8207e78455ffdf55661170876f88daf85356e4edd54e0a3dbc79586ca1e50cbe" + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strsim" version = "0.10.0" @@ -1039,6 +1282,16 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "sys-info" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "sysinfo" version = "0.23.0" @@ -1053,6 +1306,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "term_size" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -1072,12 +1335,42 @@ dependencies = [ "winapi", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "term_size", + "unicode-width", +] + [[package]] name = "textwrap" version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.1.44" @@ -1173,6 +1466,12 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.2" @@ -1223,6 +1522,15 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "wild" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "035793abb854745033f01a07647a79831eba29ec0be377205f2a25b0aa830020" +dependencies = [ + "glob", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 9201f41ee..9d89bedf2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ name = "delta" path = "src/main.rs" [dependencies] +bat = "0.20.0" chrono = "0.4.19" chrono-humanize = "0.2.1" ansi_colours = "1.0.4" diff --git a/etc/assets/syntaxes.bin b/etc/assets/syntaxes.bin deleted file mode 100644 index 9bd09ea98..000000000 Binary files a/etc/assets/syntaxes.bin and /dev/null differ diff --git a/etc/assets/themes.bin b/etc/assets/themes.bin deleted file mode 100644 index 9c31eb7d6..000000000 Binary files a/etc/assets/themes.bin and /dev/null differ diff --git a/src/cli.rs b/src/cli.rs index f9ea33000..15efd15ee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet}; use std::ffi::OsString; use std::path::PathBuf; +use bat::assets::HighlightingAssets; use clap::{AppSettings, ColorChoice, FromArgMatches, IntoApp, Parser}; use lazy_static::lazy_static; use syntect::highlighting::Theme as SyntaxTheme; @@ -10,7 +11,7 @@ use syntect::parsing::SyntaxSet; use crate::config::delta_unreachable; use crate::git_config::{GitConfig, GitConfigEntry}; use crate::options; -use crate::utils::bat::assets::HighlightingAssets; +use crate::utils; use crate::utils::bat::output::PagingMode; #[derive(Parser)] @@ -1130,7 +1131,7 @@ impl Opt { I: IntoIterator, I::Item: Into + Clone, { - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); Self::from_clap_and_git_config(Self::into_app().get_matches_from(iter), git_config, assets) } diff --git a/src/main.rs b/src/main.rs index 1de9375b4..980502fb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ use std::process; use bytelines::ByteLinesReader; use crate::delta::delta; -use crate::utils::bat::assets::{list_languages, HighlightingAssets}; +use crate::utils::bat::assets::list_languages; use crate::utils::bat::output::OutputType; pub fn fatal(errmsg: T) -> ! @@ -85,7 +85,7 @@ fn main() -> std::io::Result<()> { // report that two files differ when delta is called with two positional // arguments and without standard input; 2 is used to report a real problem. fn run_app() -> std::io::Result { - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets); let subcommand_result = if opt.list_languages { diff --git a/src/options/set.rs b/src/options/set.rs index 95b83dcac..57d95b27d 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -3,6 +3,7 @@ use std::convert::TryInto; use std::result::Result; use std::str::FromStr; +use bat::assets::HighlightingAssets; use console::Term; use crate::cli; @@ -14,7 +15,6 @@ use crate::features; use crate::git_config::{GitConfig, GitConfigEntry}; use crate::options::option_value::{OptionValue, ProvenancedOptionValue}; use crate::options::theme; -use crate::utils::bat::assets::HighlightingAssets; use crate::utils::bat::output::PagingMode; macro_rules! set_options { diff --git a/src/options/theme.rs b/src/options/theme.rs index 55260eb60..642699c21 100644 --- a/src/options/theme.rs +++ b/src/options/theme.rs @@ -6,11 +6,11 @@ /// by the user, it is determined by the classification of the syntax theme into light-background /// vs dark-background syntax themes. If the user didn't choose a syntax theme, a dark-background /// default is selected. -use syntect::highlighting::ThemeSet; +use bat; +use bat::assets::HighlightingAssets; use crate::cli; use crate::env; -use crate::utils::bat::assets::HighlightingAssets; #[allow(non_snake_case)] pub fn set__is_light_mode__syntax_theme__syntax_set( @@ -22,16 +22,15 @@ pub fn set__is_light_mode__syntax_theme__syntax_set( opt.syntax_theme.as_ref(), syntax_theme_name_from_bat_theme.as_ref(), opt.light, - &assets.theme_set, ); opt.computed.is_light_mode = is_light_mode; opt.computed.syntax_theme = if is_no_syntax_highlighting_syntax_theme_name(&syntax_theme_name) { None } else { - Some(assets.theme_set.themes[&syntax_theme_name].clone()) + Some(assets.get_theme(&syntax_theme_name).clone()) }; - opt.computed.syntax_set = assets.syntax_set; + opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone(); } pub fn is_light_syntax_theme(theme: &str) -> bool { @@ -86,34 +85,16 @@ fn get_is_light_mode_and_syntax_theme_name( theme_arg: Option<&String>, bat_theme_env_var: Option<&String>, light_mode_arg: bool, - theme_set: &ThemeSet, ) -> (bool, String) { - let theme_arg = valid_syntax_theme_name_or_none(theme_arg, theme_set); - let bat_theme_env_var = valid_syntax_theme_name_or_none(bat_theme_env_var, theme_set); match (theme_arg, bat_theme_env_var, light_mode_arg) { (None, None, false) => (false, DEFAULT_DARK_SYNTAX_THEME.to_string()), - (Some(theme_name), _, false) => (is_light_syntax_theme(&theme_name), theme_name), - (None, Some(theme_name), false) => (is_light_syntax_theme(&theme_name), theme_name), - (None, None, true) => (true, DEFAULT_LIGHT_SYNTAX_THEME.to_string()), - (Some(theme_name), _, is_light_mode) => (is_light_mode, theme_name), - (None, Some(theme_name), is_light_mode) => (is_light_mode, theme_name), - } -} - -// At this stage the theme name is considered valid if it is either a real theme name or the special -// no-syntax-highlighting name. -fn valid_syntax_theme_name_or_none( - theme_name: Option<&String>, - theme_set: &ThemeSet, -) -> Option { - match theme_name { - Some(name) - if is_no_syntax_highlighting_syntax_theme_name(name) - || theme_set.themes.contains_key(name) => - { - Some(name.to_string()) + (Some(theme_name), _, false) => (is_light_syntax_theme(theme_name), theme_name.to_string()), + (None, Some(theme_name), false) => { + (is_light_syntax_theme(theme_name), theme_name.to_string()) } - _ => None, + (None, None, true) => (true, DEFAULT_LIGHT_SYNTAX_THEME.to_string()), + (Some(theme_name), _, is_light_mode) => (is_light_mode, theme_name.to_string()), + (None, Some(theme_name), is_light_mode) => (is_light_mode, theme_name.to_string()), } } diff --git a/src/subcommands/list_syntax_themes.rs b/src/subcommands/list_syntax_themes.rs index 789c619b8..34a7b0b49 100644 --- a/src/subcommands/list_syntax_themes.rs +++ b/src/subcommands/list_syntax_themes.rs @@ -2,8 +2,7 @@ use std::io::{self, Write}; use itertools::Itertools; -use crate::options::theme::is_light_syntax_theme; -use crate::utils::bat::assets::HighlightingAssets; +use crate::{options::theme::is_light_syntax_theme, utils}; #[cfg(not(tarpaulin_include))] pub fn list_syntax_themes() -> std::io::Result<()> { @@ -17,15 +16,14 @@ pub fn list_syntax_themes() -> std::io::Result<()> { } pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result<()> { - let assets = HighlightingAssets::new(); - let themes = &assets.theme_set.themes; + let assets = utils::bat::assets::load_highlighting_assets(); writeln!(writer, "Light syntax themes:")?; - for (theme, _) in themes.iter().filter(|(t, _)| is_light_syntax_theme(*t)) { + for theme in assets.themes().filter(|t| is_light_syntax_theme(*t)) { writeln!(writer, " {}", theme)?; } writeln!(writer, "\nDark syntax themes:")?; - for (theme, _) in themes.iter().filter(|(t, _)| !is_light_syntax_theme(*t)) { + for theme in assets.themes().filter(|t| !is_light_syntax_theme(*t)) { writeln!(writer, " {}", theme)?; } writeln!( @@ -36,12 +34,8 @@ pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result } pub fn _list_syntax_themes_for_machines(writer: &mut dyn Write) -> std::io::Result<()> { - let assets = HighlightingAssets::new(); - let themes = &assets.theme_set.themes; - for (theme, _) in themes - .iter() - .sorted_by_key(|(t, _)| is_light_syntax_theme(*t)) - { + let assets = utils::bat::assets::load_highlighting_assets(); + for theme in assets.themes().sorted_by_key(|t| is_light_syntax_theme(*t)) { writeln!( writer, "{}\t{}", diff --git a/src/subcommands/show_colors.rs b/src/subcommands/show_colors.rs index 1c939700f..09a8f19b1 100644 --- a/src/subcommands/show_colors.rs +++ b/src/subcommands/show_colors.rs @@ -7,16 +7,15 @@ use crate::git_config; use crate::paint; use crate::paint::BgShouldFill; use crate::style; -use crate::utils::bat::assets::HighlightingAssets; use crate::utils::bat::output::{OutputType, PagingMode}; #[cfg(not(tarpaulin_include))] pub fn show_colors() -> std::io::Result<()> { use itertools::Itertools; - use crate::delta::DiffType; + use crate::{delta::DiffType, utils}; - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets); let config = config::Config::from(opt); diff --git a/src/subcommands/show_syntax_themes.rs b/src/subcommands/show_syntax_themes.rs index ff018d3fe..be7478a49 100644 --- a/src/subcommands/show_syntax_themes.rs +++ b/src/subcommands/show_syntax_themes.rs @@ -2,14 +2,14 @@ use crate::cli; use crate::config; use crate::delta; use crate::options::theme::is_light_syntax_theme; -use crate::utils::bat::assets::HighlightingAssets; +use crate::utils; use crate::utils::bat::output::{OutputType, PagingMode}; use clap::Parser; use std::io::{self, ErrorKind, Read, Write}; #[cfg(not(tarpaulin_include))] pub fn show_syntax_themes() -> std::io::Result<()> { - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); let mut output_type = OutputType::from_mode( PagingMode::QuitIfOneScreen, None, @@ -32,7 +32,7 @@ pub fn show_syntax_themes() -> std::io::Result<()> { let make_opt = || { let mut opt = cli::Opt::parse(); - opt.computed.syntax_set = assets.syntax_set.clone(); + opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone(); opt }; let opt = make_opt(); @@ -80,21 +80,18 @@ index f38589a..0f1bb83 100644 opt.computed.is_light_mode = is_light_mode; let mut config = config::Config::from(opt); let title_style = ansi_term::Style::new().bold(); - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); for syntax_theme in assets - .theme_set - .themes - .iter() - .filter(|(t, _)| is_light_syntax_theme(t) == is_light_mode) - .map(|(t, _)| t) + .themes() + .filter(|t| is_light_syntax_theme(t) == is_light_mode) { writeln!( writer, "\n\nSyntax theme: {}\n", title_style.paint(syntax_theme) )?; - config.syntax_theme = Some(assets.theme_set.themes[syntax_theme.as_str()].clone()); + config.syntax_theme = Some(assets.get_theme(syntax_theme).clone()); if let Err(error) = delta::delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) { diff --git a/src/utils/bat/assets.rs b/src/utils/bat/assets.rs index cfe0f5bd8..0eb3049b4 100644 --- a/src/utils/bat/assets.rs +++ b/src/utils/bat/assets.rs @@ -1,87 +1,24 @@ // Based on code from https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468 // See src/utils/bat/LICENSE -use std::fs::File; -use std::io::{self, BufReader, Write}; -use std::path::PathBuf; +use std::io::{self, Write}; use ansi_term::Colour::Green; use ansi_term::Style; -use syntect::dumps::{from_binary, from_reader}; -use syntect::highlighting::ThemeSet; -use syntect::parsing::SyntaxSet; +use bat; -use crate::errors::*; -use crate::utils::bat::dirs::PROJECT_DIRS; +use crate::utils; -pub struct HighlightingAssets { - pub syntax_set: SyntaxSet, - pub theme_set: ThemeSet, -} - -impl HighlightingAssets { - pub fn new() -> Self { - Self::from_cache().unwrap_or_else(|_| Self::from_binary()) - } - - fn get_integrated_syntaxset() -> SyntaxSet { - from_binary(include_bytes!("../../../etc/assets/syntaxes.bin")) - } - - fn get_integrated_themeset() -> ThemeSet { - from_binary(include_bytes!("../../../etc/assets/themes.bin")) - } - - fn from_cache() -> Result { - let theme_set_path = theme_set_path(); - let syntax_set_file = File::open(&syntax_set_path()).chain_err(|| { - format!( - "Could not load cached syntax set '{}'", - syntax_set_path().to_string_lossy() - ) - })?; - let syntax_set: SyntaxSet = from_reader(BufReader::new(syntax_set_file)) - .chain_err(|| "Could not parse cached syntax set")?; - - let theme_set_file = File::open(&theme_set_path).chain_err(|| { - format!( - "Could not load cached theme set '{}'", - theme_set_path.to_string_lossy() - ) - })?; - let theme_set: ThemeSet = from_reader(BufReader::new(theme_set_file)) - .chain_err(|| "Could not parse cached theme set")?; - - Ok(HighlightingAssets { - syntax_set, - theme_set, - }) - } - - fn from_binary() -> Self { - let syntax_set = Self::get_integrated_syntaxset(); - let theme_set = Self::get_integrated_themeset(); - - HighlightingAssets { - syntax_set, - theme_set, - } - } -} - -fn theme_set_path() -> PathBuf { - PROJECT_DIRS.cache_dir().join("themes.bin") -} - -fn syntax_set_path() -> PathBuf { - PROJECT_DIRS.cache_dir().join("syntaxes.bin") +pub fn load_highlighting_assets() -> bat::assets::HighlightingAssets { + bat::assets::HighlightingAssets::from_cache(utils::bat::dirs::PROJECT_DIRS.cache_dir()) + .unwrap_or_else(|_| bat::assets::HighlightingAssets::from_binary()) } pub fn list_languages() -> std::io::Result<()> { - let assets = HighlightingAssets::new(); + let assets = utils::bat::assets::load_highlighting_assets(); let mut languages = assets - .syntax_set - .syntaxes() + .get_syntaxes() + .unwrap() .iter() .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty()) .collect::>();