1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

color schemes: move other color scheme format code to color-funcs

I want to add lua funcs for importing schemes; this is a step towards
that.
This commit is contained in:
Wez Furlong 2022-07-13 21:29:51 -07:00
parent a1c8b4a9b3
commit 222853bdc5
13 changed files with 269 additions and 178 deletions

4
Cargo.lock generated
View File

@ -620,6 +620,9 @@ dependencies = [
"log",
"lru",
"luahelper",
"serde",
"serde_json",
"serde_yaml",
"wezterm-dynamic",
]
@ -4213,6 +4216,7 @@ name = "sync-color-schemes"
version = "0.1.0"
dependencies = [
"anyhow",
"color-funcs",
"config",
"futures",
"lazy_static",

View File

@ -15,4 +15,7 @@ lazy_static = "1.4"
log = "0.4"
lru = "0.7"
luahelper = { path = "../../luahelper" }
serde = {version="1.0", features=["derive"]}
serde_json = "1.0"
serde_yaml = "0.8"
wezterm-dynamic = { path = "../../wezterm-dynamic" }

View File

@ -3,6 +3,7 @@ use config::lua::{get_or_create_module, get_or_create_sub_module};
use config::{Gradient, RgbaColor, SrgbaTuple};
mod image_colors;
pub mod schemes;
#[derive(Clone)]
pub struct ColorWrap(RgbaColor);

View File

@ -0,0 +1,79 @@
use anyhow::Context;
use config::{ColorSchemeFile, ColorSchemeMetaData, Palette, RgbaColor};
use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize, Debug)]
#[allow(non_snake_case, dead_code)]
pub struct Base16Scheme {
scheme: String,
author: String,
base00: String,
base01: String,
base02: String,
base03: String,
base04: String,
base05: String,
base06: String,
base07: String,
base08: String,
base09: String,
base0A: String,
base0B: String,
base0C: String,
base0D: String,
base0E: String,
base0F: String,
}
impl Base16Scheme {
pub fn load_file<P: AsRef<Path>>(path: P) -> anyhow::Result<ColorSchemeFile>
where
P: std::fmt::Debug,
{
let data = std::fs::read_to_string(&path).context(format!("read file {path:?}"))?;
let scheme: Self = serde_yaml::from_str(&data)?;
let base_0 = RgbaColor::try_from(scheme.base00)?;
// let base_1 = RgbaColor::try_from(scheme.base01)?;
// let base_2 = RgbaColor::try_from(scheme.base02)?;
let base_3 = RgbaColor::try_from(scheme.base03)?;
// let base_4 = RgbaColor::try_from(scheme.base04)?;
let base_5 = RgbaColor::try_from(scheme.base05)?;
// let base_6 = RgbaColor::try_from(scheme.base06)?;
let base_7 = RgbaColor::try_from(scheme.base07)?;
let base_8 = RgbaColor::try_from(scheme.base08)?;
// let base_9 = RgbaColor::try_from(scheme.base09)?;
let base_a = RgbaColor::try_from(scheme.base0A)?;
let base_b = RgbaColor::try_from(scheme.base0B)?;
let base_c = RgbaColor::try_from(scheme.base0C)?;
let base_d = RgbaColor::try_from(scheme.base0D)?;
let base_e = RgbaColor::try_from(scheme.base0E)?;
// let base_f = RgbaColor::try_from(scheme.base0F)?;
Ok(ColorSchemeFile {
colors: Palette {
foreground: Some(base_5),
background: Some(base_0),
cursor_fg: Some(base_5),
cursor_bg: Some(base_5),
cursor_border: Some(base_5),
selection_bg: Some(base_5),
selection_fg: Some(base_0),
ansi: Some([
base_0, base_8, base_b, base_a, base_d, base_e, base_c, base_5,
]),
brights: Some([
base_3, base_8, base_b, base_a, base_d, base_e, base_c, base_7,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(scheme.scheme),
author: Some(scheme.author.clone()),
origin_url: None,
},
})
}
}

View File

@ -0,0 +1,80 @@
use config::{ColorSchemeFile, ColorSchemeMetaData, Palette, RgbaColor};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
pub struct GoghTheme {
name: String,
black: String,
red: String,
green: String,
yellow: String,
blue: String,
purple: String,
cyan: String,
white: String,
brightBlack: String,
brightRed: String,
brightGreen: String,
brightYellow: String,
brightBlue: String,
brightPurple: String,
brightCyan: String,
brightWhite: String,
foreground: String,
background: String,
cursorColor: String,
}
impl GoghTheme {
pub fn load_all(slice: &[u8]) -> anyhow::Result<Vec<ColorSchemeFile>> {
#[derive(Deserialize, Debug)]
struct Themes {
themes: Vec<GoghTheme>,
}
let data: Themes = serde_json::from_slice(slice)?;
let mut schemes = vec![];
for s in data.themes {
let cursor = RgbaColor::try_from(s.cursorColor)?;
let name = s.name;
schemes.push(ColorSchemeFile {
colors: Palette {
foreground: Some(RgbaColor::try_from(s.foreground)?),
background: Some(RgbaColor::try_from(s.background)?),
cursor_fg: Some(cursor),
cursor_bg: Some(cursor),
cursor_border: Some(cursor),
ansi: Some([
RgbaColor::try_from(s.black)?,
RgbaColor::try_from(s.red)?,
RgbaColor::try_from(s.green)?,
RgbaColor::try_from(s.yellow)?,
RgbaColor::try_from(s.blue)?,
RgbaColor::try_from(s.purple)?,
RgbaColor::try_from(s.cyan)?,
RgbaColor::try_from(s.white)?,
]),
brights: Some([
RgbaColor::try_from(s.brightBlack)?,
RgbaColor::try_from(s.brightRed)?,
RgbaColor::try_from(s.brightGreen)?,
RgbaColor::try_from(s.brightYellow)?,
RgbaColor::try_from(s.brightBlue)?,
RgbaColor::try_from(s.brightPurple)?,
RgbaColor::try_from(s.brightCyan)?,
RgbaColor::try_from(s.brightWhite)?,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(name.clone()),
author: None,
origin_url: None,
},
})
}
Ok(schemes)
}
}

View File

@ -0,0 +1,3 @@
pub mod base16;
pub mod gogh;
pub mod sexy;

View File

@ -0,0 +1,56 @@
use anyhow::Context;
use config::{ColorSchemeFile, ColorSchemeMetaData, Palette, RgbaColor};
use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize, Debug)]
pub struct Sexy {
name: String,
author: String,
color: [String; 16],
foreground: String,
background: String,
}
impl Sexy {
pub fn load_file<P: AsRef<Path>>(path: P) -> anyhow::Result<ColorSchemeFile>
where
P: std::fmt::Debug,
{
let data = std::fs::read(&path).context(format!("read file {path:?}"))?;
let sexy: Self = serde_json::from_slice(&data)?;
Ok(ColorSchemeFile {
colors: Palette {
foreground: Some(RgbaColor::try_from(sexy.foreground)?),
background: Some(RgbaColor::try_from(sexy.background)?),
ansi: Some([
RgbaColor::try_from(sexy.color[0].clone())?,
RgbaColor::try_from(sexy.color[1].clone())?,
RgbaColor::try_from(sexy.color[2].clone())?,
RgbaColor::try_from(sexy.color[3].clone())?,
RgbaColor::try_from(sexy.color[4].clone())?,
RgbaColor::try_from(sexy.color[5].clone())?,
RgbaColor::try_from(sexy.color[6].clone())?,
RgbaColor::try_from(sexy.color[7].clone())?,
]),
brights: Some([
RgbaColor::try_from(sexy.color[8].clone())?,
RgbaColor::try_from(sexy.color[9].clone())?,
RgbaColor::try_from(sexy.color[10].clone())?,
RgbaColor::try_from(sexy.color[11].clone())?,
RgbaColor::try_from(sexy.color[12].clone())?,
RgbaColor::try_from(sexy.color[13].clone())?,
RgbaColor::try_from(sexy.color[14].clone())?,
RgbaColor::try_from(sexy.color[15].clone())?,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(sexy.name),
author: Some(sexy.author),
origin_url: None,
},
})
}
}

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
color-funcs = { path = "../lua-api-crates/color-funcs" }
config = { path = "../config" }
futures = "0.3"
lazy_static = "1.4"

View File

@ -67,56 +67,18 @@ async fn extract_scheme_yamls(url: &str, tar_data: &[u8]) -> anyhow::Result<Vec<
if entry.path()?.extension() == Some(std::ffi::OsStr::new("yaml")) {
let dest_file = NamedTempFile::new()?;
entry.unpack(dest_file.path())?;
let data = std::fs::read_to_string(dest_file.path())?;
println!("Got {} of data for {:?}", data.len(), entry.path()?);
let scheme: Base16Scheme = serde_yaml::from_str(&data)?;
println!("{scheme:?}");
let name = format!("{} (base16)", scheme.scheme);
let mut scheme =
color_funcs::schemes::base16::Base16Scheme::load_file(dest_file.path())?;
let base_0 = RgbaColor::try_from(scheme.base00)?;
// let base_1 = RgbaColor::try_from(scheme.base01)?;
// let base_2 = RgbaColor::try_from(scheme.base02)?;
let base_3 = RgbaColor::try_from(scheme.base03)?;
// let base_4 = RgbaColor::try_from(scheme.base04)?;
let base_5 = RgbaColor::try_from(scheme.base05)?;
// let base_6 = RgbaColor::try_from(scheme.base06)?;
let base_7 = RgbaColor::try_from(scheme.base07)?;
let base_8 = RgbaColor::try_from(scheme.base08)?;
// let base_9 = RgbaColor::try_from(scheme.base09)?;
let base_a = RgbaColor::try_from(scheme.base0A)?;
let base_b = RgbaColor::try_from(scheme.base0B)?;
let base_c = RgbaColor::try_from(scheme.base0C)?;
let base_d = RgbaColor::try_from(scheme.base0D)?;
let base_e = RgbaColor::try_from(scheme.base0E)?;
// let base_f = RgbaColor::try_from(scheme.base0F)?;
let name = format!("{} (base16)", scheme.metadata.name.unwrap());
scheme.metadata.name = Some(name.clone());
scheme.metadata.origin_url = Some(url.to_string());
schemes.push(Scheme {
name: name.clone(),
name: name,
file_name: None,
data: ColorSchemeFile {
colors: Palette {
foreground: Some(base_5),
background: Some(base_0),
cursor_fg: Some(base_5),
cursor_bg: Some(base_5),
cursor_border: Some(base_5),
selection_bg: Some(base_5),
selection_fg: Some(base_0),
ansi: Some([
base_0, base_8, base_b, base_a, base_d, base_e, base_c, base_5,
]),
brights: Some([
base_3, base_8, base_b, base_a, base_d, base_e, base_c, base_7,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(name.clone()),
author: Some(scheme.author.clone()),
origin_url: Some(url.to_string()),
},
},
data: scheme,
});
}
}

View File

@ -1,91 +1,27 @@
use super::*;
use color_funcs::schemes::gogh::GoghTheme;
#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
struct GoghTheme {
name: String,
black: String,
red: String,
green: String,
yellow: String,
blue: String,
purple: String,
cyan: String,
white: String,
brightBlack: String,
brightRed: String,
brightGreen: String,
brightYellow: String,
brightBlue: String,
brightPurple: String,
brightCyan: String,
brightWhite: String,
foreground: String,
background: String,
cursorColor: String,
}
async fn fetch_gogh_json() -> anyhow::Result<Vec<GoghTheme>> {
pub async fn load_gogh() -> anyhow::Result<Vec<Scheme>> {
let latest =
fetch_url("https://raw.githubusercontent.com/Gogh-Co/Gogh/master/data/themes.json").await?;
#[derive(Deserialize, Debug)]
struct Themes {
themes: Vec<GoghTheme>,
}
Ok(GoghTheme::load_all(&latest)?
.into_iter()
.map(|mut scheme| {
let name = format!("{} (Gogh)", scheme.metadata.name.unwrap());
scheme.metadata.name = Some(name.clone());
scheme
.metadata
.origin_url
.replace("https://github.com/Gogh-Co/Gogh".to_string());
let data: Themes = serde_json::from_slice(&latest)?;
Ok(data.themes)
}
pub async fn load_gogh() -> anyhow::Result<Vec<Scheme>> {
let mut schemes = vec![];
for s in fetch_gogh_json().await? {
let cursor = RgbaColor::try_from(s.cursorColor)?;
let name = format!("{} (Gogh)", s.name);
schemes.push(Scheme {
name: name.clone(),
file_name: None,
data: ColorSchemeFile {
colors: Palette {
foreground: Some(RgbaColor::try_from(s.foreground)?),
background: Some(RgbaColor::try_from(s.background)?),
cursor_fg: Some(cursor),
cursor_bg: Some(cursor),
cursor_border: Some(cursor),
ansi: Some([
RgbaColor::try_from(s.black)?,
RgbaColor::try_from(s.red)?,
RgbaColor::try_from(s.green)?,
RgbaColor::try_from(s.yellow)?,
RgbaColor::try_from(s.blue)?,
RgbaColor::try_from(s.purple)?,
RgbaColor::try_from(s.cyan)?,
RgbaColor::try_from(s.white)?,
]),
brights: Some([
RgbaColor::try_from(s.brightBlack)?,
RgbaColor::try_from(s.brightRed)?,
RgbaColor::try_from(s.brightGreen)?,
RgbaColor::try_from(s.brightYellow)?,
RgbaColor::try_from(s.brightBlue)?,
RgbaColor::try_from(s.brightPurple)?,
RgbaColor::try_from(s.brightCyan)?,
RgbaColor::try_from(s.brightWhite)?,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(name.clone()),
author: None,
origin_url: Some("https://github.com/Gogh-Co/Gogh".to_string()),
},
},
Scheme {
name,
file_name: None,
data: scheme,
}
})
}
Ok(schemes)
.collect())
}
pub async fn sync_gogh() -> anyhow::Result<Vec<Scheme>> {

View File

@ -1,7 +1,6 @@
use crate::scheme::Scheme;
use anyhow::Context;
use config::{ColorSchemeFile, ColorSchemeMetaData, Palette, RgbaColor};
use serde::Deserialize;
use config::ColorSchemeFile;
use sqlite_cache::Cache;
use std::collections::BTreeMap;
use std::path::Path;
@ -178,10 +177,13 @@ fn accumulate(schemeses: &mut Vec<Scheme>, to_add: Vec<Scheme>) {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// They color us! my precious!
let mut schemeses = iterm2::sync_iterm2()?;
accumulate(&mut schemeses, base16::sync().await?);
accumulate(&mut schemeses, gogh::sync_gogh().await?);
accumulate(&mut schemeses, sexy::sync_sexy()?);
let mut schemeses = iterm2::sync_iterm2().context("sync iterm2")?;
accumulate(&mut schemeses, base16::sync().await.context("sync base16")?);
accumulate(
&mut schemeses,
gogh::sync_gogh().await.context("sync gogh")?,
);
accumulate(&mut schemeses, sexy::sync_sexy().context("sync sexy")?);
bake_for_config(schemeses)?;
Ok(())

View File

@ -48,7 +48,8 @@ impl Scheme {
pub fn load_schemes<P: AsRef<Path>>(scheme_dir: P) -> anyhow::Result<Vec<Scheme>> {
let scheme_dir_path = scheme_dir.as_ref();
let dir = std::fs::read_dir(scheme_dir_path)?;
let dir = std::fs::read_dir(scheme_dir_path)
.with_context(|| format!("load_schemes from dir {scheme_dir_path:?}"))?;
let mut schemes = vec![];

View File

@ -4,55 +4,18 @@ fn load_sexy_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Scheme>
where
P: std::fmt::Debug,
{
#[derive(Deserialize, Debug)]
struct Sexy {
name: String,
author: String,
color: [String; 16],
foreground: String,
background: String,
}
let data = std::fs::read(&path).context(format!("read file {path:?}"))?;
let sexy: Sexy = serde_json::from_slice(&data)?;
let name = format!("{} (terminal.sexy)", sexy.name);
let mut scheme = color_funcs::schemes::sexy::Sexy::load_file(&path)?;
let name = format!("{} (terminal.sexy)", scheme.metadata.name.unwrap());
scheme.metadata.name = Some(name.clone());
scheme
.metadata
.origin_url
.replace("https://github.com/stayradiated/terminal.sexy".to_string());
Ok(Scheme {
name: name.clone(),
file_name: None,
data: ColorSchemeFile {
colors: Palette {
foreground: Some(RgbaColor::try_from(sexy.foreground)?),
background: Some(RgbaColor::try_from(sexy.background)?),
ansi: Some([
RgbaColor::try_from(sexy.color[0].clone())?,
RgbaColor::try_from(sexy.color[1].clone())?,
RgbaColor::try_from(sexy.color[2].clone())?,
RgbaColor::try_from(sexy.color[3].clone())?,
RgbaColor::try_from(sexy.color[4].clone())?,
RgbaColor::try_from(sexy.color[5].clone())?,
RgbaColor::try_from(sexy.color[6].clone())?,
RgbaColor::try_from(sexy.color[7].clone())?,
]),
brights: Some([
RgbaColor::try_from(sexy.color[8].clone())?,
RgbaColor::try_from(sexy.color[9].clone())?,
RgbaColor::try_from(sexy.color[10].clone())?,
RgbaColor::try_from(sexy.color[11].clone())?,
RgbaColor::try_from(sexy.color[12].clone())?,
RgbaColor::try_from(sexy.color[13].clone())?,
RgbaColor::try_from(sexy.color[14].clone())?,
RgbaColor::try_from(sexy.color[15].clone())?,
]),
..Default::default()
},
metadata: ColorSchemeMetaData {
name: Some(name.clone()),
author: Some(sexy.author.clone()),
origin_url: Some("https://github.com/stayradiated/terminal.sexy".to_string()),
},
},
data: scheme,
})
}