This commit is contained in:
JT
2021-10-25 17:01:02 +13:00
parent ab9d6b206d
commit b6d269e90a
63 changed files with 1075 additions and 1013 deletions

View File

@ -1,8 +1,9 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Cd;
impl Command for Cd {
@ -22,8 +23,8 @@ impl Command for Cd {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let path: Option<String> = call.opt(context, 0)?;
let path = match path {
@ -41,6 +42,6 @@ impl Command for Cd {
//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
context.add_env_var("PWD".into(), path);
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View File

@ -6,10 +6,11 @@ use nu_engine::CallExt;
use nu_path::canonicalize_with;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
use crate::filesystem::util::FileStructure;
#[derive(Clone)]
pub struct Cp;
#[allow(unused_must_use)]
@ -39,8 +40,8 @@ impl Command for Cp {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let source: String = call.req(context, 0)?;
let destination: String = call.req(context, 1)?;
let interactive = call.has_flag("interactive");
@ -202,6 +203,6 @@ impl Command for Cp {
}
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View File

@ -2,8 +2,9 @@ use chrono::{DateTime, Utc};
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Ls;
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
@ -28,8 +29,8 @@ impl Command for Ls {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let pattern = if let Some(expr) = call.positional.get(0) {
let result = eval_expression(context, expr)?;
let mut result = result.as_string()?;
@ -50,69 +51,66 @@ impl Command for Ls {
let call_span = call.head;
let glob = glob::glob(&pattern).unwrap();
Ok(Value::Stream {
stream: glob
.into_iter()
.map(move |x| match x {
Ok(path) => match std::fs::symlink_metadata(&path) {
Ok(metadata) => {
let is_file = metadata.is_file();
let is_dir = metadata.is_dir();
let filesize = metadata.len();
Ok(glob
.into_iter()
.map(move |x| match x {
Ok(path) => match std::fs::symlink_metadata(&path) {
Ok(metadata) => {
let is_file = metadata.is_file();
let is_dir = metadata.is_dir();
let filesize = metadata.len();
let mut cols = vec!["name".into(), "type".into(), "size".into()];
let mut cols = vec!["name".into(), "type".into(), "size".into()];
let mut vals = vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
},
if is_file {
Value::string("File", call_span)
} else if is_dir {
Value::string("Dir", call_span)
} else {
Value::Nothing { span: call_span }
},
Value::Filesize {
val: filesize as i64,
span: call_span,
},
];
if let Ok(date) = metadata.modified() {
let utc: DateTime<Utc> = date.into();
cols.push("modified".into());
vals.push(Value::Date {
val: utc.into(),
span: call_span,
});
}
Value::Record {
cols,
vals,
let mut vals = vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
}
},
if is_file {
Value::string("File", call_span)
} else if is_dir {
Value::string("Dir", call_span)
} else {
Value::Nothing { span: call_span }
},
Value::Filesize {
val: filesize as i64,
span: call_span,
},
];
if let Ok(date) = metadata.modified() {
let utc: DateTime<Utc> = date.into();
cols.push("modified".into());
vals.push(Value::Date {
val: utc.into(),
span: call_span,
});
}
Err(_) => Value::Record {
cols: vec!["name".into(), "type".into(), "size".into()],
vals: vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
},
Value::Nothing { span: call_span },
Value::Nothing { span: call_span },
],
Value::Record {
cols,
vals,
span: call_span,
},
}
}
Err(_) => Value::Record {
cols: vec!["name".into(), "type".into(), "size".into()],
vals: vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
},
Value::Nothing { span: call_span },
Value::Nothing { span: call_span },
],
span: call_span,
},
_ => Value::Nothing { span: call_span },
})
.into_value_stream(),
span: call_span,
})
},
_ => Value::Nothing { span: call_span },
})
.into_pipeline_data())
}
}

View File

@ -4,8 +4,11 @@ use std::env::current_dir;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value, ValueStream};
use nu_protocol::{
IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value, ValueStream,
};
#[derive(Clone)]
pub struct Mkdir;
impl Command for Mkdir {
@ -31,8 +34,8 @@ impl Command for Mkdir {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let path = current_dir()?;
let mut directories = call
.rest::<String>(context, 0)?
@ -67,8 +70,6 @@ impl Command for Mkdir {
}
}
let stream = ValueStream::from_stream(stream.into_iter());
let span = call.head;
Ok(Value::Stream { stream, span })
Ok(stream.into_iter().into_pipeline_data())
}
}

View File

@ -5,8 +5,9 @@ use super::util::get_interactive_confirmation;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Mv;
#[allow(unused_must_use)]
@ -39,8 +40,8 @@ impl Command for Mv {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
// TODO: handle invalid directory or insufficient permissions when moving
let source: String = call.req(context, 0)?;
let destination: String = call.req(context, 1)?;
@ -128,7 +129,7 @@ impl Command for Mv {
move_file(call, &entry, &destination)?
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View File

@ -8,8 +8,11 @@ use super::util::get_interactive_confirmation;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value, ValueStream};
use nu_protocol::{
IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value, ValueStream,
};
#[derive(Clone)]
pub struct Rm;
// Where self.0 is the unexpanded target's positional index (i.e. call.positional[self.0].span)
@ -58,13 +61,13 @@ impl Command for Rm {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
rm(context, call)
}
}
fn rm(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
fn rm(context: &EvaluationContext, call: &Call) -> Result<PipelineData, ShellError> {
let trash = call.has_flag("trash");
let permanent = call.has_flag("permanent");
let interactive = call.has_flag("interactive");
@ -164,11 +167,7 @@ fn rm(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
// let temp = rm_helper(call, args).flatten();
// let temp = input.flatten(call.head, move |_| rm_helper(call, args));
Ok(Value::Stream {
stream: ValueStream::from_stream(response.into_iter()),
span: call.head,
})
Ok(response.into_iter().into_pipeline_data())
// Ok(Value::Nothing { span })
}

View File

@ -3,8 +3,9 @@ use std::fs::OpenOptions;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Touch;
impl Command for Touch {
@ -30,8 +31,8 @@ impl Command for Touch {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let target: String = call.req(context, 0)?;
let rest: Vec<String> = call.rest(context, 1)?;
@ -47,6 +48,6 @@ impl Command for Touch {
}
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}