timeline: add ui_abbrev_name

This is a simple and fast function for abbreviating usernames in the
timeline. If they are too large they make the timeline too wide. We'll
probably need to make this adjustable based on the timeline width, but
this is ok for now.

Changelog-Added: Abbreviate usernames in timelines
This commit is contained in:
William Casarin
2023-07-09 11:11:03 -07:00
parent a0be5f0173
commit 6232a8429e
3 changed files with 33 additions and 1 deletions

20
src/abbrev.rs Normal file
View File

@@ -0,0 +1,20 @@
#[inline]
pub fn floor_char_boundary(s: &str, index: usize) -> usize {
if index >= s.len() {
s.len()
} else {
let lower_bound = index.saturating_sub(3);
let new_index = s.as_bytes()[lower_bound..=index]
.iter()
.rposition(|b| is_utf8_char_boundary(*b));
// SAFETY: we know that the character boundary will be within four bytes
unsafe { lower_bound + new_index.unwrap_unchecked() }
}
}
#[inline]
fn is_utf8_char_boundary(c: u8) -> bool {
// This is bit magic equivalent to: b < 128 || b >= 192
(c as i8) >= -0x40
}