mirror of
https://github.com/Hopiu/lychee.git
synced 2026-04-27 16:34:46 +00:00
Use less-intrusive formatting inspired by eslint (#160)
This commit is contained in:
parent
a7168951c1
commit
e46710ee68
3 changed files with 36 additions and 42 deletions
|
|
@ -1,10 +1,10 @@
|
|||
use anyhow::{anyhow, Context, Result};
|
||||
use console::style;
|
||||
use headers::authorization::Basic;
|
||||
use headers::{Authorization, HeaderMap, HeaderMapExt, HeaderName};
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use options::Format;
|
||||
use regex::RegexSet;
|
||||
use stats::color_response;
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
use std::{fs, str::FromStr};
|
||||
use structopt::StructOpt;
|
||||
|
|
@ -16,10 +16,7 @@ mod stats;
|
|||
use crate::options::{Config, LycheeOptions};
|
||||
use crate::stats::ResponseStats;
|
||||
|
||||
use lychee::{
|
||||
collector::{self, Input},
|
||||
Status,
|
||||
};
|
||||
use lychee::collector::{self, Input};
|
||||
use lychee::{ClientBuilder, ClientPool, Response};
|
||||
|
||||
/// A C-like enum that can be cast to `i32` and used as process exit code.
|
||||
|
|
@ -66,30 +63,19 @@ fn run_main() -> Result<i32> {
|
|||
runtime.block_on(run(cfg, opts.inputs()))
|
||||
}
|
||||
|
||||
fn color_response(response: &Response) -> String {
|
||||
let out = match response.status {
|
||||
Status::Ok(_) => style(response).green().bright(),
|
||||
Status::Redirected(_) => style(response),
|
||||
Status::Excluded => style(response).dim(),
|
||||
Status::Error(_) => style(response).yellow().bright(),
|
||||
Status::Timeout(_) => style(response).yellow().bright(),
|
||||
Status::Failed(_) => style(response).red().bright(),
|
||||
};
|
||||
out.to_string()
|
||||
}
|
||||
|
||||
fn show_progress(progress_bar: &Option<ProgressBar>, response: &Response, verbose: bool) {
|
||||
let out = color_response(response);
|
||||
if let Some(pb) = progress_bar {
|
||||
pb.inc(1);
|
||||
pb.set_message(&response.to_string());
|
||||
pb.set_message(&out);
|
||||
if verbose {
|
||||
pb.println(color_response(response));
|
||||
pb.println(out);
|
||||
}
|
||||
} else {
|
||||
if (response.status.is_success() || response.status.is_excluded()) && !verbose {
|
||||
return;
|
||||
}
|
||||
println!("{}", color_response(response));
|
||||
println!("{}", out);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -182,7 +168,7 @@ async fn run(cfg: &Config, inputs: Vec<Input>) -> Result<i32> {
|
|||
// Note that print statements may interfere with the progress bar, so this
|
||||
// must go before printing the stats
|
||||
if let Some(pb) = &pb {
|
||||
pb.finish_with_message("Done");
|
||||
pb.finish_and_clear();
|
||||
}
|
||||
|
||||
let stats_formatted = fmt(&stats, &cfg.format)?;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use console::style;
|
||||
use pad::{Alignment, PadStr};
|
||||
use serde::Serialize;
|
||||
|
||||
|
|
@ -6,11 +7,23 @@ use std::{
|
|||
fmt::{self, Display},
|
||||
};
|
||||
|
||||
use lychee::{collector::Input, Response, Status::*};
|
||||
use lychee::{self, collector::Input, Response, Status};
|
||||
|
||||
// Maximum padding for each entry in the final statistics output
|
||||
const MAX_PADDING: usize = 20;
|
||||
|
||||
pub fn color_response(response: &Response) -> String {
|
||||
let out = match response.status {
|
||||
Status::Ok(_) => style(response).green().bright(),
|
||||
Status::Redirected(_) => style(response),
|
||||
Status::Excluded => style(response).dim(),
|
||||
Status::Error(_) => style(response).yellow().bright(),
|
||||
Status::Timeout(_) => style(response).yellow().bright(),
|
||||
Status::Failed(_) => style(response).red().bright(),
|
||||
};
|
||||
out.to_string()
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ResponseStats {
|
||||
total: usize,
|
||||
|
|
@ -41,17 +54,17 @@ impl ResponseStats {
|
|||
pub fn add(&mut self, response: Response) {
|
||||
self.total += 1;
|
||||
match response.status {
|
||||
Failed(_) => self.failures += 1,
|
||||
Timeout(_) => self.timeouts += 1,
|
||||
Redirected(_) => self.redirects += 1,
|
||||
Excluded => self.excludes += 1,
|
||||
Error(_) => self.errors += 1,
|
||||
Status::Failed(_) => self.failures += 1,
|
||||
Status::Timeout(_) => self.timeouts += 1,
|
||||
Status::Redirected(_) => self.redirects += 1,
|
||||
Status::Excluded => self.excludes += 1,
|
||||
Status::Error(_) => self.errors += 1,
|
||||
_ => self.successful += 1,
|
||||
}
|
||||
|
||||
if matches!(
|
||||
response.status,
|
||||
Failed(_) | Timeout(_) | Redirected(_) | Error(_)
|
||||
Status::Failed(_) | Status::Timeout(_) | Status::Redirected(_) | Status::Error(_)
|
||||
) {
|
||||
let fail = self.fail_map.entry(response.source.clone()).or_default();
|
||||
fail.insert(response);
|
||||
|
|
@ -91,16 +104,11 @@ impl Display for ResponseStats {
|
|||
writeln!(f)?;
|
||||
}
|
||||
for (input, responses) in &self.fail_map {
|
||||
writeln!(f, "Input: {}", input)?;
|
||||
writeln!(f, "Errors in {}", input)?;
|
||||
for response in responses {
|
||||
writeln!(
|
||||
f,
|
||||
" {} {}\n {}",
|
||||
response.status.icon(),
|
||||
response.uri,
|
||||
response.status
|
||||
)?
|
||||
writeln!(f, "{}", color_response(response))?
|
||||
}
|
||||
writeln!(f)?;
|
||||
}
|
||||
writeln!(f)
|
||||
}
|
||||
|
|
|
|||
12
src/types.rs
12
src/types.rs
|
|
@ -153,12 +153,12 @@ impl Status {
|
|||
|
||||
pub fn icon(&self) -> &str {
|
||||
match self {
|
||||
Status::Ok(_) => "✅",
|
||||
Status::Redirected(_) => "🔀️",
|
||||
Status::Excluded => "👻",
|
||||
Status::Failed(_) => "🚫",
|
||||
Status::Error(_) => "⚡",
|
||||
Status::Timeout(_) => "⌛",
|
||||
Status::Ok(_) => "✔",
|
||||
Status::Redirected(_) => "⇄️",
|
||||
Status::Excluded => "?",
|
||||
Status::Failed(_) => "✗",
|
||||
Status::Error(_) => "↯",
|
||||
Status::Timeout(_) => "⧖",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue