Skip to content

Commit

Permalink
implement cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Oct 18, 2024
1 parent f7f961f commit 4e70c3d
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
114 changes: 114 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 @@ -16,6 +16,7 @@ categories = ["gui"]
edition = "2021"

[dependencies]
clap = { version = "4.3", features = ["derive"] }
gtk = { version = "0.9.2", package = "gtk4" }
glib = { version = "0.18.0", features = ["v2_68"] }
gio = "0.18.0"
Expand Down
27 changes: 27 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,30 @@ pub fn custom_error_popup(title: &str, text: &str, modal: bool) {

dialog.show();
}

pub fn load_last_wallpaper() -> Option<String> {
let config_path = shellexpand::tilde(CONFIG_FILE).into_owned();
fs::File::open(config_path).ok().and_then(|mut file| {
let mut contents = String::new();
file.read_to_string(&mut contents).ok()?;
contents
.lines()
.find(|line| line.starts_with("last_wallpaper = "))
.map(|line| line.trim_start_matches("last_wallpaper = ").to_string())
})
}

pub fn save_last_wallpaper(path: &str) {
let config_path = shellexpand::tilde(CONFIG_FILE).into_owned();
if let Ok(mut contents) = fs::read_to_string(&config_path) {
if let Some(line) = contents
.lines()
.find(|line| line.starts_with("last_wallpaper = "))
{
contents = contents.replace(line, &format!("last_wallpaper = {}", path));
} else {
contents.push_str(&format!("\nlast_wallpaper = {}", path));
}
let _ = fs::write(config_path, contents);
}
}
28 changes: 27 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod gui;

use clap::Parser;
use gtk::{prelude::*, Application};
use lazy_static::lazy_static;
use parking_lot::Mutex;
Expand All @@ -20,10 +21,24 @@ pub enum WallpaperBackend {
Feh,
}

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(long, help = "Restore the last selected wallpaper")]
restore: bool,
}

fn main() {
let cli = Cli::parse();

let rt = Runtime::new().expect("Failed to create Tokio runtime");
let _guard = rt.enter();

if cli.restore {
restore_last_wallpaper();
return;
}

let app = Application::builder()
.application_id("nnyyxxxx.hyprwall")
.build();
Expand All @@ -35,7 +50,10 @@ fn main() {
pub fn set_wallpaper(path: String) {
glib::spawn_future_local(async move {
match set_wallpaper_internal(&path).await {
Ok(_) => println!("Wallpaper set successfully"),
Ok(_) => {
println!("Wallpaper set successfully");
gui::save_last_wallpaper(&path);
}
Err(e) => {
eprintln!("Error setting wallpaper: {}", e);
gui::custom_error_popup("Error setting wallpaper", &e, true);
Expand Down Expand Up @@ -250,3 +268,11 @@ async fn drop_all_wallpapers(backend: WallpaperBackend) {
_ => {}
}
}

fn restore_last_wallpaper() {
if let Some(last_wallpaper) = gui::load_last_wallpaper() {
set_wallpaper(last_wallpaper);
} else {
eprintln!("No last wallpaper found to restore");
}
}

0 comments on commit 4e70c3d

Please sign in to comment.