package main import "core:fmt" import "core:strings" import "core:unicode" import "core:unicode/utf8" draw_task_progress :: proc(sb: ^strings.Builder, text: string, max_width: int) { if max_width <= 0 { return } safe: Stream defer stream_destroy(&safe) _ = stream_append_safe_text(&safe, transmute([]u8)text) safe_text := string(safe.transcript.bytes[:]) text_width := 0 bytes := transmute([]u8)safe_text for index := 0; index < len(bytes); { character, size := utf8.decode_rune(bytes[index:]) if character == utf8.RUNE_ERROR && size == 1 { index += 1 continue } if character == '\n' || character == '\r' || character == '\t' { text_width += 1 } else if !unicode.is_control(character) { text_width += unicode.normalized_east_asian_width(character) } index += size } limit := max_width truncated := text_width > max_width if truncated { limit = max(0, max_width - len("...")) } written_width := 0 for index := 0; index < len(bytes); { character, size := utf8.decode_rune(bytes[index:]) if character == utf8.RUNE_ERROR && size == 1 { index += 1 continue } character_width := unicode.normalized_east_asian_width(character) whitespace := character == '\n' || character == '\r' || character == '\t' if whitespace { character_width = 1 } else if unicode.is_control(character) { index += size continue } if written_width + character_width > limit { break } if whitespace { fmt.sbprint(sb, " ") } else { fmt.sbprint(sb, string(bytes[index:index + size])) } written_width += character_width index += size } if truncated { for _ in 0 ..< min(max_width, len("...")) { fmt.sbprint(sb, ".") } } }