Skip to content

Commit bb4440e

Browse files
committed
feat(calc): Inform user if qalc is not installed
1 parent 6cdf298 commit bb4440e

File tree

1 file changed

+66
-51
lines changed

1 file changed

+66
-51
lines changed

plugins/src/calc/mod.rs

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -107,82 +107,97 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti
107107
command.args(&["-set", "decimal comma on"]);
108108
}
109109

110-
let mut child = command
110+
let spawn = command
111111
.env("LANG", "C")
112112
.stdin(Stdio::piped())
113113
.stdout(Stdio::piped())
114114
.stderr(Stdio::null())
115-
.spawn()
116-
.ok()?;
115+
.spawn();
116+
117+
let mut child = match spawn {
118+
Ok(child) => child,
119+
Err(why) => {
120+
return Some(if why.kind() == io::ErrorKind::NotFound {
121+
String::from("qalc command is not installed")
122+
} else {
123+
format!("qalc command failed to spawn: {}", why)
124+
})
125+
}
126+
};
117127

118128
if let Some(mut stdin) = child.stdin.take() {
119129
let _ = stdin
120130
.write_all([expression, "\n"].concat().as_bytes())
121131
.await;
122132
}
123133

124-
if let Some(stdout) = child.stdout.take() {
125-
let mut reader = smol::io::BufReader::new(stdout).lines().skip(2);
126-
let mut output = String::new();
127-
128-
fn has_issue(line: &str) -> bool {
129-
line.starts_with("error") || line.starts_with("warning")
134+
let stdout = match child.stdout.take() {
135+
Some(stdout) => stdout,
136+
None => {
137+
return Some(String::from(
138+
"qalc lacks stdout pipe: did you get hit by a cosmic ray?",
139+
));
130140
}
141+
};
131142

132-
while let Some(Ok(line)) = reader.next().await {
133-
let line = line.trim();
143+
let mut reader = smol::io::BufReader::new(stdout).lines().skip(2);
144+
let mut output = String::new();
134145

135-
if line.is_empty() {
136-
break;
137-
}
146+
fn has_issue(line: &str) -> bool {
147+
line.starts_with("error") || line.starts_with("warning")
148+
}
138149

139-
let normalized = regex.replace_all(line, "");
140-
let mut normalized = normalized.as_ref();
150+
while let Some(Ok(line)) = reader.next().await {
151+
let line = line.trim();
141152

142-
if has_issue(normalized) {
143-
return None;
144-
} else {
145-
if !output.is_empty() {
146-
output.push(' ');
147-
}
153+
if line.is_empty() {
154+
break;
155+
}
148156

149-
if normalized.starts_with('(') {
150-
let mut level = 1;
151-
for (byte_pos, character) in normalized[1..].char_indices() {
152-
if character == '(' {
153-
level += 1;
154-
} else if character == ')' {
155-
level -= 1;
156-
157-
if level == 0 {
158-
normalized = normalized[byte_pos + 2..].trim_start();
159-
break;
160-
}
157+
let normalized = regex.replace_all(line, "");
158+
let mut normalized = normalized.as_ref();
159+
160+
if has_issue(normalized) {
161+
return None;
162+
} else {
163+
if !output.is_empty() {
164+
output.push(' ');
165+
}
166+
167+
if normalized.starts_with('(') {
168+
let mut level = 1;
169+
for (byte_pos, character) in normalized[1..].char_indices() {
170+
if character == '(' {
171+
level += 1;
172+
} else if character == ')' {
173+
level -= 1;
174+
175+
if level == 0 {
176+
normalized = normalized[byte_pos + 2..].trim_start();
177+
break;
161178
}
162179
}
163180
}
181+
}
164182

165-
let cut = if let Some(pos) = normalized.find('=') {
166-
pos + 1
167-
} else if let Some(pos) = normalized.find('≈') {
168-
pos + '≈'.len_utf8()
169-
} else {
170-
return None;
171-
};
172-
173-
normalized = normalized[cut..].trim_start();
174-
if normalized.starts_with('(') && normalized.ends_with(')') {
175-
normalized = &normalized[1..normalized.len() - 1];
176-
}
177-
178-
output.push_str(normalized);
183+
let cut = if let Some(pos) = normalized.find('=') {
184+
pos + 1
185+
} else if let Some(pos) = normalized.find('≈') {
186+
pos + '≈'.len_utf8()
187+
} else {
188+
return None;
179189
};
180-
}
181190

182-
return Some(output);
191+
normalized = normalized[cut..].trim_start();
192+
if normalized.starts_with('(') && normalized.ends_with(')') {
193+
normalized = &normalized[1..normalized.len() - 1];
194+
}
195+
196+
output.push_str(normalized);
197+
};
183198
}
184199

185-
None
200+
Some(output)
186201
}
187202

188203
pub async fn uses_decimal_comma() -> bool {

0 commit comments

Comments
 (0)