mirror of
https://github.com/nushell/nushell.git
synced 2025-01-20 21:29:04 +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(Select));
|
||||||
working_set.add_decl(Box::new(Split));
|
working_set.add_decl(Box::new(Split));
|
||||||
working_set.add_decl(Box::new(SplitChars));
|
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(Sys));
|
||||||
working_set.add_decl(Box::new(Table));
|
working_set.add_decl(Box::new(Table));
|
||||||
working_set.add_decl(Box::new(Touch));
|
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 {
|
Value::List { vals, span } => Value::List {
|
||||||
vals: vals
|
vals: vals
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(move |v| {
|
.flat_map(|x| split_chars_helper(x, name))
|
||||||
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(),
|
|
||||||
},
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(),
|
.collect(),
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
Value::Stream { stream, span } => Value::Stream {
|
Value::Stream { stream, span } => Value::Stream {
|
||||||
stream: stream
|
stream: stream
|
||||||
.flat_map(move |v| {
|
.flat_map(move |x| split_chars_helper(&x, name))
|
||||||
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(),
|
|
||||||
},
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into_value_stream(),
|
.into_value_stream(),
|
||||||
span,
|
span,
|
||||||
},
|
},
|
||||||
v => {
|
v => {
|
||||||
let v_span = v.span();
|
let v_span = v.span();
|
||||||
if let Ok(s) = v.as_string() {
|
if v.as_string().is_ok() {
|
||||||
Value::List {
|
Value::List {
|
||||||
vals: s
|
vals: split_chars_helper(&v, name),
|
||||||
.chars()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.into_iter()
|
|
||||||
.map(move |x| Value::String {
|
|
||||||
val: x.to_string(),
|
|
||||||
span: v_span,
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
span: v_span,
|
span: v_span,
|
||||||
}
|
}
|
||||||
} else {
|
} 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)]
|
// #[cfg(test)]
|
||||||
// mod tests {
|
// mod tests {
|
||||||
// use super::ShellError;
|
// use super::ShellError;
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
pub mod chars;
|
pub mod chars;
|
||||||
// pub mod column;
|
pub mod column;
|
||||||
pub mod command;
|
pub mod command;
|
||||||
// pub mod row;
|
pub mod row;
|
||||||
|
|
||||||
pub use chars::SubCommand as SplitChars;
|
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 command::SplitCommand as Split;
|
||||||
// pub use row::SubCommand as SplitRow;
|
pub use row::SubCommand as SplitRow;
|
||||||
|
Loading…
Reference in New Issue
Block a user