From 18b380f5ecaefcec921f75e5a4e3c89dce5f69d8 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 10:50:44 +0200 Subject: [PATCH] implement plural function --- .../window/tab/item/page/search/form/result.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/browser/window/tab/item/page/search/form/result.rs b/src/app/browser/window/tab/item/page/search/form/result.rs index c85ea770..ea4a248d 100644 --- a/src/app/browser/window/tab/item/page/search/form/result.rs +++ b/src/app/browser/window/tab/item/page/search/form/result.rs @@ -24,11 +24,12 @@ impl Result { pub fn update(&self, current: Option, total: usize) { if total > 0 { + let matches = plural(total, &["match", "matches", "matches"]); match current { Some(position) => self .label - .set_label(&format!("{position} of {total} matches")), - None => self.label.set_label(&format!("{total} matches")), + .set_label(&format!("{position} of {total} {matches}")), + None => self.label.set_label(&format!("{total} {matches}")), } self.label.remove_css_class("error"); } else { @@ -37,3 +38,13 @@ impl Result { } } } + +// Tools + +fn plural<'a>(n: usize, s: &[&'a str]) -> &'a str { + s[if (n % 100) > 4 && (n % 100) < 20 { + 2 + } else { + [2, 0, 1, 1, 1, 2][std::cmp::min(n % 10, 5)] + }] +}