Skip to content

Commit d4a3272

Browse files
committed
review comments
1 parent d1e8e58 commit d4a3272

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub use self::layout::is_bad_artifact_name;
4040
use self::output_depinfo::output_depinfo;
4141
use self::unit_dependencies::UnitDep;
4242
pub use crate::core::compiler::unit::{Unit, UnitInterner};
43+
use crate::core::features::nightly_features_allowed;
4344
use crate::core::manifest::TargetSourcePath;
4445
use crate::core::profiles::{Lto, PanicStrategy, Profile};
4546
use crate::core::Feature;
@@ -714,6 +715,14 @@ fn add_error_format_and_color(
714715
} else {
715716
let mut color = true;
716717
match cx.bcx.build_config.message_format {
718+
MessageFormat::Human if nightly_features_allowed() => {
719+
if let (Some(width), _) | (_, Some(width)) = (
720+
cx.bcx.config.cli_unstable().terminal_width,
721+
cx.bcx.config.shell().accurate_err_width(),
722+
) {
723+
cmd.arg(format!("-Zterminal-width={}", width));
724+
}
725+
}
717726
MessageFormat::Human => (),
718727
MessageFormat::Json {
719728
ansi,

src/cargo/core/features.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ pub struct CliUnstable {
337337
pub build_std: Option<Vec<String>>,
338338
pub timings: Option<Vec<String>>,
339339
pub doctest_xcompile: bool,
340+
pub terminal_width: Option<usize>,
340341
}
341342

342343
impl CliUnstable {
@@ -376,6 +377,16 @@ impl CliUnstable {
376377
}
377378
}
378379

380+
fn parse_usize_opt(value: Option<&str>) -> CargoResult<Option<usize>> {
381+
Ok(match value {
382+
Some(value) => match value.parse::<usize>() {
383+
Ok(value) => Some(value),
384+
Err(e) => failure::bail!("expected a number, found: {}", e),
385+
},
386+
None => None,
387+
})
388+
}
389+
379390
match k {
380391
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
381392
"unstable-options" => self.unstable_options = true,
@@ -395,6 +406,7 @@ impl CliUnstable {
395406
}
396407
"timings" => self.timings = Some(parse_timings(v)),
397408
"doctest-xcompile" => self.doctest_xcompile = true,
409+
"terminal-width" => self.terminal_width = parse_usize_opt(v)?,
398410
_ => failure::bail!("unknown `-Z` flag specified: {}", k),
399411
}
400412

src/cargo/core/shell.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ impl Shell {
136136
}
137137
}
138138

139+
/// Returns the width of the terminal in spaces, if any. Always `None` in Windows.
140+
pub fn accurate_err_width(&self) -> Option<usize> {
141+
match self.err {
142+
ShellOut::Stream { tty: true, .. } => imp::accurate_stderr_width(),
143+
_ => None,
144+
}
145+
}
146+
139147
/// Returns `true` if stderr is a tty.
140148
pub fn is_err_tty(&self) -> bool {
141149
match self.err {
@@ -383,6 +391,10 @@ mod imp {
383391

384392
use super::Shell;
385393

394+
pub fn accurate_stderr_width() -> Option<usize> {
395+
stderr_width()
396+
}
397+
386398
pub fn stderr_width() -> Option<usize> {
387399
unsafe {
388400
let mut winsize: libc::winsize = mem::zeroed();
@@ -414,6 +426,10 @@ mod imp {
414426
mod imp {
415427
pub(super) use super::default_err_erase_line as err_erase_line;
416428

429+
pub fn accurate_stderr_width() -> Option<usize> {
430+
None
431+
}
432+
417433
pub fn stderr_width() -> Option<usize> {
418434
None
419435
}
@@ -431,6 +447,10 @@ mod imp {
431447

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

450+
pub fn accurate_stderr_width() -> Option<usize> {
451+
None
452+
}
453+
434454
pub fn stderr_width() -> Option<usize> {
435455
unsafe {
436456
let stdout = GetStdHandle(STD_ERROR_HANDLE);

src/cargo/ops/cargo_compile.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,6 @@ pub fn compile_ws<'a>(
426426
}
427427
}
428428
}
429-
if let Some(width) = config.shell().err_width() {
430-
for unit in &units {
431-
bcx.extra_compiler_args
432-
.insert(*unit, vec![format!("-Zterminal-width={}", width)]);
433-
}
434-
}
435429

436430
let unit_dependencies = build_unit_dependencies(
437431
&bcx,

tests/testsuite/build.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use cargo::util::paths::dylib_path_envvar;
22
use cargo_test_support::paths::{root, CargoPathExt};
33
use cargo_test_support::registry::Package;
44
use cargo_test_support::{
5-
basic_bin_manifest, basic_lib_manifest, basic_manifest, main_file, project, rustc_host,
6-
sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
5+
basic_bin_manifest, basic_lib_manifest, basic_manifest, is_nightly, main_file, project,
6+
rustc_host, sleep_ms, symlink_supported, t, Execs, ProjectBuilder,
77
};
88
use std::env;
99
use std::fs::{self, File};
@@ -4723,3 +4723,34 @@ d
47234723
)
47244724
.run();
47254725
}
4726+
4727+
#[cargo_test]
4728+
fn simple_terminal_width() {
4729+
if !is_nightly() {
4730+
// --terminal-width is unstable
4731+
return;
4732+
}
4733+
let p = project()
4734+
.file(
4735+
"src/lib.rs",
4736+
"
4737+
fn a() {
4738+
/**/ /**/ \
4739+
/**/ /**/ \
4740+
/**/ /**/ \
4741+
/**/ /**/ \
4742+
/**/ /**/ \
4743+
/**/ /**/ \
4744+
/**/ /**/ \
4745+
let _: () = 42;
4746+
}
4747+
",
4748+
)
4749+
.build();
4750+
4751+
p.cargo("build -Zterminal-width=60")
4752+
.masquerade_as_nightly_cargo()
4753+
.with_status(101)
4754+
.with_stderr_contains("3 | ...t _: () = 42;")
4755+
.run();
4756+
}

0 commit comments

Comments
 (0)