From a4d59ef9a94dad69c569e5bdfd8ca14f31cf51f2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 14 Aug 2025 20:41:25 +0000 Subject: [PATCH] feat: Add --cleanup command-line option This commit introduces a new `--cleanup` flag to the `pathman` tool. When this option is provided on the command line, the tool will automatically perform a cleanup of the specified environment variable. The cleanup process consists of two steps: 1. Removing any paths that no longer exist on the filesystem. 2. Removing any duplicate path entries. After cleaning, the tool updates the environment variable with the new, cleaned-up value and exits. This entire process is non-interactive and does not launch the TUI, making it suitable for use in scripts. To implement this, the `remove_dups` and `remove_nonexistent` functions in the `editor` module have been made public so they can be called from `main.rs`. --- src/editor.rs | 4 ++-- src/main.rs | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 3499827..7451196 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -36,7 +36,7 @@ fn find_dups(vec: &[T]) -> Vec { } /// Dedup a vector, keeping only the first duplicate -fn remove_dups(vec: Vec) -> Vec { +pub fn remove_dups(vec: Vec) -> Vec { let mut new = Vec::with_capacity(vec.len()); for e in vec { @@ -48,7 +48,7 @@ fn remove_dups(vec: Vec) -> Vec { new } -fn remove_nonexistent>(vec: Vec) -> Vec { +pub fn remove_nonexistent>(vec: Vec) -> Vec { vec.into_iter() .filter(|s| { let p: &Path = s.as_ref(); diff --git a/src/main.rs b/src/main.rs index 798a191..af14760 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate term; use std::env; -use clap::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; mod editor; @@ -17,6 +17,12 @@ fn main() { let matches = Command::new("pathman") .disable_help_subcommand(true) .disable_version_flag(true) + .arg( + Arg::new("cleanup") + .long("cleanup") + .action(ArgAction::SetTrue) + .help("Automatically clean up the path"), + ) .arg( Arg::new("sep") .short('s') @@ -54,14 +60,23 @@ fn main() { Vec::new() }; - let editor = editor::SimpleEditor::new(path_list); - if let Some(v) = editor.run() { - let new_val = v.join(sep_char); + if *matches.get_one::("cleanup").unwrap() { + let cleaned_path = editor::remove_nonexistent(path_list); + let cleaned_path = editor::remove_dups(cleaned_path); + + let new_val = cleaned_path.join(sep_char); shell.setenv(var_name, &new_val); - eprintln!(); - eprintln!("{} environment variable updated", var_name); + eprintln!("{} environment variable cleaned up", var_name); } else { - eprintln!(); - eprintln!("{} environment variable unchanged.", var_name); + let editor = editor::SimpleEditor::new(path_list); + if let Some(v) = editor.run() { + let new_val = v.join(sep_char); + shell.setenv(var_name, &new_val); + eprintln!(); + eprintln!("{} environment variable updated", var_name); + } else { + eprintln!(); + eprintln!("{} environment variable unchanged.", var_name); + } } }