Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn find_dups<T: Eq + Hash>(vec: &[T]) -> Vec<usize> {
}

/// Dedup a vector, keeping only the first duplicate
fn remove_dups<T: Eq>(vec: Vec<T>) -> Vec<T> {
pub fn remove_dups<T: Eq>(vec: Vec<T>) -> Vec<T> {
let mut new = Vec::with_capacity(vec.len());

for e in vec {
Expand All @@ -48,7 +48,7 @@ fn remove_dups<T: Eq>(vec: Vec<T>) -> Vec<T> {
new
}

fn remove_nonexistent<T: AsRef<Path>>(vec: Vec<T>) -> Vec<T> {
pub fn remove_nonexistent<T: AsRef<Path>>(vec: Vec<T>) -> Vec<T> {
vec.into_iter()
.filter(|s| {
let p: &Path = s.as_ref();
Expand Down
31 changes: 23 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate term;

use std::env;

use clap::{Arg, Command};
use clap::{Arg, ArgAction, Command};

mod editor;

Expand All @@ -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')
Expand Down Expand Up @@ -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::<bool>("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);
}
}
}