Function split_by_width

Source
pub fn split_by_width(s: &str, max_width: usize) -> Vec<String>
Expand description

Splits a string into lines based on display width, preserving grapheme boundaries.

This function ensures that wide characters such as emoji, CJK ideographs, or fullwidth punctuation are not split mid-grapheme. It breaks the input string into a sequence of lines, each with a total display width that does not exceed the given max_width. Ideal for terminal word wrapping and monospace layout.

§Arguments

  • s - The input string to wrap
  • max_width - Maximum display width (in columns) for each line

§Returns

A vector of strings, each representing a wrapped line within the given width.

§Example

use runefix_core::split_by_width;

let lines = split_by_width("Hello 👋 世界!", 5);
assert_eq!(lines, vec!["Hello", " 👋 ", "世界", "!"]);