Skip to content

Commit

Permalink
color schemes: adjust sync aliasing
Browse files Browse the repository at this point in the history
* Change aliasing to exact matches of the color data.
* Don't "GC" away schemes that are no longer reachable from aggregators;
  just because they are no longer listed elsewhere, doesn't mean that we
  should remove the name/scheme from wezterm if it was already made
  available in an earlier release.

refs: #3831
  • Loading branch information
wez committed Jul 10, 2023
1 parent 690878e commit 633e6e4
Show file tree
Hide file tree
Showing 7 changed files with 6,297 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ordered-float = { version = "3.0", features = ["serde"] }
portable-pty = { path = "../pty", features = ["serde_support"]}
promise = { path = "../promise" }
serde = {version="1.0", features = ["rc", "derive"]}
serde_json = "1.0"
shlex = "1.1"
smol = "1.2"
termwiz = { path = "../termwiz", features=["use_serde"] }
Expand Down
5 changes: 5 additions & 0 deletions config/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,11 @@ impl ColorSchemeFile {
Ok(dynamic_to_toml(value)?)
}

pub fn from_json_value(value: &serde_json::Value) -> anyhow::Result<Self> {
Self::from_dynamic(&crate::json_to_dynamic(value), Default::default())
.map_err(|e| anyhow::anyhow!("{}", e))
}

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> anyhow::Result<()> {
let value = self.to_toml_value()?;
let text = toml::to_string_pretty(&value)?;
Expand Down
41 changes: 41 additions & 0 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ fn toml_table_has_numeric_keys(t: &toml::value::Table) -> bool {
t.keys().all(|k| k.parse::<isize>().is_ok())
}

fn json_object_has_numeric_keys(t: &serde_json::Map<String, serde_json::Value>) -> bool {
t.keys().all(|k| k.parse::<isize>().is_ok())
}

fn toml_to_dynamic(value: &toml::Value) -> Value {
match value {
toml::Value::String(s) => s.to_dynamic(),
Expand Down Expand Up @@ -111,6 +115,43 @@ fn toml_to_dynamic(value: &toml::Value) -> Value {
}
}

fn json_to_dynamic(value: &serde_json::Value) -> Value {
match value {
serde_json::Value::Null => Value::Null,
serde_json::Value::Bool(b) => b.to_dynamic(),
serde_json::Value::Number(n) => {
if let Some(i) = n.as_i64() {
i.to_dynamic()
} else if let Some(i) = n.as_u64() {
i.to_dynamic()
} else if let Some(f) = n.as_f64() {
f.to_dynamic()
} else {
Value::Null
}
}
serde_json::Value::String(s) => s.to_dynamic(),
serde_json::Value::Array(a) => a
.iter()
.map(json_to_dynamic)
.collect::<Vec<_>>()
.to_dynamic(),
// Allow `colors.indexed` to be passed through with actual integer keys
serde_json::Value::Object(t) if json_object_has_numeric_keys(t) => Value::Object(
t.iter()
.map(|(k, v)| (k.parse::<isize>().unwrap().to_dynamic(), json_to_dynamic(v)))
.collect::<BTreeMap<_, _>>()
.into(),
),
serde_json::Value::Object(t) => Value::Object(
t.iter()
.map(|(k, v)| (Value::String(k.to_string()), json_to_dynamic(v)))
.collect::<BTreeMap<_, _>>()
.into(),
),
}
}

pub fn build_default_schemes() -> HashMap<String, Palette> {
let mut color_schemes = HashMap::new();
for (scheme_name, data) in scheme_data::SCHEMES.iter() {
Expand Down
187 changes: 173 additions & 14 deletions config/src/scheme_data.rs

Large diffs are not rendered by default.

Loading

0 comments on commit 633e6e4

Please sign in to comment.