diff --git a/cli/src/ui.rs b/cli/src/ui.rs index 5b5b85d87f..b57bf4d904 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -560,14 +560,12 @@ impl Ui { } pub fn can_prompt() -> bool { - io::stdout().is_terminal() + io::stderr().is_terminal() || env::var("JJ_INTERACTIVE") .map(|v| v == "1") .unwrap_or(false) } - #[allow(unknown_lints)] // XXX FIXME (aseipp): nightly bogons; re-test this occasionally - #[allow(clippy::assigning_clones)] pub fn prompt(&self, prompt: &str) -> io::Result { if !Self::can_prompt() { return Err(io::Error::new( @@ -575,20 +573,21 @@ impl Ui { "Cannot prompt for input since the output is not connected to a terminal", )); } - write!(self.stdout(), "{prompt}: ")?; - self.stdout().flush()?; + write!(self.stderr(), "{prompt}: ")?; + self.stderr().flush()?; let mut buf = String::new(); io::stdin().read_line(&mut buf)?; - if let Some(trimmed) = buf.strip_suffix('\n') { - buf = trimmed.to_owned(); - } else if buf.is_empty() { + if buf.is_empty() { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, "Prompt cancelled by EOF", )); } + if let Some(trimmed) = buf.strip_suffix('\n') { + buf.truncate(trimmed.len()); + } Ok(buf) } @@ -602,7 +601,7 @@ impl Ui { if !Self::can_prompt() { if let Some(default) = default { // Choose the default automatically without waiting. - writeln!(self.stdout(), "{prompt}: {default}")?; + writeln!(self.stderr(), "{prompt}: {default}")?; return Ok(default.to_owned()); } } diff --git a/cli/tests/test_next_prev_commands.rs b/cli/tests/test_next_prev_commands.rs index cda696f985..5c6555ea1e 100644 --- a/cli/tests/test_next_prev_commands.rs +++ b/cli/tests/test_next_prev_commands.rs @@ -393,10 +393,9 @@ fn test_next_fails_on_bookmarking_children_quit_prompt() { 1: zsuskuln 5f24490d (empty) third 2: rlvkpnrz 9ed53a4a (empty) second q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Error: ambiguous target commit + enter the index of the commit you want to target: Error: ambiguous target commit "###); } @@ -420,10 +419,9 @@ fn test_next_choose_bookmarking_child() { 2: zsuskuln 5f24490d (empty) third 3: rlvkpnrz 9ed53a4a (empty) second q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Working copy now at: yostqsxw 5c8fa96d (empty) (no description set) + enter the index of the commit you want to target: Working copy now at: yostqsxw 5c8fa96d (empty) (no description set) Parent commit : zsuskuln 5f24490d (empty) third "###); } @@ -463,10 +461,9 @@ fn test_prev_on_merge_commit() { 1: zsuskuln b0d21db3 right | (empty) second 2: qpvuntsm fa15625b left | (empty) first q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Working copy now at: qpvuntsm fa15625b left | (empty) first + enter the index of the commit you want to target: Working copy now at: qpvuntsm fa15625b left | (empty) first Parent commit : zzzzzzzz 00000000 (empty) (no description set) "###); } @@ -509,10 +506,9 @@ fn test_prev_on_merge_commit_with_parent_merge() { 2: qpvuntsm 6799aaa2 (empty) x 3: zzzzzzzz 00000000 (empty) (no description set) q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Working copy now at: vruxwmqv e5a6794c (empty) (no description set) + enter the index of the commit you want to target: Working copy now at: vruxwmqv e5a6794c (empty) (no description set) Parent commit : qpvuntsm 6799aaa2 (empty) x "###); @@ -523,10 +519,9 @@ fn test_prev_on_merge_commit_with_parent_merge() { 1: mzvwutvl 89b8a355 (empty) 1 2: zsuskuln a83fc061 (empty) z q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Working copy now at: zsuskuln a83fc061 (empty) z + enter the index of the commit you want to target: Working copy now at: zsuskuln a83fc061 (empty) z Parent commit : qpvuntsm 6799aaa2 (empty) x Parent commit : kkmpptxz 146d5c67 (empty) y "###); @@ -569,10 +564,9 @@ fn test_prev_prompts_on_multiple_parents() { 2: kkmpptxz b0d21db3 (empty) second 3: qpvuntsm fa15625b (empty) first q: quit the prompt - enter the index of the commit you want to target: "###); insta::assert_snapshot!(stderr,@r###" - Working copy now at: kpqxywon ddac00b0 (empty) (no description set) + enter the index of the commit you want to target: Working copy now at: kpqxywon ddac00b0 (empty) (no description set) Parent commit : qpvuntsm fa15625b (empty) first "###);