speedboat/src/common/console.rs

68 lines
1.4 KiB
Rust

use colored::{ColoredString, Colorize};
use std::process;
use super::conf::VERSION;
pub fn fatal(msg: &str) -> ! {
println!("{}: {msg}", "fatal".red().bold());
process::exit(-1);
}
pub fn fmtwhitespace(s: String) -> String {
let mut out = String::with_capacity(s.len());
s.split_whitespace().for_each(|w| {
if !out.is_empty() {
out.push(' ');
}
out.push_str(w);
});
out
}
pub fn fmtcode(code: u16) -> ColoredString {
match code {
200..=299 => code.to_string().green(),
300..=399 => code.to_string().yellow(),
400..=499 => code.to_string().bright_red(),
500..=599 => code.to_string().red().bold(),
_ => code.to_string().black(),
}
}
pub fn parsetitle(s: String) -> String {
let title = fmtwhitespace(s);
format!("{}{}{}",
"title[".bright_black().bold(),
title.bright_cyan().bold(),
"]".bright_black().bold()
)
}
pub fn parsebody(s: String) -> String {
format!("{}{}{}",
"body[".bright_black().bold(),
s.bright_magenta().bold(),
"]".bright_black().bold()
)
}
pub fn parsehit(sc: u16, url: String) -> String {
format!(
"{} {} {}",
fmtcode(sc),
"|".bright_black().bold(),
url.white().underline(),
)
}
pub fn banner() {
eprintln!(
"{}{} {}",
"speed".bright_cyan().bold(),
"boat".bright_magenta().bold(),
VERSION.bright_black()
);
}