mirror of
https://github.com/harivansh-afk/deskctl.git
synced 2026-04-18 01:00:29 +00:00
runtime contract enforcement (#6)
This commit is contained in:
parent
61f4738311
commit
543d41c3a2
7 changed files with 958 additions and 157 deletions
|
|
@ -88,9 +88,21 @@ impl std::fmt::Display for WindowInfo {
|
|||
|
||||
#[allow(dead_code)]
|
||||
fn truncate(s: &str, max: usize) -> String {
|
||||
if s.len() <= max {
|
||||
if s.chars().count() <= max {
|
||||
s.to_string()
|
||||
} else {
|
||||
format!("{}...", &s[..max - 3])
|
||||
let truncated: String = s.chars().take(max.saturating_sub(3)).collect();
|
||||
format!("{truncated}...")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::truncate;
|
||||
|
||||
#[test]
|
||||
fn truncate_is_char_safe() {
|
||||
let input = format!("fire{}fox", '\u{00E9}');
|
||||
assert_eq!(truncate(&input, 7), "fire...");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue