Skip to content

Commit

Permalink
termwiz: remove semver dep
Browse files Browse the repository at this point in the history
The newer version is much more strict about its inputs
and we're dealing with all sorts of weirdness as input
from the environment, so let's just roll our own simple
solution.
  • Loading branch information
wez committed May 13, 2024
1 parent 1f33b35 commit eb6fc75
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion termwiz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ ordered-float = "4.1"
pest = "2.1"
pest_derive = "2.1"
phf = "0.11"
semver = "1.0"
serde = {version="1.0", features = ["rc", "derive"], optional=true}
siphasher = "0.3"
sha2 = "0.10"
Expand Down
58 changes: 54 additions & 4 deletions termwiz/src/caps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
//! the terminal capabilities, but also offers a `ProbeHints`
//! that can be used by the embedding application to override those choices.
use crate::{builder, Result};
use semver::Version;
use std::env::var;
use terminfo::{self, capability as cap};

Expand Down Expand Up @@ -295,14 +294,13 @@ impl Capabilities {
// here because the iTerm2 docs don't say when the
// image protocol was first implemented, but do mention
// the gif version.
Version::parse(
version_ge(
hints
.term_program_version
.as_ref()
.unwrap_or(&"0.0.0".to_owned()),
"2.9.20150512",
)
.unwrap_or(Version::new(0, 0, 0))
>= Version::new(2, 9, 20150512)
}
Some("WezTerm") => true,
_ => false,
Expand Down Expand Up @@ -378,10 +376,62 @@ impl Capabilities {
}
}

/// Returns true if the version string `a` is >= `b`
fn version_ge(a: &str, b: &str) -> bool {
let mut a = a.split('.');
let mut b = b.split('.');

loop {
match (a.next(), b.next()) {
(Some(a), Some(b)) => match (a.parse::<u64>(), b.parse::<u64>()) {
(Ok(a), Ok(b)) => {
if a > b {
return true;
}
if a < b {
return false;
}
}
_ => {
if a > b {
return true;
}
if a < b {
return false;
}
}
},
(Some(_), None) => {
// A is greater
return true;
}
(None, Some(_)) => {
// A is smaller
return false;
}
(None, None) => {
// Equal
return true;
}
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn version_cmp() {
assert!(version_ge("1", "0"));
assert!(version_ge("1.0", "0"));
assert!(!version_ge("0", "1"));
assert!(version_ge("3.2", "2.9"));
assert!(version_ge("3.2.0beta5", "2.9"));
assert!(version_ge("3.2.0beta5", "3.2.0"));
assert!(version_ge("3.2.0beta5", "3.2.0beta1"));
}

fn load_terminfo() -> terminfo::Database {
// Load our own compiled data so that the tests have an
// environment that doesn't vary machine by machine.
Expand Down

0 comments on commit eb6fc75

Please sign in to comment.