Pi66

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

← back to log

Add: auto complete

author: pixel
date: 2025-05-03 21:06
hash: e21a5034cf1c57b6c6d3c0732750f580a71daa42

Diffstat:

M

Cargo.lock
12 +++++

M

Cargo.toml
5 +-

M

Makefile
1

A

src/completions.rs
67 ++++++++++++++++++++++++++++++

M

src/create_templates.rs
30 ++++++++-----

M

src/main.rs
41 ++++++++++++------
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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 \"{}\" -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_colors(!arg.quit);
     };
 }
-
-
-