forked from extern/nushell
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
116 lines
3.1 KiB
Rust
116 lines
3.1 KiB
Rust
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);
|
|
}
|
|
}
|