diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 7d72d36c0..bebb4ceca 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -265,6 +265,7 @@ pub fn create_default_context(cwd: impl AsRef) -> EngineState { ToJson, ToMd, ToNuon, + ToText, ToToml, ToTsv, ToCsv, diff --git a/crates/nu-command/src/formats/to/mod.rs b/crates/nu-command/src/formats/to/mod.rs index bc693e481..5397970ed 100644 --- a/crates/nu-command/src/formats/to/mod.rs +++ b/crates/nu-command/src/formats/to/mod.rs @@ -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; diff --git a/crates/nu-command/src/formats/to/text.rs b/crates/nu-command/src/formats/to/text.rs new file mode 100644 index 000000000..6233fdec2 --- /dev/null +++ b/crates/nu-command/src/formats/to/text.rs @@ -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 { + 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 { + 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 {}) + } +}