// Returns the minimum number of columns required to display a string
// that could be truncated.
//
// This function is useful because longer strings will always require at
// least two columns (one for the first character, then one for the ellipsis),
// but single character strings only need one column (unless that one character
// is a wide character!).
Returns the minimum number of columns required to display a string
that could be truncated.

This function is useful because longer strings will always require at
least two columns (one for the first character, then one for the ellipsis),
but single character strings only need one column (unless that one character
is a wide character!).
Returns the minimum number of columns required to display a string that could be truncated . This function is useful because longer strings will always require at least two columns ( one for the first character then one for the ellipsis ) but single character strings only need one column ( unless that one character is a wide character! ) .
pub fn min_required_columns_for_str(s: &str) -> isize {
    if s.is_empty() {
        return 0;
    }
    let mut graphemes = s.graphemes(true);
    let first_grapheme = graphemes.next().unwrap();
    let first_grapheme_width = UnicodeWidthStr::width(first_grapheme) as isize;
    // If the string is a single grapheme, then we only need the width of that
    // grapheme, but if it's more than that, we'll also need 1 character
    //
    if first_grapheme == s {
        return first_grapheme_width;
    } else {
        first_grapheme_width + 1
    }
}
pub fn min_required_columns_for_str ( s : & str ) -> isize { if s . is_empty ( ) { return 0 ; } let mut graphemes = s . graphemes ( true ) ; let first_grapheme = graphemes . next ( ) . unwrap ( ) ; let first_grapheme_width = UnicodeWidthStr :: width ( first_grapheme ) as isize ; // If the string is a single grapheme, then we only need the width of that // grapheme, but if it's more than that, we'll also need 1 character // if first_grapheme == s { return first_grapheme_width ; } else { first_grapheme_width + 1 } }
Returns the minimum number of columns required to display a string
that could be truncated.
Returns the minimum number of columns required to display a string that could be truncated .
s
&str