sometimes you want a text output (#5441)

This commit is contained in:
Darren Schroeder 2022-05-04 14:12:23 -05:00 committed by GitHub
parent 48cf103439
commit 6623ed9061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 0 deletions

View File

@ -265,6 +265,7 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
ToJson,
ToMd,
ToNuon,
ToText,
ToToml,
ToTsv,
ToCsv,

View File

@ -5,6 +5,7 @@ mod html;
mod json;
mod md;
mod nuon;
mod text;
mod toml;
mod tsv;
mod url;
@ -19,6 +20,7 @@ pub use html::ToHtml;
pub use json::ToJson;
pub use md::ToMd;
pub use nuon::ToNuon;
pub use text::ToText;
pub use tsv::ToTsv;
pub use xml::ToXml;
pub use yaml::ToYaml;

View File

@ -0,0 +1,82 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
};
#[derive(Clone)]
pub struct ToText;
impl Command for ToText {
fn name(&self) -> &str {
"to text"
}
fn signature(&self) -> Signature {
Signature::build("to text").category(Category::Formats)
}
fn usage(&self) -> &str {
"Converts data into simple text."
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let span = call.head;
let config = engine_state.get_config();
let line_ending = if cfg!(target_os = "windows") {
"\r\n"
} else {
"\n"
};
let collected_input = input.collect_string(line_ending, config)?;
Ok(Value::String {
val: collected_input,
span,
}
.into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Outputs data as simple text",
example: "1 | to text",
result: Some(Value::test_string("1")),
},
Example {
description: "Outputs external data as simple text",
example: "git help -a | lines | find -r '^ ' | to text",
result: None,
},
Example {
description: "Outputs records as simple text",
example: "ls | to text",
result: None,
},
]
}
fn search_terms(&self) -> Vec<&str> {
vec!["text", "convert"]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(ToText {})
}
}