use crate::{schemas::AddressType, soutln};
fn red_text(text: &str) -> String {
format!("\x1b[1;31m{text}\x1b[0m")
}
fn cyan_text(text: &str) -> String {
format!("\x1B[36m{text}\x1B[0m")
}
fn yellow_text(text: &str) -> String {
format!("\x1b[1;33m{text}\x1b[0m")
}
fn bold_text(text: &str) -> String {
format!("\x1B[1m{text}\x1B[0m")
}
pub fn format_known_address(remote_address: &str, address_type: &AddressType) -> String {
match address_type {
AddressType::Unspecified => {
format!("*{remote_address}*")
}
AddressType::Localhost => {
format!("*{remote_address} localhost*")
}
AddressType::Extern => remote_address.to_string(),
}
}
pub fn pretty_print_info(text: &str) {
soutln!("{}", bold_text(&format!("{} {}", cyan_text("Info:"), text)));
}
pub fn pretty_print_error(text: &str) {
soutln!("{}", bold_text(&format!("{} {}", red_text("Error:"), text)));
}
pub fn pretty_print_warning(text: &str) {
soutln!(
"{}",
bold_text(&format!("{} {}", yellow_text("Warning:"), text))
);
}
pub fn pretty_print_syntax_error(preamble: &str, text: &str, line: usize, column: usize) {
let erronous_line: &str = text.lines().nth(line - 1).unwrap_or(text);
let line_pointer = "└─>";
pretty_print_error(preamble);
soutln!(" {}", red_text("│"));
soutln!(" {} {}", red_text(line_pointer), erronous_line);
soutln!(
" {} {}",
" ".repeat(line_pointer.chars().count() + column - 1),
red_text("^")
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_known_address_localhost() {
let addr = "127.0.0.1".to_string();
let result = format_known_address(&addr, &AddressType::Localhost);
assert_eq!(result, "*127.0.0.1 localhost*");
}
#[test]
fn test_format_known_address_unspecified() {
let addr = "0.0.0.0".to_string();
let result = format_known_address(&addr, &AddressType::Unspecified);
assert_eq!(result, "*0.0.0.0*");
}
#[test]
fn test_format_known_address_extern() {
let addr = "123.123.123".to_string();
let result = format_known_address(&addr, &AddressType::Extern);
assert_eq!(result, "123.123.123");
}
}