From 83183c4bb7d8c6cc29645f389d8457adc5f15025 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Fri, 20 Aug 2021 18:02:40 +0200 Subject: [PATCH] feat(calc): Detect and support the decimal comma --- plugins/src/calc/mod.rs | 32 +++++++++++++++++++++++++++++--- plugins/src/lib.rs | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/plugins/src/calc/mod.rs b/plugins/src/calc/mod.rs index 4fb1636..15bc2f3 100644 --- a/plugins/src/calc/mod.rs +++ b/plugins/src/calc/mod.rs @@ -11,6 +11,7 @@ pub async fn main() { let mut requests = json_input_stream(async_stdin()); let mut app = App::default(); + app.decimal_comma = uses_decimal_comma().await; while let Some(result) = requests.next().await { match result { @@ -30,6 +31,7 @@ pub async fn main() { } pub struct App { + pub decimal_comma: bool, out: Unblock, outcome: Option, regex: Regex, @@ -38,6 +40,7 @@ pub struct App { impl Default for App { fn default() -> Self { Self { + decimal_comma: false, out: async_stdout(), outcome: None, regex: Regex::new("\\x1B\\[(?:;?[0-9]{1,3})+[mGK]").expect("bad regex for qalc"), @@ -70,7 +73,7 @@ impl App { pub async fn search(&mut self, query: &str) { if let Some(mut search) = query.strip_prefix("=") { search = search.trim(); - self.outcome = qcalc(&mut self.regex, search).await; + self.outcome = qcalc(&mut self.regex, search, self.decimal_comma).await; crate::send( &mut self.out, @@ -92,8 +95,14 @@ impl App { } } -async fn qcalc(regex: &mut Regex, expression: &str) -> Option { - let mut child = Command::new("qalc") +async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Option { + let mut command = Command::new("qalc"); + + if decimal_comma { + command.args(&["-set", "decimal comma on"]); + } + + let mut child = command .env("LANG", "C") .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -171,3 +180,20 @@ async fn qcalc(regex: &mut Regex, expression: &str) -> Option { None } + +pub async fn uses_decimal_comma() -> bool { + let spawn_result = Command::new("locale") + .arg("-ck") + .arg("decimal_point") + .stderr(Stdio::null()) + .output() + .await; + + if let Ok(output) = spawn_result { + if let Ok(string) = String::from_utf8(output.stdout) { + return string.contains("decimal_point=\",\""); + } + } + + return false; +} diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index cbbeefd..8f17e86 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -31,4 +31,4 @@ pub fn mime_from_path(path: &Path) -> Cow<'static, str> { /// Launches a file with its default appplication via `xdg-open`. pub fn xdg_open>(file: S) { let _ = smol::process::Command::new("xdg-open").arg(file).spawn(); -} \ No newline at end of file +}