Repository: nushell/nushell
File: crates/nu-protocol/src/pipeline/pipeline_data.rs (Rust)

Code before:
                        }
                        let range_values: Vec<Value> =
                            val.into_range_iter(span, Signals::empty()).collect();
                        Ok(PipelineData::Value(Value::list(range_values, span), None))
                    }
                    x => Ok(PipelineData::Value(x, metadata)),
                }
            }
            _ => Ok(self),
        }
    }

    /// Consume and print self data immediately, formatted using table command.
    ///
    /// This does not respect the display_output hook. If a value is being printed out by a command,
    /// this function should be used. Otherwise, `nu_cli::util::print_pipeline` should be preferred.
    ///
    /// `no_newline` controls if we need to attach newline character to output.
    /// `to_stderr` controls if data is output to stderr, when the value is false, the data is output to stdout.
    pub fn print_table(
        self,
        engine_state: &EngineState,
        stack: &mut Stack,
        no_newline: bool,
        to_stderr: bool,
        span: Option<Span>,
    ) -> Result<(), ShellError> {
        match self {
            // Print byte streams directly as long as they aren't binary.
            PipelineData::ByteStream(stream, ..) if stream.type_() != ByteStreamType::Binary => {
                stream.print(to_stderr)
            }
            _ => {
                // If the table function is in the declarations, then we can use it
                // to create the table value that will be printed in the terminal
                if let Some(decl_id) = engine_state.table_decl_id {
                    let command = engine_state.get_decl(decl_id);
                    if command.block_id().is_some() {
                        self.write_all_and_flush(engine_state, no_newline, to_stderr, span)
                    } else {
                        let call = Call::new(Span::new(0, 0));
                        let table = command.run(engine_state, stack, &(&call).into(), self)?;
                        table.write_all_and_flush(engine_state, no_newline, to_stderr, span)
                    }
                } else {
                    self.write_all_and_flush(engine_state, no_newline, to_stderr, span)
                }
            }
        }
    }


Review comment (refactor):
I think it might make more sense to use the PipelineData's span for all of these rather than having the caller supply a span. It is nice that we can show the span of the `print` call this way, but I think it's just as clear to point to the actual thing we're trying to print. That would also simplify all the other usages, especially since right now you're passing `None` in those cases.

Code after:
                        }
                        let range_values: Vec<Value> =
                            val.into_range_iter(span, Signals::empty()).collect();
                        Ok(PipelineData::Value(Value::list(range_values, span), None))
                    }
                    x => Ok(PipelineData::Value(x, metadata)),
                }
            }
            _ => Ok(self),
        }
    }

    /// Consume and print self data immediately, formatted using table command.
    ///
    /// This does not respect the display_output hook. If a value is being printed out by a command,
    /// this function should be used. Otherwise, `nu_cli::util::print_pipeline` should be preferred.
    ///
    /// `no_newline` controls if we need to attach newline character to output.
    /// `to_stderr` controls if data is output to stderr, when the value is false, the data is output to stdout.
    pub fn print_table(
        self,
        engine_state: &EngineState,
        stack: &mut Stack,
        no_newline: bool,
        to_stderr: bool,
    ) -> Result<(), ShellError> {
        match self {
            // Print byte streams directly as long as they aren't binary.
            PipelineData::ByteStream(stream, ..) if stream.type_() != ByteStreamType::Binary => {
                stream.print(to_stderr)
            }
            _ => {
                // If the table function is in the declarations, then we can use it
                // to create the table value that will be printed in the terminal
                if let Some(decl_id) = engine_state.table_decl_id {
                    let command = engine_state.get_decl(decl_id);
                    if command.block_id().is_some() {
                        self.write_all_and_flush(engine_state, no_newline, to_stderr)
                    } else {
                        let call = Call::new(Span::new(0, 0));
                        let table = command.run(engine_state, stack, &(&call).into(), self)?;
                        table.write_all_and_flush(engine_state, no_newline, to_stderr)
                    }
                } else {
                    self.write_all_and_flush(engine_state, no_newline, to_stderr)
                }
            }
        }
    }

    /// Consume and print self data without any extra formatting.