diff --git a/crates/nu-cli/src/print.rs b/crates/nu-cli/src/print.rs index 7a9cd137fa..6dc823d7cc 100644 --- a/crates/nu-cli/src/print.rs +++ b/crates/nu-cli/src/print.rs @@ -16,6 +16,11 @@ impl Command for Print { fn signature(&self) -> Signature { Signature::build("print") .rest("rest", SyntaxShape::Any, "the values to print") + .switch( + "no_newline", + "print without inserting a newline for the line ending", + Some('n'), + ) .category(Category::Strings) } @@ -31,10 +36,13 @@ impl Command for Print { _input: PipelineData, ) -> Result { let args: Vec = call.rest(engine_state, stack, 0)?; + let no_newline = call.has_flag("no_newline"); + let head = call.head; for arg in args { - arg.into_pipeline_data().print(engine_state, stack)?; + arg.into_pipeline_data() + .print(engine_state, stack, no_newline)?; } Ok(PipelineData::new(head)) diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index ab599a94bc..5e9f95def2 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -237,7 +237,7 @@ pub fn eval_source( set_last_exit_code(stack, 0); } - if let Err(err) = pipeline_data.print(engine_state, stack) { + if let Err(err) = pipeline_data.print(engine_state, stack, false) { let working_set = StateWorkingSet::new(engine_state); report_error(&working_set, &err); diff --git a/crates/nu-command/src/filesystem/watch.rs b/crates/nu-command/src/filesystem/watch.rs index 8b0daead9c..f8012597b8 100644 --- a/crates/nu-command/src/filesystem/watch.rs +++ b/crates/nu-command/src/filesystem/watch.rs @@ -216,7 +216,7 @@ impl Command for Watch { match eval_result { Ok(val) => { - val.print(engine_state, stack)?; + val.print(engine_state, stack, false)?; } Err(err) => { let working_set = StateWorkingSet::new(engine_state); diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 015da74592..853b675355 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -416,12 +416,16 @@ impl PipelineData { } } - pub fn print(self, engine_state: &EngineState, stack: &mut Stack) -> Result<(), ShellError> { + pub fn print( + self, + engine_state: &EngineState, + stack: &mut Stack, + no_newline: bool, + ) -> Result<(), ShellError> { // 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 let config = engine_state.get_config(); - let stdout = std::io::stdout(); if let PipelineData::ExternalStream { @@ -460,11 +464,15 @@ impl PipelineData { let working_set = StateWorkingSet::new(engine_state); format_error(&working_set, &error) + } else if no_newline { + item.into_string("", config) } else { item.into_string("\n", config) }; - out.push('\n'); + if !no_newline { + out.push('\n'); + } match stdout.lock().write_all(out.as_bytes()) { Ok(_) => (), @@ -479,10 +487,15 @@ impl PipelineData { let working_set = StateWorkingSet::new(engine_state); format_error(&working_set, &error) + } else if no_newline { + item.into_string("", config) } else { item.into_string("\n", config) }; - out.push('\n'); + + if !no_newline { + out.push('\n'); + } match stdout.lock().write_all(out.as_bytes()) { Ok(_) => (),