Skip to content

Commit

Permalink
Print to file without additional newline (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeDawkins authored Apr 27, 2021
1 parent 361a19a commit 9110438
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/command/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ impl RoverStdout {
}
RoverStdout::Sdl(sdl) => {
print_descriptor("SDL");
println!("{}", &sdl);
print_content(&sdl);
}
RoverStdout::CoreSchema(csdl) => {
print_descriptor("CoreSchema");
println!("{}", &csdl);
print_content(&csdl);
}
RoverStdout::SchemaHash(hash) => {
print_descriptor("Schema Hash");
println!("{}", &hash);
print_content(&hash);
}
RoverStdout::SubgraphList(details) => {
let mut table = table::get_table();
Expand Down Expand Up @@ -105,7 +105,7 @@ impl RoverStdout {
}
RoverStdout::Introspection(introspection_response) => {
print_descriptor("Introspection Response");
println!("{}", &introspection_response);
print_content(&introspection_response);
}
RoverStdout::None => (),
}
Expand All @@ -117,3 +117,14 @@ fn print_descriptor(descriptor: impl Display) {
eprintln!("{}: ", descriptor);
}
}

/// if the user is outputting to a terminal, we want there to be a terminating
/// newline, but we don't want that newline to leak into output that's piped
/// to a file, like from a `graph fetch`
fn print_content(content: impl Display) {
if atty::is(Stream::Stdout) {
println!("{}", content)
} else {
print!("{}", content)
}
}

0 comments on commit 9110438

Please sign in to comment.