From ded70aafc81919403a8f13cbd4c1b38520e8189e Mon Sep 17 00:00:00 2001 From: dploch Date: Wed, 25 Sep 2024 11:57:55 -0400 Subject: [PATCH 1/2] ui: simplify stripping logic and get rid of warning --- cli/src/ui.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cli/src/ui.rs b/cli/src/ui.rs index 5b5b85d87f..e70a8bb1b5 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -566,8 +566,6 @@ impl Ui { .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( @@ -580,15 +578,16 @@ impl Ui { 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) } From da663acccddef99d3b3978241564af7e6c988273 Mon Sep 17 00:00:00 2001 From: dploch Date: Mon, 7 Oct 2024 11:28:22 -0400 Subject: [PATCH 2/2] ui: write prompt messages to stderr This enables more scripting use cases where the caller may wish to parse stdout for programmatic purposes. --- cli/src/ui.rs | 8 ++++---- cli/tests/test_next_prev_commands.rs | 18 ++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/cli/src/ui.rs b/cli/src/ui.rs index e70a8bb1b5..b57bf4d904 100644 --- a/cli/src/ui.rs +++ b/cli/src/ui.rs @@ -560,7 +560,7 @@ 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) @@ -573,8 +573,8 @@ 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)?; @@ -601,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 "###);