Skip to content

Commit

Permalink
implement cli arguments (#12)
Browse files Browse the repository at this point in the history
* implement cli arguments

* fix problems with restoring

* forgot to bump ver LOL

* more implementation

* fix swaybg issues

* add to md

* use -r for small arg
  • Loading branch information
nnyyxxxx authored Oct 18, 2024
1 parent f7f961f commit 01bd882
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 9 deletions.
120 changes: 117 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyprwall"
version = "0.1.1"
version = "0.1.2"
authors = ["Nyx <[email protected]>"]
license = "GPL-2.0"
description = "GUI for setting wallpapers with Hyprpaper, written in blazingly fast Rust!"
Expand All @@ -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
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ An unofficial GUI for setting wallpapers with multiple backends, built with GTK4
- **High capacity** - Hyprwall can handle a large number of wallpapers (over 1000 at one time!) without any issues.
- **Multiple monitors** - Hyprwall supports setting wallpapers on **Multiple** monitors at once.
- **True async** - Hyprwall is built to be asynchronous, it uses tokio to run commands in this manner massively improving performance.
- **Cli args** - Hyprwall supports command line arguments, to view these type **`hyprwall --help`**, **--restore** is one of them, if you wish you can restore your last used wallpaper in the gui with this argument.
- **Supports swaybg, swww, wallutils, feh, and hyprpaper** - Hyprwall supports a variety of wallpaper backends, so you can use it with your preferred wallpaper tool.

## Requirements
Expand Down
79 changes: 78 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ pub fn build_ui(app: &Application) {
backend_combo.append(Some("swww"), "Swww");
backend_combo.append(Some("wallutils"), "Wallutils");
backend_combo.append(Some("feh"), "Feh");
backend_combo.set_active_id(Some("hyprpaper"));

let current_backend = *crate::CURRENT_BACKEND.lock();
let backend_id = match current_backend {
WallpaperBackend::Hyprpaper => "hyprpaper",
WallpaperBackend::Swaybg => "swaybg",
WallpaperBackend::Swww => "swww",
WallpaperBackend::Wallutils => "wallutils",
WallpaperBackend::Feh => "feh",
};
backend_combo.set_active_id(Some(backend_id));

backend_combo.connect_changed(|combo| {
if let Some(active_id) = combo.active_id() {
Expand Down Expand Up @@ -431,3 +440,71 @@ 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);
}
}

pub fn save_wallpaper_backend(backend: &WallpaperBackend) {
let config_path = shellexpand::tilde(CONFIG_FILE).into_owned();
if let Ok(mut contents) = fs::read_to_string(&config_path) {
let backend_str = match backend {
WallpaperBackend::Hyprpaper => "hyprpaper",
WallpaperBackend::Swaybg => "swaybg",
WallpaperBackend::Swww => "swww",
WallpaperBackend::Wallutils => "wallutils",
WallpaperBackend::Feh => "feh",
};
if let Some(line) = contents.lines().find(|line| line.starts_with("backend = ")) {
contents = contents.replace(line, &format!("backend = {}", backend_str));
} else {
contents.push_str(&format!("\nbackend = {}", backend_str));
}
let _ = fs::write(config_path, contents);
}
}

pub fn load_wallpaper_backend() -> Option<WallpaperBackend> {
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("backend = "))
.and_then(|line| {
let backend_str = line.trim_start_matches("backend = ");
match backend_str {
"hyprpaper" => Some(WallpaperBackend::Hyprpaper),
"swaybg" => Some(WallpaperBackend::Swaybg),
"swww" => Some(WallpaperBackend::Swww),
"wallutils" => Some(WallpaperBackend::Wallutils),
"feh" => Some(WallpaperBackend::Feh),
_ => None,
}
})
})
}
Loading

0 comments on commit 01bd882

Please sign in to comment.