Skip to content

Commit

Permalink
refactor(whiskerify): split into functions, redo color_matches
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed Jun 2, 2024
1 parent 93dcd28 commit b3d36b7
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/whiskerify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ use fancy_regex::Regex;
pub fn convert(path: PathBuf, dry_run: bool) -> Result<()> {
let mut contents = fs::read_to_string(&path)?;

let mut possible_rgbs: Vec<(&str, [u8; 4])> = vec![];

let temp = contents.clone();
for m in Regex::new("rgba?\\(.*\\)").unwrap().captures_iter(&temp) {
let text = m.unwrap().get(0).unwrap().as_str();
let color = csscolorparser::parse(text).unwrap();

possible_rgbs.push((text, color.to_rgba8()));
}
let mut color_matches = Regex::new("rgba?\\(.*\\)")
.unwrap()
.captures_iter(&contents.clone())
.map(|m| {
let text = m.unwrap().get(0).unwrap().as_str();
let color = csscolorparser::parse(text).unwrap();
(text.to_string(), color.to_rgba8())
})
.collect::<Vec<_>>();

for flavor in catppuccin::PALETTE.all_flavors() {
contents = contents.replace(
&flavor.name.to_string(),
&format!("{} {} {}", "{{", "flavor.name", "}}"),
);
contents = contents.replace(
&flavor.identifier().to_string(),
&format!("{} {} {}", "{{", "flavor.identifier", "}}"),
);
contents = contents
.replace(&flavor.name.to_string(), &as_tera_expr("flavor.name"))
.replace(
&flavor.identifier().to_string(),
&as_tera_expr("flavor.identifier"),
);

for color in &flavor.colors {
contents = contents.replace(
&color.hex.to_string(),
&format!("#{} {}.{} {}", "{{", color.identifier(), "hex", "}}"),
&format!(
"#{}",
as_tera_expr(&(color.identifier().to_string() + ".hex"))
),
);

for (text, values) in possible_rgbs.clone() {
for (text, values) in color_matches.clone() {
if color.rgb.r == values[0] && color.rgb.g == values[1] && color.rgb.b == values[2]
{
let opacity = values[3];
Expand All @@ -49,28 +50,21 @@ pub fn convert(path: PathBuf, dry_run: bool) -> Result<()> {
};

contents = contents.replace(
text,
format!("{} {}{} {}", "{{", color.identifier(), filters, "}}").as_str(),
&text,
&as_tera_expr(&(color.identifier().to_owned() + &filters)),
);
possible_rgbs.retain(|x| *x.0 != *text);
color_matches.retain(|x| *x.0 != *text);
}
}
}
}

for (text, _) in possible_rgbs {
let (line_number, line_content) = contents
.lines()
.enumerate()
.find(|(_i, line)| line.contains(text))
.unwrap();

for (text, _) in color_matches {
warn!(
"could not replace non-Catppuccin color '{}' at {}:{}:{}",
"could not replace non-Catppuccin color '{}' at {}:{}",
text.yellow(),
path.to_string_lossy(),
line_number + 1,
line_content.find(text).unwrap() + 1
&get_location_in_text(&text, &contents)
);
}

Expand All @@ -82,3 +76,21 @@ pub fn convert(path: PathBuf, dry_run: bool) -> Result<()> {

Ok(())
}

fn as_tera_expr(value: &str) -> String {
format!("{} {} {}", "{{", value, "}}")
}

fn get_location_in_text(search: &str, text: &str) -> String {
let (line_number, line_content) = text
.lines()
.enumerate()
.find(|(_i, line)| line.contains(search))
.unwrap();

format!(
"{}:{}",
line_number + 1,
line_content.find(search).unwrap() + 1
)
}

0 comments on commit b3d36b7

Please sign in to comment.