mirror of
https://github.com/nushell/nushell.git
synced 2024-12-17 04:31:21 +01:00
Port split column and split row
This commit is contained in:
parent
44fbf0fce3
commit
60f9fe1aa4
@ -43,6 +43,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
working_set.add_decl(Box::new(Select));
|
||||
working_set.add_decl(Box::new(Split));
|
||||
working_set.add_decl(Box::new(SplitChars));
|
||||
working_set.add_decl(Box::new(SplitColumn));
|
||||
working_set.add_decl(Box::new(SplitRow));
|
||||
working_set.add_decl(Box::new(Sys));
|
||||
working_set.add_decl(Box::new(Table));
|
||||
working_set.add_decl(Box::new(Touch));
|
||||
|
@ -65,69 +65,21 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
|
||||
Value::List { vals, span } => Value::List {
|
||||
vals: vals
|
||||
.iter()
|
||||
.flat_map(move |v| {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let v_span = v.span();
|
||||
s.chars()
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.map(move |x| Value::String {
|
||||
val: x.to_string(),
|
||||
span: v_span,
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![Value::Error {
|
||||
error: ShellError::PipelineMismatch {
|
||||
expected: Type::String,
|
||||
expected_span: name,
|
||||
origin: v.span(),
|
||||
},
|
||||
}]
|
||||
}
|
||||
})
|
||||
.flat_map(|x| split_chars_helper(x, name))
|
||||
.collect(),
|
||||
span,
|
||||
},
|
||||
Value::Stream { stream, span } => Value::Stream {
|
||||
stream: stream
|
||||
.flat_map(move |v| {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let v_span = v.span();
|
||||
s.chars()
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.map(move |x| Value::String {
|
||||
val: x.to_string(),
|
||||
span: v_span,
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![Value::Error {
|
||||
error: ShellError::PipelineMismatch {
|
||||
expected: Type::String,
|
||||
expected_span: name,
|
||||
origin: v.span(),
|
||||
},
|
||||
}]
|
||||
}
|
||||
})
|
||||
.flat_map(move |x| split_chars_helper(&x, name))
|
||||
.into_value_stream(),
|
||||
span,
|
||||
},
|
||||
v => {
|
||||
let v_span = v.span();
|
||||
if let Ok(s) = v.as_string() {
|
||||
if v.as_string().is_ok() {
|
||||
Value::List {
|
||||
vals: s
|
||||
.chars()
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.map(move |x| Value::String {
|
||||
val: x.to_string(),
|
||||
span: v_span,
|
||||
})
|
||||
.collect(),
|
||||
vals: split_chars_helper(&v, name),
|
||||
span: v_span,
|
||||
}
|
||||
} else {
|
||||
@ -143,6 +95,28 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
|
||||
})
|
||||
}
|
||||
|
||||
fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let v_span = v.span();
|
||||
s.chars()
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.map(move |x| Value::String {
|
||||
val: x.to_string(),
|
||||
span: v_span,
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
vec![Value::Error {
|
||||
error: ShellError::PipelineMismatch {
|
||||
expected: Type::String,
|
||||
expected_span: name,
|
||||
origin: v.span(),
|
||||
},
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::ShellError;
|
||||
|
@ -1,9 +1,9 @@
|
||||
pub mod chars;
|
||||
// pub mod column;
|
||||
pub mod column;
|
||||
pub mod command;
|
||||
// pub mod row;
|
||||
pub mod row;
|
||||
|
||||
pub use chars::SubCommand as SplitChars;
|
||||
// pub use column::SubCommand as SplitColumn;
|
||||
pub use column::SubCommand as SplitColumn;
|
||||
pub use command::SplitCommand as Split;
|
||||
// pub use row::SubCommand as SplitRow;
|
||||
pub use row::SubCommand as SplitRow;
|
||||
|
Loading…
Reference in New Issue
Block a user