use std::env::args;
use std::process::{Command, Stdio, exit};
use ureq;
fn main() {
let mut argues: Vec<String> = args().skip(1).collect();
if argues.len() < 1 {
eprintln!("Missing arguement.");
help();
exit(1);
}
let mut flags = [false, false, false, false];
for arg in &argues {
match arg.as_str() {
"-t" | "--no-tldr" => flags[0] = true,
"-c" | "--no-cheatsh" => flags[1] = true,
"-i" | "--no-info" => flags[2] = true,
"-m" | "--no-man" => flags[3] = true,
"-T" | "--only-tldr" => {
flags.fill(true);
flags[0] = false;
}
"-C" | "--only-cheatsh" => {
flags.fill(true);
flags[1] = false;
}
"-I" | "--only-info" => {
flags.fill(true);
flags[2] = false;
}
"-M" | "--only-man" => {
flags.fill(true);
flags[3] = false;
}
_ => break,
}
}
argues.retain(|s| {
!matches!(
s.as_str(),
"-t" | "-c" | "-i" | "-m" | "-T" | "-C" | "-I" | "-M"
)
});
tldr(&flags, &argues);
}
fn tldr(flags: &[bool; 4], argues: &[String]) {
if flags[0] != true {
let result = Command::new("tldr")
.args(argues)
.stderr(Stdio::null())
.status();
match result {
Ok(status) => {
if status.success() {
exit(0);
} else {
cheatsh(&flags, &argues);
}
}
Err(_error) => {
cheatsh(&flags, &argues);
}
}
} else {
cheatsh(&flags, &argues);
}
}
fn cheatsh(flags: &[bool; 4], argues: &[String]) {
if flags[1] != true {
let links = argues.join("/");
let domain = "https://cheat.sh/";
let url = format!("{}{:?}", domain, links);
let request = ureq::get(&url).header("User-Agent", "curl/7.81.0").call();
match request {
Ok(response) => {
let text = response.into_body().read_to_string().unwrap();
println!("{}", text);
}
Err(_error) => {
infopage(&flags, &argues);
}
}
} else {
infopage(&flags, &argues);
}
}
fn infopage(flags: &[bool; 4], argues: &[String]) {
if flags[2] != true {
let infopage = Command::new("info")
.args(argues)
.stderr(Stdio::null())
.status();
match infopage {
Ok(status) => {
if status.success() {
exit(0);
} else {
manpage(&flags, &argues);
}
}
Err(_error) => {
manpage(&flags, &argues);
}
}
} else {
manpage(&flags, &argues);
}
}
fn manpage(flags: &[bool; 4], argues: &[String]) {
if flags[3] != true {
let manpage = Command::new("man")
.args(argues)
.stderr(Stdio::null())
.status();
match manpage {
Ok(status) => {
if status.success() {
exit(1);
} else {
println!(
"Nothing could be found. Check your internet connection, and reload tldr and man."
);
}
}
Err(_error) => {
println!(
"Nothing could be found. Check your internet connection, and reload tldr and man caches."
);
}
}
} else {
println!(
"Nothing could be found. Check your internet connection, and reload tldr and man caches."
);
}
}
fn help() {
println!("Semi useful rust utilities -- How");
println!("Easily get knowledge for a specific command, from multiple sources.");
println!("List of flags:");
println!("-t / --no-tldr: Remove tldr from the available sources.");
println!("-c / --no-cheatsh: Remove cheat.sh from the available sources.");
println!("-i / --no-info: Remove info from the available sources.");
println!("-m / --no-man: Remove man from the available sources.");
println!("-T / --only-tldr: Use only tldr for the sources.");
println!("-C / --only-cheatsh: Use only cheat.sh for the sources.");
println!("-I / --only-info: Use only info for the sources.");
println!("-M / --only-man: Use only man for the sources.");
}