Skip to content

Commit

Permalink
feat: add taskbar progress reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon01 committed Dec 17, 2024
1 parent 769f622 commit cbd0508
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 17 deletions.
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
23 changes: 23 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 supports_osc9_4(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
ShellOut::Stream {
progress_report,
stderr_tty,
..
} => *progress_report && *stderr_tty,
}
}

/// 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,15 @@ 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 {
// Windows Terminal session.
std::env::var("WT_SESSION").is_ok()
// Compatibility with ConEmu's ANSI support.
|| std::env::var("ConEmuANSI").ok() == Some("ON".into())
}

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
5 changes: 5 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2747,8 +2747,10 @@ pub struct TermConfig {
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProgressConfig {
#[serde(default)]
pub when: ProgressWhen,
pub width: Option<usize>,
pub taskbar: Option<bool>,
}

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

0 comments on commit cbd0508

Please sign in to comment.