From e91d8655c6256c6f192c0cb8d1048b71bc2c05cd Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 28 Jan 2022 18:22:09 -0500 Subject: [PATCH] Only trim prompt (#876) * Only trim the output for prompts * Only remove the last newline --- crates/nu-command/src/filesystem/open.rs | 1 - crates/nu-command/src/network/fetch.rs | 1 - crates/nu-command/src/system/run_external.rs | 2 +- crates/nu-command/src/viewers/table.rs | 2 -- crates/nu-protocol/src/pipeline_data.rs | 6 ------ crates/nu-protocol/src/value/stream.rs | 9 --------- src/main.rs | 1 - src/prompt_update.rs | 21 +++++++++++++++++++- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index a32743dbdc..5d53e2d612 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -124,7 +124,6 @@ impl Command for Open { RawStream::new( Box::new(BufferedReader { input: buf_reader }), ctrlc, - false, call_span, ), call_span, diff --git a/crates/nu-command/src/network/fetch.rs b/crates/nu-command/src/network/fetch.rs index 2d74968735..993077a56b 100644 --- a/crates/nu-command/src/network/fetch.rs +++ b/crates/nu-command/src/network/fetch.rs @@ -362,7 +362,6 @@ fn response_to_buffer( input: buffered_input, }), engine_state.ctrlc.clone(), - false, span, ), span, diff --git a/crates/nu-command/src/system/run_external.rs b/crates/nu-command/src/system/run_external.rs index 2b0e937cc2..c8d7afa47b 100644 --- a/crates/nu-command/src/system/run_external.rs +++ b/crates/nu-command/src/system/run_external.rs @@ -243,7 +243,7 @@ impl ExternalCommand { let receiver = ChannelReceiver::new(rx); Ok(PipelineData::RawStream( - RawStream::new(Box::new(receiver), output_ctrlc, true, head), + RawStream::new(Box::new(receiver), output_ctrlc, head), head, None, )) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 4fdbb6ceff..38f181a7f5 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -72,7 +72,6 @@ impl Command for Table { .into_iter(), ), ctrlc, - false, head, ), head, @@ -189,7 +188,6 @@ impl Command for Table { stream, }), ctrlc, - false, head, ), head, diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 456f3767e4..585813b4b6 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -150,8 +150,6 @@ impl PipelineData { PipelineData::RawStream(s, ..) => { let mut items = vec![]; - let trim_end = s.trim_end; - for val in s { match val { Ok(val) => { @@ -173,10 +171,6 @@ impl PipelineData { } } - if trim_end { - output = output.trim_end().to_string(); - } - Ok(output) } } diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index ae82389aa5..7478b7c221 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -12,7 +12,6 @@ pub struct RawStream { pub leftover: Vec, pub ctrlc: Option>, pub is_binary: bool, - pub trim_end: bool, pub span: Span, } @@ -20,7 +19,6 @@ impl RawStream { pub fn new( stream: Box, ShellError>> + Send + 'static>, ctrlc: Option>, - trim_end: bool, span: Span, ) -> Self { Self { @@ -28,7 +26,6 @@ impl RawStream { leftover: vec![], ctrlc, is_binary: false, - trim_end, span, } } @@ -46,16 +43,10 @@ impl RawStream { pub fn into_string(self) -> Result { let mut output = String::new(); - let trim_end = self.trim_end; - for item in self { output.push_str(&item?.as_string()?); } - if trim_end { - output = output.trim_end().to_string(); - } - Ok(output) } } diff --git a/src/main.rs b/src/main.rs index c9ef24e773..8363b8e379 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,7 +123,6 @@ fn main() -> Result<()> { RawStream::new( Box::new(BufferedReader::new(buf_reader)), Some(ctrlc), - true, redirect_stdin.span, ), redirect_stdin.span, diff --git a/src/prompt_update.rs b/src/prompt_update.rs index 6d8832755e..472d10c662 100644 --- a/src/prompt_update.rs +++ b/src/prompt_update.rs @@ -59,6 +59,7 @@ fn get_prompt_string( .and_then(|v| match v { Value::Block { val: block_id, .. } => { let block = engine_state.get_block(block_id); + // Use eval_subexpression to force a redirection of output, so we can use everything in prompt eval_subexpression( engine_state, stack, @@ -70,6 +71,7 @@ fn get_prompt_string( Value::String { val: source, .. } => { let mut working_set = StateWorkingSet::new(engine_state); let (block, _) = parse(&mut working_set, None, source.as_bytes(), true); + // Use eval_subexpression to force a redirection of output, so we can use everything in prompt eval_subexpression( engine_state, stack, @@ -80,7 +82,24 @@ fn get_prompt_string( } _ => None, }) - .and_then(|pipeline_data| pipeline_data.collect_string("", config).ok()) + .and_then(|pipeline_data| { + let output = pipeline_data.collect_string("", config).ok(); + + match output { + Some(mut x) => { + // Just remove the very last newline. + if x.ends_with('\n') { + x.pop(); + } + + if x.ends_with('\r') { + x.pop(); + } + Some(x) + } + None => None, + } + }) } pub(crate) fn update_prompt<'prompt>(