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

Taskbar progress reporting via ANSI codes #14615

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/cargo/core/compiler/job_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<'gctx> DrainState<'gctx> {
}

fn handle_error(
&self,
&mut self,
shell: &mut Shell,
err_state: &mut ErrorsDuringDrain,
new_err: impl Into<ErrorToHandle>,
Expand All @@ -863,6 +863,7 @@ impl<'gctx> DrainState<'gctx> {
if new_err.print_always || err_state.count == 0 {
crate::display_error(&new_err.error, shell);
if err_state.count == 0 && !self.active.is_empty() {
self.progress.indicate_error();
let _ = shell.warn("build failed, waiting for other jobs to finish...");
}
err_state.count += 1;
Expand Down
29 changes: 29 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Shell {
stderr_tty: std::io::stderr().is_terminal(),
stdout_unicode: supports_unicode(&std::io::stdout()),
stderr_unicode: supports_unicode(&std::io::stderr()),
progress_report: supports_progress_report(),
},
verbosity: Verbosity::Verbose,
needs_clear: false,
Expand Down Expand Up @@ -286,6 +287,17 @@ impl Shell {
}
}

pub fn progress_report_available(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream {
progress_report,
stderr_tty,
..
} => *progress_report && *stderr_tty,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally this check is done in the supports functions, should we make that consistent?

Copy link
Author

@Gordon01 Gordon01 Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking on the other code it seems that it's a separate thing:

  • supports_progress_report() detects the supported terminal from environment variables
  • progress_report_available() (I've renamed is from the supports_osc9_4() for clarity) signals that terminal supports the feature and it's a tty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm following

  • supports_unicode checks !stream.is_terminal() || supports_unicode::supports_unicode()
  • AutoStream::new(std::io::stdout(), stdout_choice) will check is_terminal as well as what the terminal supports

hyperlinks is a bit odd but that is later combined with the AutoStream check

}
}

/// Gets the current color choice.
///
/// If we are not using a color stream, this will always return `Never`, even if the color
Expand Down Expand Up @@ -426,6 +438,8 @@ enum ShellOut {
hyperlinks: bool,
stdout_unicode: bool,
stderr_unicode: bool,
/// Whether the terminal supports progress notifications via OSC 9;4 sequences
progress_report: bool,
},
}

Expand Down Expand Up @@ -565,6 +579,21 @@ fn supports_unicode(stream: &dyn IsTerminal) -> bool {
!stream.is_terminal() || supports_unicode::supports_unicode()
}

/// Detects if the terminal supports OSC 9;4 progress notifications.
#[allow(clippy::disallowed_methods)] // ALLOWED: to read terminal app signature
fn supports_progress_report() -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we rename this (and the variables) to from progress_report to taskbar_progress?

"progress report" is very generic and sounds like its talking about progress indicators in the terminal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From @joshtriplett:

Different terminal emulators handle this differently; for instance, some of the Linux terminal emulators show this in the terminal's tab bar, instead. Rather than having a name that's specific to the rendering of a subset of terminals, could we use a name that's a little more generic?

Windows Terminal also displays progress in the tab. And here I also started to think about more generic term, because it's really more like a progress and status messaging to terminal.

I'm fine to change it back to taskbar_progress because naming is hard anyway.

epage marked this conversation as resolved.
Show resolved Hide resolved
// Windows Terminal session
std::env::var("WT_SESSION").is_ok()
// Compatibility with ConEmu's ANSI support
|| std::env::var("ConEmuANSI").ok() == Some("ON".into())
epage marked this conversation as resolved.
Show resolved Hide resolved
// WezTerm: check if TERM_PROGRAM is "WezTerm" and the version is recent enough
// https://github.com/rust-lang/cargo/pull/14615#issuecomment-2646738819
|| (std::env::var("TERM_PROGRAM").ok() == Some("WezTerm".into())
&& std::env::var("TERM_PROGRAM_VERSION")
.ok()
.map_or(false, |v| v.as_str() >= "20250209-182623-44866cc1"))
Comment on lines +589 to +594
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main reason for version checks is to deal with buggy terminal implementations. If older wezterms gracefully handle these codes, should we skip the version check?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, older versions of wezterm will swallow an OSC they doesn't understand, so you could remove the version number check and just check for wezterm without it. That feels like less magic to keep track of over here.

epage marked this conversation as resolved.
Show resolved Hide resolved
}

fn supports_hyperlinks() -> bool {
#[allow(clippy::disallowed_methods)] // We are reading the state of the system, not config
if std::env::var_os("TERM_PROGRAM").as_deref() == Some(std::ffi::OsStr::new("iTerm.app")) {
Expand Down
6 changes: 6 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2770,8 +2770,11 @@ pub struct TermConfig {
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProgressConfig {
#[serde(default)]
pub when: ProgressWhen,
pub width: Option<usize>,
/// Communicate progress status with terminal
pub report: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
Expand Down Expand Up @@ -2804,10 +2807,12 @@ where
"auto" => Ok(Some(ProgressConfig {
when: ProgressWhen::Auto,
width: None,
report: None,
})),
"never" => Ok(Some(ProgressConfig {
when: ProgressWhen::Never,
width: None,
report: None,
})),
"always" => Err(E::custom("\"always\" progress requires a `width` key")),
_ => Err(E::unknown_variant(s, &["auto", "never"])),
Expand All @@ -2829,6 +2834,7 @@ where
if let ProgressConfig {
when: ProgressWhen::Always,
width: None,
..
} = pc
{
return Err(serde::de::Error::custom(
Expand Down
Loading
Loading