kitchentimer/src/utils.rs

29 lines
1 KiB
Rust
Raw Normal View History

2021-04-18 08:20:21 +02:00
// Copyright 2021, Shy.
//
// This file is part of Kitchentimer.
//
// Kitchentimer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kitchentimer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kitchentimer. If not, see <https://www.gnu.org/licenses/>.
2021-04-10 07:57:41 +02:00
use unicode_segmentation::UnicodeSegmentation;
2021-04-16 16:01:43 +02:00
pub fn grapheme_truncate(input: &mut String, limit: usize, ellipse: char) {
2021-04-10 07:57:41 +02:00
match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) {
2021-04-12 08:59:47 +02:00
Some((i, _)) => {
input.truncate(i);
2021-04-16 16:01:43 +02:00
input.push(ellipse);
2021-04-17 18:20:39 +02:00
}
2021-04-10 07:57:41 +02:00
None => (),
}
}