feat(calc): Inform user if qalc is not installed

This commit is contained in:
Michael Aaron Murphy 2021-10-06 17:06:51 +02:00 • committed by Michael Murphy
parent 6cdf2987a9
commit bb4440e8a3

View file

@ -107,13 +107,23 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti
command.args(&["-set", "decimal comma on"]); command.args(&["-set", "decimal comma on"]);
} }
let mut child = command let spawn = command
.env("LANG", "C") .env("LANG", "C")
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::null()) .stderr(Stdio::null())
.spawn() .spawn();
.ok()?;
let mut child = match spawn {
Ok(child) => child,
Err(why) => {
return Some(if why.kind() == io::ErrorKind::NotFound {
String::from("qalc command is not installed")
} else {
format!("qalc command failed to spawn: {}", why)
})
}
};
if let Some(mut stdin) = child.stdin.take() { if let Some(mut stdin) = child.stdin.take() {
let _ = stdin let _ = stdin
@ -121,7 +131,15 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti
.await; .await;
} }
if let Some(stdout) = child.stdout.take() { let stdout = match child.stdout.take() {
Some(stdout) => stdout,
None => {
return Some(String::from(
"qalc lacks stdout pipe: did you get hit by a cosmic ray?",
));
}
};
let mut reader = smol::io::BufReader::new(stdout).lines().skip(2); let mut reader = smol::io::BufReader::new(stdout).lines().skip(2);
let mut output = String::new(); let mut output = String::new();
@ -179,10 +197,7 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti
}; };
} }
return Some(output); Some(output)
}
None
} }
pub async fn uses_decimal_comma() -> bool { pub async fn uses_decimal_comma() -> bool {