1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

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
This commit is contained in:
Wez Furlong 2020-09-10 21:18:32 -07:00
parent 3090c9bb68
commit 94fb5094a4
6 changed files with 62 additions and 52 deletions

View File

@ -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()

View File

@ -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/*
}

View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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<PathBuf> {
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(())
}