Pi66

Home | Repos | About | Blog | Gaza

walrs
pywal but if its Fast-Minimal-Simple
git clone https://git.pi66.xyz/walrs

About | Files | Commits | Refs | License


Add: auto complete

Author: pixel
Date: 2025-05-03 21:06
commit: e21a5034

Stat

M Cargo.lock 12 +11 -1
M Cargo.toml 5 +3 -2
A Makefile 1 +1 -0
A src/completions.rs 67 +67 -0
M src/create_templates.rs 30 +18 -12
M src/main.rs 41 +28 -13

Diff

diff --git a/Cargo.lock b/Cargo.lock
index da3cc71..6b5237e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -251,6 +251,15 @@ dependencies = [
  "strsim",
 ]

+[[package]] +name = "clap_complete" +version = "4.5.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8c97f3a6f02b9e24cadc12aaba75201d18754b53ea0a9d99642f806ccdb4c9" +dependencies = [ + "clap 4.5.37", +] + [[package]] name = "clap_derive" version = "4.5.32" ...

Show all

diff --git a/Cargo.lock b/Cargo.lock
index da3cc71..6b5237e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -251,6 +251,15 @@ dependencies = [
  "strsim",
 ]

+[[package]] +name = "clap_complete" +version = "4.5.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be8c97f3a6f02b9e24cadc12aaba75201d18754b53ea0a9d99642f806ccdb4c9" +dependencies = [ + "clap 4.5.37", +] + [[package]] name = "clap_derive" version = "4.5.32" @@ -1344,9 +1353,10 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"

[[package]] name = "walrs" -version = "1.0.7" +version = "1.0.8" dependencies = [ "clap 4.5.37", + "clap_complete", "color-thief", "dirs-next", "image", diff --git a/Cargo.toml b/Cargo.toml index f5e8726..c4c6dd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,14 @@ [package] name = "walrs" -version = "1.0.7" +version = "1.0.8" edition = "2024" -description = "walrs is a fast color scheme generator." +description = "walrs - Generate colorscheme from image." license = "GPL" repository = "https://github.com/pixel2175/walrs"

[dependencies] clap = { version = "4.5.34", features = ["derive"] } +clap_complete = "4.5.48" color-thief = "0.2.2" dirs-next = "2.0.0" image = "0.25.6" diff --git a/Makefile b/Makefile index 1a46165..ba41703 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ build: install: build sudo install -m755 target/release/walrs /usr/bin/walrs sudo install -d ./templates/ /etc/walrs/templates + /usr/bin/walrs --install-completions -q sudo cp -r templates/* /etc/walrs/templates/

uninstall: diff --git a/src/completions.rs b/src/completions.rs new file mode 100644 index 0000000..f496f0a --- /dev/null +++ b/src/completions.rs @@ -0,0 +1,67 @@ +use clap::CommandFactory; +use clap_complete::{Shell, generate}; +use dirs_next::home_dir; +use std::fs; +use std::path::PathBuf; + +pub fn install_completions() -> std::io::Result<()> { + let shell = detect_shell(); + let mut cmd = crate::Arg::command(); + let app_name = cmd.get_name().to_string(); + + let (target_dir, completion_file) = match shell { + Shell::Bash => ( + home_dir().unwrap().join(".bash_completion.d"), + PathBuf::from(app_name.clone()), + ), + Shell::Zsh => ( + home_dir().unwrap().join(".zsh/completions"), + PathBuf::from(format!("_{}", app_name)), + ), + Shell::Fish => ( + home_dir().unwrap().join(".config/fish/completions"), + PathBuf::from(format!("{}.fish", app_name)), + ), + Shell::PowerShell => ( + home_dir().unwrap().join("Documents/PowerShell/Modules"), + PathBuf::from(format!("{}.ps1", app_name)), + ), + _ => { + return Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "Unsupported shell", + )); + } + }; + + fs::create_dir_all(&target_dir)?; + + let mut file = fs::File::create(target_dir.join(completion_file))?; + generate(shell, &mut cmd, app_name, &mut file); + + Ok(()) +} + +fn detect_shell() -> Shell { + std::env::var("SHELL") + .ok() + .and_then(|shell| { + if shell.contains("zsh") { + Some(Shell::Zsh) + } else if shell.contains("bash") { + Some(Shell::Bash) + } else if shell.contains("fish") { + Some(Shell::Fish) + } else { + None + } + }) + .unwrap_or_else(|| { + if cfg!(windows) { + Shell::PowerShell + } else { + // Default to bash if detection fails + Shell::Bash + } + }) +} diff --git a/src/create_templates.rs b/src/create_templates.rs index ba1a8e1..6f28c7f 100644 --- a/src/create_templates.rs +++ b/src/create_templates.rs @@ -1,5 +1,8 @@ use crate::utils::{get_cache_folder, get_config_folder}; -use std::{fs::{self, create_dir_all, read_to_string, write}, path::Path}; +use std::{ + fs::{self, create_dir_all, read_to_string, write}, + path::Path, +};

fn change(template: &str, colors: (u8, u8, u8), alpha: u8) -> String { let (r, g, b) = colors; @@ -26,16 +29,19 @@ fn fill_template( template: &str, colors: &(Vec<(u8, u8, u8)>, u8), wallpaper: &str, - output_dir:String + output_dir: String, ) { let output_path = if output_dir == "None" { -format!( - "{}/wal/{}", - get_cache_folder().expect("Can't find cache folder"), - template_name -) - }else { -Path::new(&output_dir).join(template_name).to_string_lossy().to_string() + format!( + "{}/wal/{}", + get_cache_folder().expect("Can't find cache folder"), + template_name + ) + } else { + Path::new(&output_dir) + .join(template_name) + .to_string_lossy() + .to_string() }; let alpha = colors.1;

@@ -121,7 +127,7 @@ Path::new(&output_dir).join(template_name).to_string_lossy().to_string() write(output_path, result).expect("Failed to write filled template"); }

-pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str,output_dir:String) { +pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str, output_dir: String) { let system_template_path = "/etc/walrs/templates/"; let user_template_path = format!("{}/walrs/templates/", get_config_folder().unwrap()); let cache_path = format!("{}/wal/", get_cache_folder().unwrap()); @@ -139,7 +145,7 @@ pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str,output_d let Some(name) = entry.file_name().into_string().ok() else { continue; }; - fill_template(&name, &content, &colors, wallpaper,output_dir.clone()); + fill_template(&name, &content, &colors, wallpaper, output_dir.clone()); } } } @@ -162,7 +168,7 @@ pub fn create_template(colors: (Vec<(u8, u8, u8)>, u8), wallpaper: &str,output_d let user_file_path = format!("{}{}", user_template_path, name); let _ = write(&user_file_path, &content);

- fill_template(&name, &content, &colors, wallpaper,output_dir.clone()); + fill_template(&name, &content, &colors, wallpaper, output_dir.clone()); } } } diff --git a/src/main.rs b/src/main.rs index d43eb0c..8236871 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod completions; mod create_templates; mod get_colors; mod reload; @@ -15,8 +16,8 @@ use utils::*; #[derive(Parser, Debug)] #[command( name = "walrs", - version = "v1.0.7", - about = "walrs - Generate colorscheme from image" + version = env!("CARGO_PKG_VERSION"), + about = "walrs - Generate colorscheme from image", )] struct Arg { /// path/to/your/wal.png | for random image: path/to/your/wallpapers/ @@ -50,6 +51,10 @@ struct Arg { /// specify the brightness value -128 => 127 #[arg(short = 'b', allow_hyphen_values = true)] brightness: Option<i8>, + + /// Install completions for the current shell + #[arg(long = "install-completions", hide = true)] + install_completions: bool, }

fn image_path(image: Option<String>, send: bool) -> String { @@ -65,11 +70,11 @@ fn image_path(image: Option<String>, send: bool) -> String { .arg(format!("find &#34;{}&#34; -type f | sort -R | head -n1", p)) .output() .unwrap() - .stdout, + .stdout, ) - .unwrap() - .trim() - .to_string() + .unwrap() + .trim() + .to_string() } } None => { @@ -91,6 +96,19 @@ fn image_path(image: Option<String>, send: bool) -> String { fn main() { let arg = Arg::parse();

+ if arg.install_completions { + if let Err(_) = completions::install_completions() { + warning("Completions", "Failed to install completions", !arg.quit); + exit(1); + } + info( + "Completions", + "Completions installed successfully!", + !arg.quit, + ); + exit(0) + } + let output_dir = match arg.output { Some(v) => { if !Path::new(&v).is_dir() { @@ -98,8 +116,8 @@ fn main() { exit(1) } v - }, - None => "None".to_string() + } + None => "None".to_string(), }; if arg.reload_nowal { reload(!arg.quit, true); @@ -122,7 +140,7 @@ fn main() { let palette = get_colors(&image_path, !arg.quit, arg.brightness, arg.saturation); info("Generate", "generate colors", !arg.quit);

- create_template(palette, &image_path,output_dir); + create_template(palette, &image_path, output_dir); info("Template", "create templates", !arg.quit); exit(0) }; @@ -133,13 +151,10 @@ fn main() { let palette = get_colors(&image_path, !arg.quit, arg.brightness, arg.saturation); info("Generate", "generate colors", !arg.quit);

- create_template(palette, &image_path,output_dir); + create_template(palette, &image_path, output_dir); info("Template", "create templates", !arg.quit);

     reload(!arg.quit, true);
     print&#95;colors(!arg.quit);
 };

} - - -

// Created by Pi66

// Copyright ©Pi66 2026