Render a list into a fixed width, eliding the tail as `first,+n`. Truncating mid-word would leave a project name that looks real but is not; `+2` at least says how much is hidden and sends the reader to `show`.
(items: &[String], width: usize)
| 155 | /// Truncating mid-word would leave a project name that looks real but is not; |
| 156 | /// `+2` at least says how much is hidden and sends the reader to `show`. |
| 157 | fn summarize(items: &[String], width: usize) -> String { |
| 158 | if items.is_empty() { |
| 159 | return "(all)".to_string(); |
| 160 | } |
| 161 | let joined = items.join(","); |
| 162 | if joined.len() <= width { |
| 163 | return joined; |
| 164 | } |
| 165 | let mut out = String::new(); |
| 166 | let mut shown = 0; |
| 167 | for item in items { |
| 168 | let candidate = if out.is_empty() { |
| 169 | item.clone() |
| 170 | } else { |
| 171 | format!("{out},{item}") |
| 172 | }; |
| 173 | // Leave room for the ",+n" suffix. |
| 174 | if candidate.len() + 4 > width { |
| 175 | break; |
| 176 | } |
| 177 | out = candidate; |
| 178 | shown += 1; |
| 179 | } |
| 180 | if shown == 0 { |
| 181 | // Even one entry does not fit; show what we can rather than nothing. |
| 182 | return items[0].chars().take(width).collect(); |
| 183 | } |
| 184 | format!("{out},+{}", items.len() - shown) |
| 185 | } |
| 186 | |
| 187 | #[cfg(test)] |
| 188 | mod tests { |