Skip to content

Commit

Permalink
feat: Add feature to get currently applied wall
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHamkerCat committed Sep 15, 2022
1 parent 4fbc836 commit f089000
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Quick links:
- [x] Automatically change wallpapers using [`cron`](https://crontab.guru/) expressions (wallpaper slideshow).
- [x] Supports multiple modes [center, fill, scale, tile]
- [x] Saving fetched wallpapers to a file.
- [ ] Aquiring currently applied wallpaper and saving it to a file. (if set by feh)
- [x] Aquiring currently applied wallpaper and saving it to a file.

## Demo

Expand Down Expand Up @@ -92,6 +92,12 @@ $ wallrus set --query=batman --save=a.png
$ wallrus set --query=avengers --cron="0 * * * *"
```

### Get current wallpaper and save it to a file

```console
$ wallrus get --file=wall.png
```

## Installation

```console
Expand Down
8 changes: 8 additions & 0 deletions src/argparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ pub fn arguments<'t>() -> Command<'t> {
.required(false)
)
,
).subcommand(
Command::new("get")
.about("Get currenty set wallpaper and save it in a file.")
.arg(
arg!(-f --file "File name to save the wallpaper in.")
.takes_value(true)
.required(true)
)
)
}
4 changes: 4 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ pub fn gen_tmpfile() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 6)
)
}

pub fn get_home_dir() -> std::string::String {
std::env::var("HOME").unwrap()
}
48 changes: 48 additions & 0 deletions src/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use clap::ArgMatches;
use colored::Colorize;

use crate::functions::{get_home_dir, program_exists};

pub fn get_wallpaper(subc: &ArgMatches) {
let path = subc.value_of("file").unwrap();

if !program_exists("feh") {
eprintln!(
"{}",
"feh is not installed, install it and try again!".red()
);
}

let fehconf =
match std::fs::read_to_string(get_home_dir() + "/.fehbg") {
Ok(fehconf) => fehconf,
Err(_) => {
eprintln!(
"{}",
"Couln't find feh config file.".red()
);
return;
}
};
let wallpaper_file = fehconf
.split('\n')
.nth(1)
.unwrap()
.split_whitespace()
.nth_back(0)
.unwrap()
.replace('\'', "");

if !std::path::Path::new(&wallpaper_file).exists() {
println!(
"{}",
"Your current wallpaper file seems missing.".red()
);
return;
}

match std::fs::copy(wallpaper_file, path) {
Ok(_) => println!("{}", "Wallpaper saved to file!".green()),
Err(e) => eprintln!("{}", e),
};
}
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
pub mod argparse;
pub mod functions;
mod get;
mod set;

fn main() {
let args = argparse::arguments().get_matches();

match args.subcommand() {
Some(("set", subc)) => {
set::set_wallpaper(subc).unwrap();
if let Some((name, subc)) = args.subcommand() {
match name {
"set" => set::set_wallpaper(subc).unwrap(),
"get" => get::get_wallpaper(subc),
_ => println!("Lost?? try --help"),
}
_ => println!("Lost?? try --help"),
}
};
}
5 changes: 0 additions & 5 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ pub async fn set_wall_using_query(
set_wall_using_path(&filename[..], mode, noxinerama);
println!("{}", "Wallpaper set successfully!".green());

// remove the wallpaper if user doesn't use --save
if save.is_none() {
std::fs::remove_file(filename)?;
}

if let Some(cron_expression) = cron {
let exp_len = cron_expression.split_whitespace().count();

Expand Down

0 comments on commit f089000

Please sign in to comment.