Find a good byte offset to split `text` at, fitting within `avail` display columns. Prefers splitting at the last space boundary. Returns (byte_offset, display_width_consumed).
(text: &str, avail: usize)
| 488 | /// Find a good byte offset to split `text` at, fitting within `avail` display columns. |
| 489 | /// Prefers splitting at the last space boundary. Returns (byte_offset, display_width_consumed). |
| 490 | fn find_wrap_point(text: &str, avail: usize) -> (usize, usize) { |
| 491 | let mut byte_pos = 0; |
| 492 | let mut width = 0; |
| 493 | let mut last_space_byte = 0; |
| 494 | let mut last_space_width = 0; |
| 495 | |
| 496 | for ch in text.chars() { |
| 497 | let ch_w = UnicodeWidthChar::width(ch).unwrap_or(0); |
| 498 | if width + ch_w > avail { |
| 499 | break; |
| 500 | } |
| 501 | byte_pos += ch.len_utf8(); |
| 502 | width += ch_w; |
| 503 | |
| 504 | if ch == ' ' { |
| 505 | last_space_byte = byte_pos; |
| 506 | last_space_width = width; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // Prefer breaking at word boundary if we found a space in the first 60% of the line |
| 511 | if last_space_byte > 0 && last_space_width > avail / 3 { |
| 512 | (last_space_byte, last_space_width) |
| 513 | } else { |
| 514 | (byte_pos, width) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /// Style modifier |
| 519 | #[derive(Debug, Clone, Copy)] |
no outgoing calls
no test coverage detected