mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
str set sub command removal. (#2940)
This commit is contained in:
parent
ebc4694e05
commit
d8ed01400f
@ -260,8 +260,8 @@ pub(crate) use split_by::SplitBy;
|
|||||||
pub(crate) use str_::{
|
pub(crate) use str_::{
|
||||||
Str, StrCamelCase, StrCapitalize, StrCollect, StrContains, StrDowncase, StrEndsWith,
|
Str, StrCamelCase, StrCapitalize, StrCollect, StrContains, StrDowncase, StrEndsWith,
|
||||||
StrFindReplace, StrFrom, StrIndexOf, StrKebabCase, StrLPad, StrLength, StrPascalCase, StrRPad,
|
StrFindReplace, StrFrom, StrIndexOf, StrKebabCase, StrLPad, StrLength, StrPascalCase, StrRPad,
|
||||||
StrReverse, StrScreamingSnakeCase, StrSet, StrSnakeCase, StrStartsWith, StrSubstring,
|
StrReverse, StrScreamingSnakeCase, StrSnakeCase, StrStartsWith, StrSubstring, StrToDatetime,
|
||||||
StrToDatetime, StrToDecimal, StrToInteger, StrTrim, StrTrimLeft, StrTrimRight, StrUpcase,
|
StrToDecimal, StrToInteger, StrTrim, StrTrimLeft, StrTrimRight, StrUpcase,
|
||||||
};
|
};
|
||||||
pub(crate) use table::Table;
|
pub(crate) use table::Table;
|
||||||
pub(crate) use tags::Tags;
|
pub(crate) use tags::Tags;
|
||||||
|
@ -91,7 +91,6 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
|
|||||||
whole_stream_command(StrFindReplace),
|
whole_stream_command(StrFindReplace),
|
||||||
whole_stream_command(StrFrom),
|
whole_stream_command(StrFrom),
|
||||||
whole_stream_command(StrSubstring),
|
whole_stream_command(StrSubstring),
|
||||||
whole_stream_command(StrSet),
|
|
||||||
whole_stream_command(StrToDatetime),
|
whole_stream_command(StrToDatetime),
|
||||||
whole_stream_command(StrContains),
|
whole_stream_command(StrContains),
|
||||||
whole_stream_command(StrIndexOf),
|
whole_stream_command(StrIndexOf),
|
||||||
|
@ -12,7 +12,6 @@ mod length;
|
|||||||
mod lpad;
|
mod lpad;
|
||||||
mod reverse;
|
mod reverse;
|
||||||
mod rpad;
|
mod rpad;
|
||||||
mod set;
|
|
||||||
mod starts_with;
|
mod starts_with;
|
||||||
mod substring;
|
mod substring;
|
||||||
mod to_datetime;
|
mod to_datetime;
|
||||||
@ -39,7 +38,6 @@ pub use length::SubCommand as StrLength;
|
|||||||
pub use lpad::SubCommand as StrLPad;
|
pub use lpad::SubCommand as StrLPad;
|
||||||
pub use reverse::SubCommand as StrReverse;
|
pub use reverse::SubCommand as StrReverse;
|
||||||
pub use rpad::SubCommand as StrRPad;
|
pub use rpad::SubCommand as StrRPad;
|
||||||
pub use set::SubCommand as StrSet;
|
|
||||||
pub use starts_with::SubCommand as StrStartsWith;
|
pub use starts_with::SubCommand as StrStartsWith;
|
||||||
pub use substring::SubCommand as StrSubstring;
|
pub use substring::SubCommand as StrSubstring;
|
||||||
pub use to_datetime::SubCommand as StrToDatetime;
|
pub use to_datetime::SubCommand as StrToDatetime;
|
||||||
|
@ -1,115 +0,0 @@
|
|||||||
use crate::prelude::*;
|
|
||||||
use nu_engine::WholeStreamCommand;
|
|
||||||
use nu_errors::ShellError;
|
|
||||||
use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
||||||
use nu_source::{Tag, Tagged};
|
|
||||||
use nu_value_ext::ValueExt;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Arguments {
|
|
||||||
replace: Tagged<String>,
|
|
||||||
rest: Vec<ColumnPath>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SubCommand;
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl WholeStreamCommand for SubCommand {
|
|
||||||
fn name(&self) -> &str {
|
|
||||||
"str set"
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
|
||||||
Signature::build("str set")
|
|
||||||
.required("set", SyntaxShape::String, "the new string to set")
|
|
||||||
.rest(
|
|
||||||
SyntaxShape::ColumnPath,
|
|
||||||
"optionally set text by column paths",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
|
||||||
"sets text"
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
||||||
operate(args).await
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
|
||||||
vec![
|
|
||||||
Example {
|
|
||||||
description: "Set contents with preferred string",
|
|
||||||
example: "echo 'good day' | str set 'good bye'",
|
|
||||||
result: Some(vec![Value::from("good bye")]),
|
|
||||||
},
|
|
||||||
Example {
|
|
||||||
description: "Set the contents on preferred column paths",
|
|
||||||
example: "open Cargo.toml | str set '255' package.version",
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct Replace(String);
|
|
||||||
|
|
||||||
async fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
||||||
let (Arguments { replace, rest }, input) = args.process().await?;
|
|
||||||
let options = Replace(replace.item);
|
|
||||||
|
|
||||||
let column_paths: Vec<_> = rest;
|
|
||||||
|
|
||||||
Ok(input
|
|
||||||
.map(move |v| {
|
|
||||||
if column_paths.is_empty() {
|
|
||||||
ReturnSuccess::value(action(&v, &options, v.tag())?)
|
|
||||||
} else {
|
|
||||||
let mut ret = v;
|
|
||||||
|
|
||||||
for path in &column_paths {
|
|
||||||
let options = options.clone();
|
|
||||||
|
|
||||||
ret = ret.swap_data_by_column_path(
|
|
||||||
path,
|
|
||||||
Box::new(move |old| action(old, &options, old.tag())),
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReturnSuccess::value(ret)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.to_output_stream())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn action(_input: &Value, options: &Replace, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
|
||||||
let replacement = &options.0;
|
|
||||||
Ok(UntaggedValue::string(replacement.as_str()).into_value(tag))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::ShellError;
|
|
||||||
use super::{action, Replace, SubCommand};
|
|
||||||
use nu_source::Tag;
|
|
||||||
use nu_test_support::value::string;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
||||||
use crate::examples::test as test_examples;
|
|
||||||
|
|
||||||
Ok(test_examples(SubCommand {})?)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sets() {
|
|
||||||
let word = string("andres");
|
|
||||||
let expected = string("robalino");
|
|
||||||
|
|
||||||
let set_options = Replace(String::from("robalino"));
|
|
||||||
|
|
||||||
let actual = action(&word, &set_options, Tag::unknown()).unwrap();
|
|
||||||
assert_eq!(actual, expected);
|
|
||||||
}
|
|
||||||
}
|
|
@ -150,30 +150,6 @@ fn converts_to_decimal() {
|
|||||||
assert_eq!(actual.out, "3.1415");
|
assert_eq!(actual.out, "3.1415");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sets() {
|
|
||||||
Playground::setup("str_test_5", |dirs, sandbox| {
|
|
||||||
sandbox.with_files(vec![FileWithContent(
|
|
||||||
"sample.toml",
|
|
||||||
r#"
|
|
||||||
[package]
|
|
||||||
name = "nushell"
|
|
||||||
"#,
|
|
||||||
)]);
|
|
||||||
|
|
||||||
let actual = nu!(
|
|
||||||
cwd: dirs.test(), pipeline(
|
|
||||||
r#"
|
|
||||||
open sample.toml
|
|
||||||
| str set wykittenshell package.name
|
|
||||||
| get package.name
|
|
||||||
"#
|
|
||||||
));
|
|
||||||
|
|
||||||
assert_eq!(actual.out, "wykittenshell");
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn find_and_replaces() {
|
fn find_and_replaces() {
|
||||||
Playground::setup("str_test_6", |dirs, sandbox| {
|
Playground::setup("str_test_6", |dirs, sandbox| {
|
||||||
|
Loading…
Reference in New Issue
Block a user