Repository: nushell/nushell
File: crates/nu-command/src/formats/to/text.rs (Rust)

Code before:
            )
            .category(Category::Formats)
    }

    fn description(&self) -> &str {
        "Converts data into simple text."
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let span = call.head;
        let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
        let input = input.try_expand_range()?;
        let config = stack.get_config(engine_state);

        match input {
            PipelineData::Empty => Ok(Value::string(String::new(), span)
                .into_pipeline_data_with_metadata(update_metadata(None))),
            PipelineData::Value(value, ..) => {
                let mut str = local_into_string(value, LINE_ENDING, &config);
                if !no_newline {
                    str.push_str(LINE_ENDING);
                }

                Ok(
                    Value::string(str, span)
                        .into_pipeline_data_with_metadata(update_metadata(None)),
                )
            }
            PipelineData::ListStream(stream, meta) => {
                let span = stream.span();
                let iter = stream.into_inner().map(move |value| {
                    let mut str = local_into_string(value, LINE_ENDING, &config);
                    if !no_newline {
                        str.push_str(LINE_ENDING);
                    }
                    str
                });
                Ok(PipelineData::ByteStream(
                    ByteStream::from_iter(
                        iter,
                        span,
                        engine_state.signals().clone(),
                        ByteStreamType::String,
                    ),
                    update_metadata(meta),

Review comment (suggestion):
Maybe a new line should also only be added here if the value was a list or record. This way, `([('a' | to text), ('b' | to text)] | to text) == ([a b] | to text)`

Code after:
            )
            .category(Category::Formats)
    }

    fn description(&self) -> &str {
        "Converts data into simple text."
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let span = call.head;
        let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
        let input = input.try_expand_range()?;
        let config = stack.get_config(engine_state);

        match input {
            PipelineData::Empty => Ok(Value::string(String::new(), span)
                .into_pipeline_data_with_metadata(update_metadata(None))),
            PipelineData::Value(value, ..) => {
                let is_compound_type = matches!(value, Value::List { .. } | Value::Record { .. });
                let mut str = local_into_string(value, LINE_ENDING, &config);
                if is_compound_type && !no_newline {
                    str.push_str(LINE_ENDING);
                }

                Ok(
                    Value::string(str, span)
                        .into_pipeline_data_with_metadata(update_metadata(None)),
                )
            }
            PipelineData::ListStream(stream, meta) => {
                let span = stream.span();
                let iter = stream.into_inner().map(move |value| {
                    let mut str = local_into_string(value, LINE_ENDING, &config);
                    if !no_newline {
                        str.push_str(LINE_ENDING);
                    }
                    str
                });
                Ok(PipelineData::ByteStream(
                    ByteStream::from_iter(
                        iter,
                        span,
                        engine_state.signals().clone(),
                        ByteStreamType::String,
                    ),