Skip to content

Commit

Permalink
calc config hash and add image cache checking
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk1f0 committed Feb 27, 2023
1 parent 4efb1ed commit 75b39a1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 27 deletions.
7 changes: 7 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.1.6", features = ["derive"] }
colored = "2.0.0"
glob = "0.3.1"
hyprland = "0.3.0"
image = "0.24.5"
md5 = "0.7.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rwpspread --help
- [x] splitting for any screen layout (two or more screens)
- [x] Hyprland Integration
- [x] wpaperd Integration
- [x] wallpaper caching (don't resplit if we don't need to)
- [ ] restore standalone support
- [ ] wallpaper caching (don't resplit if we don't need to)
- [ ] alignment adjust if wallpaper is big enough
- [ ] monitor bezel compensation
101 changes: 84 additions & 17 deletions src/splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use image::{GenericImageView, DynamicImage, imageops::FilterType};
use std::cmp;
use std::env::var;
use std::path::PathBuf;
use md5::compute;
use md5::{compute, Digest};
use colored::Colorize;
use glob::glob;
use crate::Config;
use crate::wpaperd::{WpaperdConfig, check_existing};
use crate::layout::Monitor;
Expand Down Expand Up @@ -42,20 +43,31 @@ impl Splitter {
|err| err.to_string()
)?;

// calculate the hash
self.base_hash = format!("# {:x}\n", compute(img.as_bytes()));
// calculate hash
self.base_hash = self.hash_config(
compute(img.as_bytes())
);

// check existing config
//check hash
if self.with_wpaperd {
let check_result = check_existing(
format!("{}/.config/wpaperd/output.conf",var("HOME").unwrap()),
if let true = check_existing(
&format!(
"{}/.config/wpaperd/output.conf",
var("HOME").unwrap()
),
&self.base_hash
).map_err(
|err| err.to_string()
)?;
if check_result {
// we're done
return Ok(())
).map_err( |err| err.to_string())? {
// config hashes match
println!(
"{}: Hashes match",
"INFO".green().bold()
);

// check caches
if self.check_caches() {
// we're done
return Ok(())
}
}
}

Expand Down Expand Up @@ -87,8 +99,14 @@ impl Splitter {
let mut overall_width = 0;
let mut overall_height = 0;
for monitor in &self.monitors {
overall_width = cmp::max(monitor.width + monitor.x as u32, overall_width);
overall_height = cmp::max(monitor.height + monitor.y as u32, overall_height);
overall_width = cmp::max(
monitor.width + monitor.x as u32,
overall_width
);
overall_height = cmp::max(
monitor.height + monitor.y as u32,
overall_height
);
}

// check if we need to upscale
Expand Down Expand Up @@ -121,11 +139,10 @@ impl Splitter {
ResultPaper {
monitor_name: format!("{}", &monitor.name),
image_full_path: format!(
"{}rwpspread_{}_{}x{}.png",
"{}rwps_{}_{}.png",
&self.save_path,
&self.base_hash[2..16],
format!("{}", &monitor.name),
cropped_image.width(),
cropped_image.height(),
),
image: cropped_image
}
Expand Down Expand Up @@ -159,4 +176,54 @@ impl Splitter {
// return
Ok(())
}
fn hash_config(&self, img_hash: Digest) -> String {
// new hash string
let mut hash_string = String::new();

// loop over config params and add to string
for monitor in &self.monitors {
hash_string.push_str(
&format!("{}{}{}{}{}",
monitor.name,
monitor.x,
monitor.y,
monitor.width,
monitor.height
)
);
}

// compute and assemble hash
format!(
"# {:?}{:?}\n",
img_hash,
compute(hash_string.as_bytes())
)
}
fn check_caches(&self) -> bool {
// wildacrd search for cached images
for entry in glob(
&format!(
"{}/.cache/rwps_{}*",
var("HOME").unwrap(),
&self.base_hash[2..16]
)
).unwrap() {
match entry {
Ok(_) => {
// files exist
println!(
"{}: Cache exists",
"INFO".green().bold(),
);

return true
},
Err(_) => break
}
}

// if we dont exit sooner, return false
false
}
}
11 changes: 2 additions & 9 deletions src/wpaperd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@ impl WpaperdConfig {
}
}

/*
check for existing config file
@TODO:
-> Currently only compares bae image hash
-> we have to check if cached images still exist
-> also check if layout is still the same (incorporate to hash)
*/
pub fn check_existing(path: String, base_hash: &String) -> Result<bool, String> {
// check for existing config
pub fn check_existing(path: &String, base_hash: &String) -> Result<bool, String> {
// Open the file
let read_file = std::fs::read_to_string(path).map_err(
|_| "unable to open config"
Expand Down

0 comments on commit 75b39a1

Please sign in to comment.