Skip to content

Commit

Permalink
change file cache location
Browse files Browse the repository at this point in the history
  • Loading branch information
0xk1f0 committed Feb 10, 2024
1 parent 80afbdb commit 14c0b97
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ rwpspread -wdi /some/path/wallpaper.png

## `swaylock` Integration

A drop-in string for swaylock will be put in `/home/$USER/.cache/rwps_swaylock.conf` which can look something like:
A drop-in string for swaylock will be put in `/home/$USER/.cache/rwpspread/rwps_swaylock.conf` which can look something like:

```text
-i <image_path_1> -i <image_path_2>
Expand All @@ -93,19 +93,19 @@ This file can be sourced and used with your swaylock command, for exmaple:
#!/usr/bin/bash

# source the command options
IMAGES=$(/bin/cat /home/$USER/.cache/rwps_swaylock.conf)
IMAGES=$(/bin/cat /home/$USER/.cache/rwpspread/rwps_swaylock.conf)
# execute with them
/usr/bin/swaylock $IMAGES --scaling fill
```

## Save Locations

All generate files are stored in `/home/$USER/.cache/` with the `rwps_` prefix.
All generate files are stored in `/home/$USER/.cache/rwpspread/` with the `rwps_` prefix.

To get all files simply do:

```bash
ls /home/$USER/.cache/rwps_*
ls /home/$USER/.cache/rwpspread/
```

> Note: If you are using the wpaperd, `rwpspread` will use its default config path `/home/$USER/.config/wpaperd/`.
Expand All @@ -116,7 +116,7 @@ If you encounter issues after an update or with a new version please do the foll

```bash
# clear cached images
rm /home/$USER/.cache/rwps_*
rm -r /home/$USER/.cache/rwpspread/
# clear wpaperd config (if you use it)
rm /home/$USER/.config/wpaperd/wallpaper.toml
```
Expand Down
5 changes: 3 additions & 2 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Palette {
})
}

pub fn generate_mostused(mut self, save_path: String) -> Result<(), String> {
pub fn generate_mostused(mut self, save_path: &String) -> Result<(), String> {
// define 16 colors sections
let sections = vec![
(0..16, 0..16),
Expand Down Expand Up @@ -96,7 +96,8 @@ impl Palette {
}

// save to json
self.to_json(save_path).map_err(|err| err.to_string())?;
self.to_json(save_path.to_string())
.map_err(|err| err.to_string())?;

// done
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/swaylock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::worker::ResultPaper;
use std::fs;

// generate new file
pub fn generate(papers: &Vec<ResultPaper>, path: String) -> Result<(), String> {
pub fn generate(papers: &Vec<ResultPaper>, path: &String) -> Result<(), String> {
let mut base_string = String::from("");

for paper in papers {
Expand Down
33 changes: 22 additions & 11 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ResultPaper {
pub struct Worker {
hash: String,
monitors: Vec<Monitor>,
cache_location: String,
result_papers: Vec<ResultPaper>,
}

Expand All @@ -31,6 +32,7 @@ impl Worker {
Self {
hash: String::new(),
monitors: Vec::new(),
cache_location: String::new(),
result_papers: Vec::new(),
}
}
Expand All @@ -43,6 +45,11 @@ impl Worker {
// set monitors
self.monitors = mon_vec;

// set cache location
self.cache_location = format!("{}/.cache/rwpspread", env::var("HOME").unwrap());
self.ensure_cache_location(&self.cache_location)
.map_err(|e| e)?;

// calculate hash
let mut hasher = hash_map::DefaultHasher::new();
img.as_bytes().hash(&mut hasher);
Expand All @@ -69,7 +76,7 @@ impl Worker {

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

Expand All @@ -95,25 +102,22 @@ impl Worker {
} else {
// just split
self.result_papers = self
.perform_split(img, config, env::var("PWD").unwrap())
.perform_split(img, config, &env::var("PWD").unwrap())
.map_err(|err| err.to_string())?;
}

// check for palette bool
if config.with_palette && !caches_present || config.force_resplit {
let color_palette = Palette::new(&config.image_path).map_err(|err| err.to_string())?;
color_palette
.generate_mostused(format!("{}/.cache", env::var("HOME").unwrap()))
.generate_mostused(&self.cache_location)
.map_err(|err| err.to_string())?;
}

// check if we need to generate for swaylock
if config.with_swaylock && !caches_present || config.force_resplit {
swaylock::generate(
&self.result_papers,
format!("{}/.cache", env::var("HOME").unwrap()),
)
.map_err(|err| err.to_string())?;
swaylock::generate(&self.result_papers, &self.cache_location)
.map_err(|err| err.to_string())?;
}

// return
Expand All @@ -125,7 +129,7 @@ impl Worker {
&self,
mut img: DynamicImage,
config: &Config,
save_path: String,
save_path: &String,
) -> Result<Vec<ResultPaper>, String> {
/*
Calculate Overall Size
Expand Down Expand Up @@ -261,10 +265,17 @@ impl Worker {
}
}

fn ensure_cache_location(&self, path: &str) -> Result<(), String> {
// try to create, notify if fail
fs::create_dir_all(path).map_err(|_| "Failed to create Cache Directory")?;
// else we're good
Ok(())
}

fn cleanup_cache(&self) {
// wildcard search for our
// images and delete them
for entry in glob(&format!("{}/.cache/rwps_*", env::var("HOME").unwrap())).unwrap() {
for entry in glob(&format!("{}/rwps_*", &self.cache_location)).unwrap() {
if let Ok(path) = entry {
// yeet any file that we cached
fs::remove_file(path).unwrap();
Expand All @@ -274,7 +285,7 @@ impl Worker {

fn check_caches(&self, config: &Config) -> bool {
// what we search for
let base_format = format!("{}/.cache/rwps_", env::var("HOME").unwrap());
let base_format = format!("{}/rwps_*", &self.cache_location);

// path vector
let mut path_list: Vec<(bool, String)> = Vec::with_capacity(3);
Expand Down

0 comments on commit 14c0b97

Please sign in to comment.