Fix directory change lag (#672)

This commit is contained in:
JT 2022-01-05 16:50:27 +11:00 committed by GitHub
parent c158d29577
commit affb9696c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 17 deletions

View File

@ -1,4 +1,3 @@
use nu_engine::env::current_dir_str;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
@ -32,13 +31,7 @@ impl Command for Cd {
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
let (path, span) = match path_val {
Some(v) => {
let path = nu_path::canonicalize_with(
v.as_string()?,
current_dir_str(engine_state, stack)?,
)?;
(path.to_string_lossy().to_string(), v.span()?)
}
Some(v) => (v.as_string()?, v.span()?),
None => {
let path = nu_path::expand_tilde("~");
(path.to_string_lossy().to_string(), call.head)

View File

@ -1,8 +1,6 @@
use std::fs::OpenOptions;
use nu_engine::env::current_dir_str;
use nu_engine::CallExt;
use nu_path::expand_path_with;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
@ -41,8 +39,7 @@ impl Command for Touch {
let rest: Vec<String> = call.rest(engine_state, stack, 1)?;
for (index, item) in vec![target].into_iter().chain(rest).enumerate() {
let path = expand_path_with(&item, current_dir_str(engine_state, stack)?);
match OpenOptions::new().write(true).create(true).open(&path) {
match OpenOptions::new().write(true).create(true).open(&item) {
Ok(_) => continue,
Err(err) => {
return Err(ShellError::CreateNotPossible(

View File

@ -37,9 +37,8 @@ impl Command for Enter {
let cwd = current_dir(engine_state, stack)?;
if let Ok(s) = new_path.as_string() {
let path = nu_path::expand_path_with(s, &cwd);
if !path.exists() {
if let Ok(s) = new_path.as_path() {
if !s.exists() {
return Err(ShellError::DirectoryNotFound(new_path.span()?));
}
}

View File

@ -18,6 +18,7 @@ use sys_locale::get_locale;
pub use unit::*;
use std::collections::HashMap;
use std::path::PathBuf;
use std::{cmp::Ordering, fmt::Debug};
use crate::ast::{CellPath, PathMember};
@ -173,6 +174,17 @@ impl Value {
}
}
pub fn as_path(&self) -> Result<PathBuf, ShellError> {
match self {
Value::String { val, .. } => Ok(PathBuf::from(val)),
x => Err(ShellError::CantConvert(
"path".into(),
x.get_type().to_string(),
self.span()?,
)),
}
}
pub fn as_block(&self) -> Result<BlockId, ShellError> {
match self {
Value::Block { val, .. } => Ok(*val),

View File

@ -468,8 +468,11 @@ fn main() -> Result<()> {
}
// FIXME: permanent state changes like this hopefully in time can be removed
// and be replaced by just passing the cwd in where needed
let cwd = nu_engine::env::current_dir_str(&engine_state, &stack)?;
let _ = std::env::set_current_dir(cwd);
if let Some(cwd) = stack.get_env_var(&engine_state, "PWD") {
let path = cwd.as_string()?;
let _ = std::env::set_current_dir(path);
engine_state.env_vars.insert("PWD".into(), cwd);
}
}
Ok(Signal::CtrlC) => {
// `Reedline` clears the line content. New prompt is shown