Skip to content

Commit

Permalink
hardcode some things and add missing erro handling
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk1f0 committed May 16, 2023
1 parent 55b5395 commit ccc4df6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
20 changes: 8 additions & 12 deletions src/splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::Path;

pub struct ResultPaper {
pub monitor_name: String,
pub image_full_path: String,
pub full_path: String,
pub image: DynamicImage,
}

Expand Down Expand Up @@ -44,10 +44,7 @@ impl Splitter {
// check if we need to generate wpaperd config
if config.with_wpaperd {
// create new wpaperd instance
let wpaperd = WpaperdConfig::new(
format!("{}/.config/wpaperd/wallpaper.toml", var("HOME").unwrap()),
self.hash.clone(),
);
let wpaperd = WpaperdConfig::new(self.hash.clone());

// check caches
let caches_present = self.check_caches();
Expand All @@ -59,7 +56,7 @@ impl Splitter {

// we need to resplit
self.result_papers = self
.perform_split(img, config, format!("{}/.cache/", var("HOME").unwrap()))
.perform_split(img, config)
.map_err(|err| err.to_string())?;
}

Expand All @@ -85,7 +82,7 @@ impl Splitter {
} else {
// just split
self.result_papers = self
.perform_split(img, config, format!("{}/", var("PWD").unwrap()))
.perform_split(img, config)
.map_err(|err| err.to_string())?;
}

Expand All @@ -98,7 +95,6 @@ impl Splitter {
&self,
mut img: DynamicImage,
config: &Config,
save_path: String,
) -> Result<Vec<ResultPaper>, String> {
/*
Calculate Overall Size
Expand Down Expand Up @@ -139,10 +135,10 @@ impl Splitter {

// get full image path
let path_image = format!(
"{}rwps_{}_{}.png",
save_path,
"{}/rwps_{}_{}.png",
var("PWD").unwrap(),
&self.hash[2..32],
format!("{}", &monitor.name),
&monitor.name,
);

// save it
Expand All @@ -153,7 +149,7 @@ impl Splitter {
// push to result vector
result.push(ResultPaper {
monitor_name: format!("{}", &monitor.name),
image_full_path: path_image,
full_path: path_image,
image: cropped_image,
})
}
Expand Down
30 changes: 17 additions & 13 deletions src/wpaperd.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
use crate::splitter::ResultPaper;
use std::env::var;
use std::fs::File;
use std::io::Write;
use std::process::{Command, Stdio};
use toml::{to_string_pretty, Value};

pub struct WpaperdConfig {
pub config_path: String,
pub config_hash: String,
config_path: String,
}

impl WpaperdConfig {
pub fn new(config_path: String, config_hash: String) -> Self {
pub fn new(config_hash: String) -> Self {
Self {
config_path,
config_hash,
config_path: format!("{}/.config/wpaperd/wallpaper.toml", var("HOME").unwrap()),
}
}

// build new wpaperd config to file
pub fn build(&self, wallpapers: &Vec<ResultPaper>) -> Result<(), String> {
// Create a new config file
let mut file = File::create(&self.config_path).map_err(|_| "unable to open config")?;
let mut config_file =
File::create(&self.config_path).map_err(|_| "unable to open config")?;

// Open the file
let read_file =
Expand All @@ -32,25 +34,27 @@ impl WpaperdConfig {
.map_err(|_| "unable to parse config")?;

// Add new output sections
for monitor in wallpapers {
for fragment in wallpapers {
// insert new section
values.as_table_mut().unwrap().insert(
monitor.monitor_name.to_string(),
fragment.monitor_name.to_string(),
Value::Table(Default::default()),
);
// add path value
let path = values.get_mut(monitor.monitor_name.to_string()).unwrap();
let path = values.get_mut(fragment.monitor_name.to_string()).unwrap();
path.as_table_mut().unwrap().insert(
"path".to_string(),
Value::String(monitor.image_full_path.to_string()),
Value::String(fragment.full_path.to_string()),
);
}

// write the file
file.write(self.config_hash.as_bytes()).unwrap();
file.write(b"# AUTOGENERATED CONFIG BY RWPSPREAD\n\n")
config_file.write(self.config_hash.as_bytes()).unwrap();
config_file
.write(b"# AUTOGENERATED CONFIG BY RWPSPREAD\n\n")
.unwrap();
file.write_all(to_string_pretty(&values).unwrap().as_bytes())
config_file
.write_all(to_string_pretty(&values).unwrap().as_bytes())
.unwrap();

// return
Expand Down Expand Up @@ -93,7 +97,7 @@ impl CmdWrapper {
.map_err(|err| err.to_string())?;
}
}
Err(_) => {}
Err(e) => return Err(e.to_string()),
}

// Spawn new wpaperd instance
Expand All @@ -120,7 +124,7 @@ impl CmdWrapper {
.map_err(|err| err.to_string())?;
}
}
Err(_) => {}
Err(e) => return Err(e.to_string()),
}

Ok(())
Expand Down

0 comments on commit ccc4df6

Please sign in to comment.