Skip to content

Commit

Permalink
when passing no argument to module cd, then switch the cwd to the one…
Browse files Browse the repository at this point in the history
… of the last loaded module
  • Loading branch information
Frederik Delaere committed Oct 3, 2019
1 parent 64764f4 commit 4951311
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
}
Expand Down
8 changes: 6 additions & 2 deletions src/rsmodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -330,6 +330,10 @@ fn read_input<T: AsRef<str>>(filename: T) -> std::io::Result<(PathBuf, String)>
}

fn cd(selected_module: &str) -> Vec<String> {
if selected_module.is_empty() {
return Vec::new();
}

let mut lines: Vec<String> = Vec::new();
let paths = script::get_readme_paths();

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4951311

Please sign in to comment.