Add in auto-cd if you pass just a directory (#479)

* Add in auto-cd if you pass just a directory

* clippy
This commit is contained in:
JT 2021-12-13 10:41:34 +13:00 committed by GitHub
parent c33d082ecc
commit d1d1402512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::io::{BufRead, BufReader, Write};
use std::path::Path;
use std::process::{Command as CommandSys, Stdio};
use std::sync::atomic::Ordering;
use std::sync::mpsc;
@ -47,13 +48,31 @@ impl Command for External {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let name: Spanned<String> = call.req(engine_state, stack, 0)?;
let mut name: Spanned<String> = call.req(engine_state, stack, 0)?;
let args: Vec<String> = call.rest(engine_state, stack, 1)?;
let last_expression = call.has_flag("last_expression");
let env_vars = stack.get_env_vars();
let config = stack.get_config()?;
// Check if this is a single call to a directory, if so auto-cd
let path = nu_path::expand_path(&name.item);
name.item = path.to_string_lossy().to_string();
let path = Path::new(&name.item);
if (name.item.starts_with('.') || name.item.starts_with('/') || name.item.starts_with('\\'))
&& path.is_dir()
&& args.is_empty()
{
// We have an auto-cd
let _ = std::env::set_current_dir(&path);
//FIXME: this only changes the current scope, but instead this environment variable
//should probably be a block that loads the information from the state in the overlay
stack.add_env_var("PWD".into(), name.item.clone());
return Ok(PipelineData::new(call.head));
}
let command = ExternalCommand {
name,
args,