From 94fb5094a4d99291e0ddd7c86703e6cc64449329 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 10 Sep 2020 21:18:32 -0700 Subject: [PATCH] wezterm: compile in default color schemes Rather than scanning directories and reading in ~230 files on startup, do the scan at build time so that we're parsing from memory rather than local storage. This should shave a bit of time off the startup, although I haven't measured this, and I've only run this on a remote linux system thus far. refs: https://github.com/wez/wezterm/issues/264 --- build.rs | 48 ++++++++++++++++++++++++++++++++++++- ci/PKGBUILD.template | 2 -- ci/appimage.sh | 1 - ci/deploy.sh | 10 ++------ ci/windows-installer.iss | 1 - src/config/mod.rs | 52 ++++++++++------------------------------ 6 files changed, 62 insertions(+), 52 deletions(-) diff --git a/build.rs b/build.rs index 8d60bb232..40d02a353 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,55 @@ +use std::path::Path; use vergen::{generate_cargo_keys, ConstantsFlags}; +fn bake_color_schemes() { + let dir = std::fs::read_dir("assets/colors").unwrap(); + + let mut schemes = vec![]; + + for entry in dir { + let entry = entry.unwrap(); + let name = entry.file_name(); + let name = name.to_str().unwrap(); + + if name.ends_with(".toml") { + let len = name.len(); + let scheme_name = &name[..len - 5]; + let data = String::from_utf8(std::fs::read(entry.path()).unwrap()).unwrap(); + schemes.push((scheme_name.to_string(), data)); + + println!("cargo:rerun-if-changed={}", entry.path().display()); + } + } + + let mut code = String::new(); + code.push_str(&format!( + "pub const SCHEMES: [(&'static str, &'static str); {}] = [", + schemes.len() + )); + for (name, data) in schemes { + code.push_str(&format!( + "(\"{}\", \"{}\"),\n", + name.escape_default(), + data.escape_default(), + )); + } + code.push_str("];\n"); + + std::fs::write( + Path::new(&std::env::var_os("OUT_DIR").unwrap()).join("scheme_data.rs"), + code, + ) + .unwrap(); +} + fn main() { + println!("cargo:rerun-if-changed=build.rs"); + let mut flags = ConstantsFlags::all(); flags.remove(ConstantsFlags::SEMVER_FROM_CARGO_PKG); + + bake_color_schemes(); + // Generate the 'cargo:' key output generate_cargo_keys(ConstantsFlags::all()).expect("Unable to generate the cargo keys!"); @@ -21,7 +68,6 @@ fn main() { #[cfg(windows)] { use std::io::Write; - use std::path::Path; let profile = std::env::var("PROFILE").unwrap(); let exe_output_dir = Path::new("target").join(profile); let windows_dir = std::env::current_dir() diff --git a/ci/PKGBUILD.template b/ci/PKGBUILD.template index 2c10feb1f..f6dd9a13f 100644 --- a/ci/PKGBUILD.template +++ b/ci/PKGBUILD.template @@ -53,6 +53,4 @@ package() { "${pkgdir}/usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png" install -Dm644 squashfs-root/usr/share/applications/org.wezfurlong.wezterm.desktop \ "${pkgdir}/usr/share/applications/org.wezfurlong.wezterm.desktop" - install -dm755 ${pkgdir}/usr/share/wezterm/colors - install -Dm644 -t ${pkgdir}/usr/share/wezterm/colors/ squashfs-root/usr/share/wezterm/colors/* } diff --git a/ci/appimage.sh b/ci/appimage.sh index 45e60031a..987d9038b 100755 --- a/ci/appimage.sh +++ b/ci/appimage.sh @@ -6,7 +6,6 @@ mkdir AppDir install -Dsm755 -t AppDir/usr/bin target/release/wezterm install -Dsm755 -t AppDir/usr/bin target/release/strip-ansi-escapes install -Dm644 assets/icon/terminal.png AppDir/usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png -install -Dm644 -t AppDir/usr/share/wezterm/colors assets/colors/* install -Dm644 assets/wezterm.desktop AppDir/usr/share/applications/org.wezfurlong.wezterm.desktop install -Dm644 assets/wezterm.appdata.xml AppDir/usr/share/metainfo/org.wezfurlong.wezterm.appdata.xml diff --git a/ci/deploy.sh b/ci/deploy.sh index 353690594..1c3e82aad 100755 --- a/ci/deploy.sh +++ b/ci/deploy.sh @@ -28,7 +28,6 @@ case $OSTYPE in cp -r assets/macos/WezTerm.app $zipdir/ cp $TARGET_DIR/release/wezterm $zipdir/WezTerm.app cp $TARGET_DIR/release/strip-ansi-escapes $zipdir/WezTerm.app - cp -r assets/colors $zipdir/WezTerm.app/Contents/Resources/ zip -r $zipname $zipdir SHA256=$(shasum -a 256 $zipname | cut -d' ' -f1) @@ -53,7 +52,6 @@ case $OSTYPE in assets/windows/conhost/conpty.dll \ assets/windows/conhost/OpenConsole.exe \ $zipdir - cp -r assets/colors $zipdir/ 7z a -tzip $zipname $zipdir iscc.exe -DMyAppVersion=${TAG_NAME#nightly} -F${instname} ci/windows-installer.iss ;; @@ -84,11 +82,9 @@ echo "Doing the build bit here" %install set -x cd ${HERE} -mkdir -p %{buildroot}/usr/bin %{buildroot}/usr/share/wezterm/colors %{buildroot}/usr/share/applications -install -Dsm755 target/release/wezterm %{buildroot}/usr/bin -install -Dsm755 target/release/strip-ansi-escapes %{buildroot}/usr/bin +install -Dsm755 target/release/wezterm -t %{buildroot}/usr/bin +install -Dsm755 target/release/strip-ansi-escapes -t %{buildroot}/usr/bin install -Dm644 assets/icon/terminal.png %{buildroot}/usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png -install -Dm644 -t %{buildroot}/usr/share/wezterm/colors assets/colors/* install -Dm644 assets/wezterm.desktop %{buildroot}/usr/share/applications/org.wezfurlong.wezterm.desktop install -Dm644 assets/wezterm.appdata.xml %{buildroot}/usr/share/metainfo/org.wezfurlong.wezterm.appdata.xml @@ -96,7 +92,6 @@ install -Dm644 assets/wezterm.appdata.xml %{buildroot}/usr/share/metainfo/org.we /usr/bin/wezterm /usr/bin/strip-ansi-escapes /usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png -/usr/share/wezterm/colors/* /usr/share/applications/org.wezfurlong.wezterm.desktop /usr/share/metainfo/org.wezfurlong.wezterm.appdata.xml EOF @@ -124,7 +119,6 @@ EOF install -Dsm755 -t pkg/debian/usr/bin target/release/wezterm install -Dsm755 -t pkg/debian/usr/bin target/release/strip-ansi-escapes install -Dm644 assets/icon/terminal.png pkg/debian/usr/share/icons/hicolor/128x128/apps/org.wezfurlong.wezterm.png - install -Dm644 -t pkg/debian/usr/share/wezterm/colors assets/colors/* install -Dm644 assets/wezterm.desktop pkg/debian/usr/share/applications/org.wezfurlong.wezterm.desktop install -Dm644 assets/wezterm.appdata.xml pkg/debian/usr/share/metainfo/org.wezfurlong.wezterm.appdata.xml if [[ "$BUILD_REASON" == "Schedule" ]] ; then diff --git a/ci/windows-installer.iss b/ci/windows-installer.iss index 6a0aa7fae..5e88d8184 100644 --- a/ci/windows-installer.iss +++ b/ci/windows-installer.iss @@ -46,7 +46,6 @@ Source: "..\target\release\wezterm.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "..\target\release\conpty.dll"; DestDir: "{app}"; Flags: ignoreversion Source: "..\target\release\OpenConsole.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "..\target\release\strip-ansi-escapes.exe"; DestDir: "{app}"; Flags: ignoreversion -Source: "..\assets\colors\*"; DestDir: "{app}\colors"; Flags: ignoreversion ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Icons] diff --git a/src/config/mod.rs b/src/config/mod.rs index 5d8071b36..9d18f5723 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -47,6 +47,8 @@ lazy_static! { static ref CONFIG: Configuration = Configuration::new(); } +include!(concat!(env!("OUT_DIR"), "/scheme_data.rs")); + fn xdg_config_home() -> PathBuf { match std::env::var_os("XDG_CONFIG_HOME").map(|s| PathBuf::from(s).join("wezterm")) { Some(p) => p, @@ -767,45 +769,7 @@ impl Config { fn compute_color_scheme_dirs(&self) -> Vec { let mut paths = self.color_scheme_dirs.clone(); paths.push(CONFIG_DIR.join("colors")); - - if let Ok(exe_name) = std::env::current_exe() { - // If running out of the source tree our executable path will be - // something like: `.../wezterm/target/release/wezterm`. - // It takes 3 parent calls to reach the wezterm dir; if we get - // there, get to the `assets/colors` dir. - if let Some(colors_dir) = exe_name - .parent() - .and_then(|release| release.parent()) - .and_then(|target| target.parent()) - .map(|srcdir| srcdir.join("assets").join("colors")) - { - paths.push(colors_dir); - } - - // If running out of an AppImage, resolve our installed colors - // path relative to our binary location: - // `/usr/bin/wezterm` -> `/usr/share/wezterm/colors` - if let Some(colors_dir) = exe_name - .parent() - .and_then(|bin| bin.parent()) - .map(|usr| usr.join("share").join("wezterm").join("colors")) - { - paths.push(colors_dir); - } - } - - if cfg!(target_os = "macos") { - if let Ok(exe_name) = std::env::current_exe() { - if let Some(colors_dir) = exe_name - .parent() - .map(|srcdir| srcdir.join("Contents").join("Resources").join("colors")) - { - paths.push(colors_dir); - } - } - } else if cfg!(unix) { - paths.push(PathBuf::from("/usr/share/wezterm/colors")); - } else if cfg!(windows) { + if cfg!(windows) { // See commentary re: portable tools above! if let Ok(exe_name) = std::env::current_exe() { if let Some(exe_dir) = exe_name.parent() { @@ -862,6 +826,16 @@ impl Config { } } + for (scheme_name, data) in SCHEMES.iter() { + let scheme_name = scheme_name.to_string(); + if self.color_schemes.contains_key(&scheme_name) { + // This scheme has already been defined + continue; + } + let scheme: ColorSchemeFile = toml::from_str(data).unwrap(); + self.color_schemes.insert(scheme_name, scheme.colors); + } + Ok(()) }