pub fn format_columns<S: AsRef<str>>(strs: &[S], screen_width: usize,
        horizontal: bool) -> Option<Vec<usize>> {
    if strs.is_empty() {
        return None;
    }

    let n_strs = strs.len();

    let (mut min_len, mut max_len) = min_max(strs.iter().map(|s| s.as_ref().chars().count()));

    if min_len == 0 { min_len = 1; }
    if max_len == 0 { max_len = 1; }

    let mut min_cols = min(n_strs, screen_width / max_len);
    let max_cols = min(n_strs, screen_width / min_len);

    if min_cols <= 1 {
        // No point in checking whether text can fit within one column
        min_cols = 2;
    }

    if max_cols <= 1 {
        return None;
    }

    let mut col_sizes = if min_cols == max_cols {
        vec![vec![0; max_cols]]
    } else {
        (min_cols..max_cols + 1)
            .map(|n| vec![0; n]).collect::<Vec<_>>()
    };

    for (i, s) in strs.iter().enumerate() {
        let len = s.as_ref().chars().count();

        for cols in &mut col_sizes {
            let n_cols = cols.len();

            let col = if horizontal {
                i % n_cols
            } else {
                let per_col = (n_strs + (n_cols - 1)) / n_cols;
                i / per_col
            };

            let real_len = if col == n_cols - 1 { len } else { len + COL_SPACE };

            if real_len > cols[col] {
                cols[col] = real_len;
            }
        }
    }

    for cols in col_sizes.into_iter().rev() {
        if cols.iter().fold(0, |a, b| a + b) <= screen_width {
            return Some(cols);
        }
    }

    None
}
/// Formats a series of strings into columns, fitting within a given screen width.
/// Returns the size of each resulting column, including spacing.
///
/// If the strings cannot be formatted into columns (e.g. one or more strings
/// are longer than the screen width) or the result would be only one column,
/// `None` is returned.
Formats a series of strings into columns, fitting within a given screen width.
Returns the size of each resulting column, including spacing.

If the strings cannot be formatted into columns  or the result would be only one column,
`None` is returned.
Formats a series of strings into columns fitting within a given screen width . Returns the size of each resulting column including spacing . If the strings cannot be formatted into columns or the result would be only one column ` None ` is returned .
pub fn format_columns<S: AsRef<str>>(strs: &[S], screen_width: usize,
        horizontal: bool) -> Option<Vec<usize>> {
    if strs.is_empty() {
        return None;
    }
    let n_strs = strs.len();
    let (mut min_len, mut max_len) = min_max(strs.iter().map(|s| s.as_ref().chars().count()));
    if min_len == 0 { min_len = 1; }
    if max_len == 0 { max_len = 1; }
    let mut min_cols = min(n_strs, screen_width / max_len);
    let max_cols = min(n_strs, screen_width / min_len);
    if min_cols <= 1 {
        // No point in checking whether text can fit within one column
        min_cols = 2;
    }
    if max_cols <= 1 {
        return None;
    }
    let mut col_sizes = if min_cols == max_cols {
        vec![vec![0; max_cols]]
    } else {
        (min_cols..max_cols + 1)
            .map(|n| vec![0; n]).collect::<Vec<_>>()
    };
    for (i, s) in strs.iter().enumerate() {
        let len = s.as_ref().chars().count();
        for cols in &mut col_sizes {
            let n_cols = cols.len();
            let col = if horizontal {
                i % n_cols
            } else {
                let per_col = (n_strs + (n_cols - 1)) / n_cols;
                i / per_col
            };
            let real_len = if col == n_cols - 1 { len } else { len + COL_SPACE };
            if real_len > cols[col] {
                cols[col] = real_len;
            }
        }
    }
    for cols in col_sizes.into_iter().rev() {
        if cols.iter().fold(0, |a, b| a + b) <= screen_width {
            return Some(cols);
        }
    }
    None
}
pub fn format_columns < S : AsRef < str > > ( strs : & [ S ] , screen_width : usize , horizontal : bool ) -> Option < Vec < usize > > { if strs . is_empty ( ) { return None ; } let n_strs = strs . len ( ) ; let ( mut min_len , mut max_len ) = min_max ( strs . iter ( ) . map ( | s | s . as_ref ( ) . chars ( ) . count ( ) ) ) ; if min_len == 0 { min_len = 1 ; } if max_len == 0 { max_len = 1 ; } let mut min_cols = min ( n_strs , screen_width / max_len ) ; let max_cols = min ( n_strs , screen_width / min_len ) ; if min_cols <= 1 { // No point in checking whether text can fit within one column min_cols = 2 ; } if max_cols <= 1 { return None ; } let mut col_sizes = if min_cols == max_cols { vec ! [ vec [ 0 max_cols ] ] } else { ( min_cols .. max_cols + 1 ) . map ( | n | vec ! [ 0 n ] ) . collect :: < Vec < _ > > ( ) } ; for ( i , s ) in strs . iter ( ) . enumerate ( ) { let len = s . as_ref ( ) . chars ( ) . count ( ) ; for cols in & mut col_sizes { let n_cols = cols . len ( ) ; let col = if horizontal { i % n_cols } else { let per_col = ( n_strs + ( n_cols - 1 ) ) / n_cols ; i / per_col } ; let real_len = if col == n_cols - 1 { len } else { len + COL_SPACE } ; if real_len > cols [ col ] { cols [ col ] = real_len ; } } } for cols in col_sizes . into_iter ( ) . rev ( ) { if cols . iter ( ) . fold ( 0 , | a , b | a + b ) <= screen_width { return Some ( cols ) ; } } None }
Formats a series of strings into columns, fitting within a given screen width.
Formats a series of strings into columns fitting within a given screen width .
strs
&[S]
screen_width
usize
horizontal
bool
strs
&[S]
screen_width
usize
horizontal
bool