Skip to content

Commit

Permalink
Print reports using shell to handle old Windows colors properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 21, 2021
1 parent 7e2b31a commit a93bfcb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ fn report_future_incompatibilies(config: &Config, args: &ArgMatches<'_>) -> CliR
.unwrap_or_else(|| reports.last_id());
let report = reports.get_report(id, config)?;
drop_println!(config, "{}", REPORT_PREAMBLE);
drop_println!(config, "{}", report);
drop(config.shell().print_ansi_stdout(report.as_bytes()));
Ok(())
}
7 changes: 3 additions & 4 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ use crate::core::compiler::future_incompat::{
};
use crate::core::resolver::ResolveBehavior;
use crate::core::{FeatureValue, PackageId, Shell, TargetKind};
use crate::drop_eprint;
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
use crate::util::interning::InternedString;
use crate::util::machine_message::{self, Message as _};
Expand Down Expand Up @@ -272,7 +271,7 @@ impl<'a> JobState<'a> {
pub fn stderr(&self, stderr: String) -> CargoResult<()> {
if let Some(config) = self.output {
let mut shell = config.shell();
shell.print_ansi(stderr.as_bytes())?;
shell.print_ansi_stderr(stderr.as_bytes())?;
shell.err().write_all(b"\n")?;
} else {
self.messages.push_bounded(Message::Stderr(stderr));
Expand Down Expand Up @@ -561,7 +560,7 @@ impl<'cfg> DrainState<'cfg> {
}
Message::Stderr(err) => {
let mut shell = cx.bcx.config.shell();
shell.print_ansi(err.as_bytes())?;
shell.print_ansi_stderr(err.as_bytes())?;
shell.err().write_all(b"\n")?;
}
Message::FixDiagnostic(msg) => {
Expand Down Expand Up @@ -841,7 +840,7 @@ impl<'cfg> DrainState<'cfg> {

if bcx.build_config.future_incompat_report {
let rendered = on_disk_reports.get_report(report_id, bcx.config).unwrap();
drop_eprint!(bcx.config, "{}", rendered);
drop(bcx.config.shell().print_ansi_stderr(rendered.as_bytes()));
drop(bcx.config.shell().note(&format!(
"this report can be shown with `cargo report \
future-incompatibilities -Z future-incompat-report --id {}`",
Expand Down
20 changes: 18 additions & 2 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ impl Shell {
}
}

/// Prints a message and translates ANSI escape code into console colors.
pub fn print_ansi(&mut self, message: &[u8]) -> CargoResult<()> {
/// Prints a message to stderr and translates ANSI escape code into console colors.
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
if self.needs_clear {
self.err_erase_line();
}
Expand All @@ -339,6 +339,22 @@ impl Shell {
Ok(())
}

/// Prints a message to stdout and translates ANSI escape code into console colors.
pub fn print_ansi_stdout(&mut self, message: &[u8]) -> CargoResult<()> {
if self.needs_clear {
self.err_erase_line();
}
#[cfg(windows)]
{
if let ShellOut::Stream { stdout, .. } = &mut self.output {
::fwdansi::write_ansi(stdout, message)?;
return Ok(());
}
}
self.out().write_all(message)?;
Ok(())
}

pub fn print_json<T: serde::ser::Serialize>(&mut self, obj: &T) -> CargoResult<()> {
// Path may fail to serialize to JSON ...
let encoded = serde_json::to_string(&obj)?;
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/future_incompat_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ fn test_multi_crate() {
}
assert!(count > 0);
}
assert_eq!(lines.next(), Some(""));
assert_eq!(lines.next(), None);
}

Expand Down

0 comments on commit a93bfcb

Please sign in to comment.