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

Add simple markdown formatting to rustc --explain output #104540

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3675,6 +3675,7 @@ name = "rustc_errors"
version = "0.0.0"
dependencies = [
"annotate-snippets",
"regex",
"rustc_ast",
"rustc_ast_pretty",
"rustc_data_structures",
Expand Down
41 changes: 34 additions & 7 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc_ast as ast;
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
use rustc_data_structures::sync::SeqCst;
use rustc_errors::markdown;
use rustc_errors::registry::{InvalidErrorCode, Registry};
use rustc_errors::{ErrorGuaranteed, PResult};
use rustc_feature::find_gated_cfg;
Expand Down Expand Up @@ -511,7 +512,7 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
text.push('\n');
}
if io::stdout().is_terminal() {
show_content_with_pager(&text);
show_md_content_with_pager(&text);
} else {
print!("{}", text);
}
Expand All @@ -525,17 +526,38 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
}
}

fn show_content_with_pager(content: &str) {
fn show_md_content_with_pager(content: &str) {
let mut print_color = true;
let mut fallback_to_println = false;

let pager_name = env::var_os("PAGER").unwrap_or_else(|| {
if cfg!(windows) { OsString::from("more.com") } else { OsString::from("less") }
});

let mut fallback_to_println = false;
let mut cmd = Command::new(&pager_name);

// FIXME: find if other pagers accept color options
if pager_name == "less" {
cmd.arg("-r");
} else {
print_color = false;
};

match Command::new(pager_name).stdin(Stdio::piped()).spawn() {
let md_ast = markdown::create_ast(content);
let bufwtr = markdown::create_stdout_bufwtr();
let mut buffer = bufwtr.buffer();
md_ast.write_termcolor_buf(&mut buffer);

match cmd.stdin(Stdio::piped()).spawn() {
Ok(mut pager) => {
if let Some(pipe) = pager.stdin.as_mut() {
if pipe.write_all(content.as_bytes()).is_err() {
let res = if print_color {
pipe.write_all(buffer.as_slice())
} else {
pipe.write_all(content.as_bytes())
};

if res.is_err() {
fallback_to_println = true;
}
}
Expand All @@ -551,8 +573,13 @@ fn show_content_with_pager(content: &str) {

// If pager fails for whatever reason, we should still print the content
// to standard output
if fallback_to_println {
print!("{}", content);
if fallback_to_println && print_color {
// If we fail to print the buffer, we'll just fall back to println
print_color = bufwtr.print(&buffer).is_ok();
}

if fallback_to_println && !print_color {
println!("{content}");
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ annotate-snippets = "0.9"
termize = "0.1.1"
serde = { version = "1.0.125", features = [ "derive" ] }
serde_json = "1.0.59"
regex = "1.5"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = [ "handleapi", "synchapi", "winbase" ] }
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(adt_const_params)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(once_cell)]
#![feature(result_option_inspect)]
#![feature(rustc_attrs)]
#![allow(incomplete_features)]
Expand Down Expand Up @@ -56,6 +57,7 @@ mod diagnostic_impls;
pub mod emitter;
pub mod json;
mod lock;
pub mod markdown;
pub mod registry;
mod snippet;
mod styled_buffer;
Expand Down
Loading