Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: redirect prompt messages to stderr #4536

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,35 +560,34 @@ 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<String> {
if !Self::can_prompt() {
torquestomp marked this conversation as resolved.
Show resolved Hide resolved
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"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)
}

Expand All @@ -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());
}
}
Expand Down
18 changes: 6 additions & 12 deletions cli/tests/test_next_prev_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"###);
}

Expand All @@ -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
"###);
}
Expand Down Expand Up @@ -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)
"###);
}
Expand Down Expand Up @@ -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
"###);

Expand All @@ -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
"###);
Expand Down Expand Up @@ -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
"###);

Expand Down