From 4951311cdab723bc59908163f296745c65e0988b Mon Sep 17 00:00:00 2001 From: Frederik Delaere Date: Thu, 3 Oct 2019 21:14:50 +0200 Subject: [PATCH] when passing no argument to module cd, then switch the cwd to the one of the last loaded module --- README.md | 3 ++- src/main.rs | 24 +++++++++++++++++++++++- src/rsmodules.rs | 8 ++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7c090e2..8c34e40 100755 --- a/README.md +++ b/README.md @@ -66,7 +66,8 @@ D perl/5.14.1 | Perl 5.14.1 is a highly capable, feature-rich programming langua * ```module autoload append|prepend|remove|list|purge [modulename(s)]``` Manages the auto loading of modules by adding them to your startup scripts. * ```module delete [modulename(s)]``` Deletes one or more modulefiles. But only if you have the permissions to do so. * ```module readme [modulename]``` Looks for a manpage or README file in the module installation folder and displays the contents of this file. - * ```module cd [modulename]``` Changes your current working directory to the module installation folder. + * ```module cd [modulename]``` Changes your current working directory to the module installation folder. When you don't provide a modulename + the working directory is changed to the module installation folder of the last loaded module. * ```module edit [modulename]``` Opens the modulefile in your $EDITOR or if this variable is not present in ```vi -e```. * The output is not redirected to stderr, but to stdout. So you are able to use grep / rg on the output and it doesn't trigger errors in pipelines. * RSModules is fast because it's written in a compiled language and it is using cache files for listing the modules. diff --git a/src/main.rs b/src/main.rs index d1df7fc..56b2dfa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,7 +182,9 @@ static LONG_HELP: &str = " * cd [modulename] Changes your current working directory to the module - installation folder. + installation folder. When you don't provide a modulename + the working directory is changed to the module installation + folder of the last loaded module. * edit [modulename] Opens the modulefile in your $EDITOR or if this variable is not @@ -412,12 +414,32 @@ fn run(args: &[String]) { } } + let loadedmodules: String; if num_hits != 1 { usage(true); return; } else { matches = true; + if command_hit == "cd" { + modulename = if modulename.is_empty() { + match env::var(rsmod::ENV_LOADEDMODULES) { + Ok(list) => loadedmodules = list, + Err(_) => { + loadedmodules = String::from(""); + } + }; + + let mut loadedmodules: Vec<&str> = loadedmodules.split(':').collect(); + loadedmodules.retain(|&x| x != ""); + + let loadedmodule: &str = if !loadedmodules.is_empty() { loadedmodules[0] } else { "" }; + loadedmodule + } else { + modulename + }; + } + if command_hit == "add" { command_hit = "load"; } diff --git a/src/rsmodules.rs b/src/rsmodules.rs index a386479..ce4cb3b 100644 --- a/src/rsmodules.rs +++ b/src/rsmodules.rs @@ -49,7 +49,7 @@ mod manage; mod script; static DEFAULT_MODULE_PATH: &str = "/usr/local"; -static ENV_LOADEDMODULES: &str = "LOADEDMODULES"; // name of an env var +pub static ENV_LOADEDMODULES: &str = "LOADEDMODULES"; // name of an env var static ENV_UNDO: &str = "RSMODULES_UNDO"; // name of an env var #[derive(Debug, Default, Options)] @@ -330,6 +330,10 @@ fn read_input>(filename: T) -> std::io::Result<(PathBuf, String)> } fn cd(selected_module: &str) -> Vec { + if selected_module.is_empty() { + return Vec::new(); + } + let mut lines: Vec = Vec::new(); let paths = script::get_readme_paths(); @@ -513,7 +517,7 @@ fn module_action(rsmod: &mut Rsmodule, action: &str) { reversed_modules.reverse(); - if rsmod.arg == "" { + if rsmod.cmd != "cd" && rsmod.arg == "" { // TODO: only print usage info about this subcommand super::usage(true); return;