Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Sep 22, 2019
1 parent d1e8e58 commit d4a3272
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub use self::layout::is_bad_artifact_name;
use self::output_depinfo::output_depinfo;
use self::unit_dependencies::UnitDep;
pub use crate::core::compiler::unit::{Unit, UnitInterner};
use crate::core::features::nightly_features_allowed;
use crate::core::manifest::TargetSourcePath;
use crate::core::profiles::{Lto, PanicStrategy, Profile};
use crate::core::Feature;
Expand Down Expand Up @@ -714,6 +715,14 @@ fn add_error_format_and_color(
} else {
let mut color = true;
match cx.bcx.build_config.message_format {
MessageFormat::Human if nightly_features_allowed() => {
if let (Some(width), _) | (_, Some(width)) = (
cx.bcx.config.cli_unstable().terminal_width,
cx.bcx.config.shell().accurate_err_width(),
) {
cmd.arg(format!("-Zterminal-width={}", width));
}
}
MessageFormat::Human => (),
MessageFormat::Json {
ansi,
Expand Down
12 changes: 12 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ pub struct CliUnstable {
pub build_std: Option<Vec<String>>,
pub timings: Option<Vec<String>>,
pub doctest_xcompile: bool,
pub terminal_width: Option<usize>,
}

impl CliUnstable {
Expand Down Expand Up @@ -376,6 +377,16 @@ impl CliUnstable {
}
}

fn parse_usize_opt(value: Option<&str>) -> CargoResult<Option<usize>> {
Ok(match value {
Some(value) => match value.parse::<usize>() {
Ok(value) => Some(value),
Err(e) => failure::bail!("expected a number, found: {}", e),
},
None => None,
})
}

match k {
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
"unstable-options" => self.unstable_options = true,
Expand All @@ -395,6 +406,7 @@ impl CliUnstable {
}
"timings" => self.timings = Some(parse_timings(v)),
"doctest-xcompile" => self.doctest_xcompile = true,
"terminal-width" => self.terminal_width = parse_usize_opt(v)?,
_ => failure::bail!("unknown `-Z` flag specified: {}", k),
}

Expand Down
20 changes: 20 additions & 0 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ impl Shell {
}
}

/// Returns the width of the terminal in spaces, if any. Always `None` in Windows.
pub fn accurate_err_width(&self) -> Option<usize> {
match self.err {
ShellOut::Stream { tty: true, .. } => imp::accurate_stderr_width(),
_ => None,
}
}

/// Returns `true` if stderr is a tty.
pub fn is_err_tty(&self) -> bool {
match self.err {
Expand Down Expand Up @@ -383,6 +391,10 @@ mod imp {

use super::Shell;

pub fn accurate_stderr_width() -> Option<usize> {
stderr_width()
}

pub fn stderr_width() -> Option<usize> {
unsafe {
let mut winsize: libc::winsize = mem::zeroed();
Expand Down Expand Up @@ -414,6 +426,10 @@ mod imp {
mod imp {
pub(super) use super::default_err_erase_line as err_erase_line;

pub fn accurate_stderr_width() -> Option<usize> {
None
}

pub fn stderr_width() -> Option<usize> {
None
}
Expand All @@ -431,6 +447,10 @@ mod imp {

pub(super) use super::default_err_erase_line as err_erase_line;

pub fn accurate_stderr_width() -> Option<usize> {
None
}

pub fn stderr_width() -> Option<usize> {
unsafe {
let stdout = GetStdHandle(STD_ERROR_HANDLE);
Expand Down
6 changes: 0 additions & 6 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,6 @@ pub fn compile_ws<'a>(
}
}
}
if let Some(width) = config.shell().err_width() {
for unit in &units {
bcx.extra_compiler_args
.insert(*unit, vec![format!("-Zterminal-width={}", width)]);
}
}

let unit_dependencies = build_unit_dependencies(
&bcx,
Expand Down
35 changes: 33 additions & 2 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use cargo::util::paths::dylib_path_envvar;
use cargo_test_support::paths::{root, CargoPathExt};
use cargo_test_support::registry::Package;
use cargo_test_support::{
basic_bin_manifest, basic_lib_manifest, basic_manifest, main_file, project, rustc_host,
sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, main_file, project,
rustc_host, sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
};
use std::env;
use std::fs::{self, File};
Expand Down Expand Up @@ -4723,3 +4723,34 @@ d
)
.run();
}

#[cargo_test]
fn simple_terminal_width() {
if !is_nightly() {
// --terminal-width is unstable
return;
}
let p = project()
.file(
"src/lib.rs",
"
fn a() {
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
/**/ /**/ \
let _: () = 42;
}
",
)
.build();

p.cargo("build -Zterminal-width=60")
.masquerade_as_nightly_cargo()
.with_status(101)
.with_stderr_contains("3 | ...t _: () = 42;")
.run();
}

0 comments on commit d4a3272

Please sign in to comment.