use crossterm::{
execute,
style::{Color, Print, ResetColor, SetForegroundColor},
};
use std::{io, process::Command};
pub const NLC: &str = "\n";
pub const RUSLVER: &str = "0.1.3\n";
pub fn v() {
println!("rusl - {}", RUSLVER);
}
pub fn nl() {
println!("{}", NLC);
}
pub fn hw() {
println!("Hello, World!");
}
pub fn cls(printversion: &str) {
if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "cls"]).status().unwrap();
} else {
Command::new("clear").status().unwrap();
}
match printversion.trim().to_lowercase().as_str() {
"yes" | "y" => crate::v(),
"no" | "n" => {}
_ => panic!("Invalid argument for rsrusl::cls(printversion). Use 'yes' or 'no'."),
}
}
pub fn log(status: &str, message: &str) {
let mut stdout = io::stdout();
let status_color = match status {
"tip" => Color::Magenta,
"test" => Color::Blue,
"good" => Color::Green,
"error" => Color::Red,
"warning" => Color::DarkYellow, "userhelp" => Color::Cyan,
_ => Color::White, };
let formattedmessage = format!("| {}\n", message);
execute!(
stdout,
SetForegroundColor(status_color),
Print("▼ "),
ResetColor,
Print(formattedmessage),
)
.unwrap();
}
pub fn g(name: &str) {
let greetlanguages = [
"Hello",
"Hi",
"Sup",
"Howdy",
"Greetings",
"Namaste",
"Bonjour",
"Salut",
"Hola",
"Ciao",
"Hallo",
"Hej",
"Hei",
"Merhaba",
"привет",
"Привіт",
"Γεια σας",
];
let idx = fastrand::usize(0..greetlanguages.len());
println!("{}, {}!", greetlanguages[idx], name);
}
pub mod i {
use crossterm::{
execute,
style::{Color, Print, ResetColor, SetForegroundColor},
};
use std::io::{self, Write};
pub fn userinput(prompt: &str) -> String {
let mut stdout = io::stdout();
let statuscolor = Color::Cyan;
let indicator = "🞂 ";
let formattedprompt = format!("| {} ", prompt);
execute!(
stdout,
SetForegroundColor(statuscolor),
Print(indicator),
ResetColor,
Print(formattedprompt),
)
.unwrap();
io::stdout().flush().expect("Failed to flush stdout");
let mut useroutput = String::new();
io::stdin()
.read_line(&mut useroutput)
.expect("Failed to read input");
useroutput.trim().to_string()
}
}
pub mod testing {
use crossterm::{
execute,
style::{Color, Print, ResetColor, SetForegroundColor},
};
use std::{io, panic, time::Instant};
pub fn run_tests<F>(test_block: F)
where
F: FnOnce() + panic::UnwindSafe,
{
let start_time = Instant::now();
let old_hook = panic::take_hook();
panic::set_hook(Box::new(|_info| {
}));
let result = panic::catch_unwind(|| {
test_block();
});
panic::set_hook(old_hook);
let elapsed = start_time.elapsed();
let mut stdout = io::stdout();
if result.is_err() {
execute!(
stdout,
SetForegroundColor(Color::Red),
Print("\n▼ "),
ResetColor
)
.unwrap();
} else {
execute!(
stdout,
SetForegroundColor(Color::Blue),
Print("\n▼ "),
ResetColor
)
.unwrap();
}
let time_taken = format!("{:.2?}", elapsed);
execute!(stdout, Print(format!("| {} | ", time_taken))).unwrap();
if result.is_err() {
execute!(
stdout,
SetForegroundColor(Color::Red),
Print("An error occurred during tests."),
ResetColor
)
.unwrap();
} else {
execute!(stdout, Print("All tests ran successfully.")).unwrap();
}
}
pub fn sample_test() {
let input = crate::i::userinput("What is your name?");
println!("{}", input);
}
pub fn run() {
run_tests(|| {
sample_test();
});
}
}
#[macro_export]
macro_rules! tests {
($($body:tt)*) => {
$crate::cls("y");
$crate::testing::run_tests(|| {
$($body)*
})
}
}