From f0890006412abaf1a944f66461f4a45ec6bc6e62 Mon Sep 17 00:00:00 2001 From: TheHamkerCat Date: Thu, 15 Sep 2022 16:25:20 +0530 Subject: [PATCH] feat: Add feature to get currently applied wall --- README.md | 8 +++++++- src/argparse.rs | 8 ++++++++ src/functions.rs | 4 ++++ src/get.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 +++++++----- src/set.rs | 5 ----- 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/get.rs diff --git a/README.md b/README.md index a9d17e5..d570c14 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/argparse.rs b/src/argparse.rs index f2ce3bb..f1fafb3 100644 --- a/src/argparse.rs +++ b/src/argparse.rs @@ -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) + ) ) } diff --git a/src/functions.rs b/src/functions.rs index 489b5fd..7f73978 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -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() +} diff --git a/src/get.rs b/src/get.rs new file mode 100644 index 0000000..17f94a2 --- /dev/null +++ b/src/get.rs @@ -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), + }; +} diff --git a/src/main.rs b/src/main.rs index 17155c1..76b7cc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"), - } + }; } diff --git a/src/set.rs b/src/set.rs index 01faaff..4bc5809 100644 --- a/src/set.rs +++ b/src/set.rs @@ -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();