Fix directory change lag (#672)
This commit is contained in:
parent
c158d29577
commit
affb9696c7
|
@ -1,4 +1,3 @@
|
||||||
use nu_engine::env::current_dir_str;
|
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
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_val: Option<Value> = call.opt(engine_state, stack, 0)?;
|
||||||
|
|
||||||
let (path, span) = match path_val {
|
let (path, span) = match path_val {
|
||||||
Some(v) => {
|
Some(v) => (v.as_string()?, v.span()?),
|
||||||
let path = nu_path::canonicalize_with(
|
|
||||||
v.as_string()?,
|
|
||||||
current_dir_str(engine_state, stack)?,
|
|
||||||
)?;
|
|
||||||
(path.to_string_lossy().to_string(), v.span()?)
|
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
let path = nu_path::expand_tilde("~");
|
let path = nu_path::expand_tilde("~");
|
||||||
(path.to_string_lossy().to_string(), call.head)
|
(path.to_string_lossy().to_string(), call.head)
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
|
|
||||||
use nu_engine::env::current_dir_str;
|
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_path::expand_path_with;
|
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
|
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)?;
|
let rest: Vec<String> = call.rest(engine_state, stack, 1)?;
|
||||||
|
|
||||||
for (index, item) in vec![target].into_iter().chain(rest).enumerate() {
|
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(&item) {
|
||||||
match OpenOptions::new().write(true).create(true).open(&path) {
|
|
||||||
Ok(_) => continue,
|
Ok(_) => continue,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return Err(ShellError::CreateNotPossible(
|
return Err(ShellError::CreateNotPossible(
|
||||||
|
|
|
@ -37,9 +37,8 @@ impl Command for Enter {
|
||||||
|
|
||||||
let cwd = current_dir(engine_state, stack)?;
|
let cwd = current_dir(engine_state, stack)?;
|
||||||
|
|
||||||
if let Ok(s) = new_path.as_string() {
|
if let Ok(s) = new_path.as_path() {
|
||||||
let path = nu_path::expand_path_with(s, &cwd);
|
if !s.exists() {
|
||||||
if !path.exists() {
|
|
||||||
return Err(ShellError::DirectoryNotFound(new_path.span()?));
|
return Err(ShellError::DirectoryNotFound(new_path.span()?));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ use sys_locale::get_locale;
|
||||||
pub use unit::*;
|
pub use unit::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::{cmp::Ordering, fmt::Debug};
|
use std::{cmp::Ordering, fmt::Debug};
|
||||||
|
|
||||||
use crate::ast::{CellPath, PathMember};
|
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> {
|
pub fn as_block(&self) -> Result<BlockId, ShellError> {
|
||||||
match self {
|
match self {
|
||||||
Value::Block { val, .. } => Ok(*val),
|
Value::Block { val, .. } => Ok(*val),
|
||||||
|
|
|
@ -468,8 +468,11 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
// FIXME: permanent state changes like this hopefully in time can be removed
|
// FIXME: permanent state changes like this hopefully in time can be removed
|
||||||
// and be replaced by just passing the cwd in where needed
|
// and be replaced by just passing the cwd in where needed
|
||||||
let cwd = nu_engine::env::current_dir_str(&engine_state, &stack)?;
|
if let Some(cwd) = stack.get_env_var(&engine_state, "PWD") {
|
||||||
let _ = std::env::set_current_dir(cwd);
|
let path = cwd.as_string()?;
|
||||||
|
let _ = std::env::set_current_dir(path);
|
||||||
|
engine_state.env_vars.insert("PWD".into(), cwd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(Signal::CtrlC) => {
|
Ok(Signal::CtrlC) => {
|
||||||
// `Reedline` clears the line content. New prompt is shown
|
// `Reedline` clears the line content. New prompt is shown
|
||||||
|
|
Loading…
Reference in New Issue
Block a user