implement plural function

This commit is contained in:
yggverse 2024-12-18 10:50:44 +02:00
parent 1f678d7a76
commit 18b380f5ec

View File

@ -24,11 +24,12 @@ impl Result {
pub fn update(&self, current: Option<usize>, 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)]
}]
}