Avoid extra newlines in output (#178)

Ensure that no extra newlines are printed in the output.
This commit is contained in:
Paweł Romanowski 2021-03-14 19:59:52 +01:00 committed by GitHub
parent 5a775b5801
commit b4de8e0983
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 16 deletions

View file

@ -175,7 +175,12 @@ async fn run(cfg: &Config, inputs: Vec<Input>) -> Result<i32> {
if let Some(output) = &cfg.output {
fs::write(output, stats_formatted).context("Cannot write status output to file")?;
} else {
println!("\n{}", stats_formatted);
if cfg.verbose && !stats.is_empty() {
// separate summary from the verbose list of links above
println!();
}
// we assume that the formatted stats don't have a final newline
println!("{}", stats_formatted);
}
match stats.is_success() {

View file

@ -74,9 +74,13 @@ impl ResponseStats {
pub fn is_success(&self) -> bool {
self.total == self.successful + self.excludes
}
pub fn is_empty(&self) -> bool {
self.total == 0
}
}
fn write_stat(f: &mut fmt::Formatter, title: &str, stat: usize) -> fmt::Result {
fn write_stat(f: &mut fmt::Formatter, title: &str, stat: usize, newline: bool) -> fmt::Result {
let fill = title.chars().count();
f.write_str(title)?;
f.write_str(
@ -84,7 +88,12 @@ fn write_stat(f: &mut fmt::Formatter, title: &str, stat: usize) -> fmt::Result {
.to_string()
.pad(MAX_PADDING - fill, '.', Alignment::Right, false),
)?;
f.write_str("\n")
if newline {
f.write_str("\n")?;
}
Ok(())
}
impl Display for ResponseStats {
@ -93,24 +102,23 @@ impl Display for ResponseStats {
writeln!(f, "📝 Summary")?;
writeln!(f, "{}", separator)?;
write_stat(f, "🔍 Total", self.total)?;
write_stat(f, "✅ Successful", self.successful)?;
write_stat(f, "⏳ Timeouts", self.timeouts)?;
write_stat(f, "🔀 Redirected", self.redirects)?;
write_stat(f, "👻 Excluded", self.excludes)?;
write_stat(f, "🚫 Errors", self.errors + self.failures)?;
write_stat(f, "🔍 Total", self.total, true)?;
write_stat(f, "✅ Successful", self.successful, true)?;
write_stat(f, "⏳ Timeouts", self.timeouts, true)?;
write_stat(f, "🔀 Redirected", self.redirects, true)?;
write_stat(f, "👻 Excluded", self.excludes, true)?;
write_stat(f, "🚫 Errors", self.errors + self.failures, false)?;
if !&self.fail_map.is_empty() {
writeln!(f)?;
}
for (input, responses) in &self.fail_map {
writeln!(f, "Errors in {}", input)?;
// Using leading newlines over trailing ones (e.g. `writeln!`)
// lets us avoid extra newlines without any additional logic.
write!(f, "\n\nErrors in {}", input)?;
for response in responses {
writeln!(f, "{}", color_response(response))?
write!(f, "\n{}", color_response(response))?
}
writeln!(f)?;
}
writeln!(f)
Ok(())
}
}
@ -120,6 +128,20 @@ mod test_super {
use super::*;
#[test]
fn test_stats_is_empty() {
let mut stats = ResponseStats::new();
assert!(stats.is_empty());
stats.add(Response {
uri: website("http://example.org/ok"),
status: Status::Ok(http::StatusCode::OK),
source: Input::Stdin,
});
assert!(!stats.is_empty());
}
#[test]
fn test_stats() {
let mut stats = ResponseStats::new();