| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode" |
| 6 | "unicode/utf8" |
| 7 | |
| 8 | "charm.land/lipgloss/v2" |
| 9 | "github.com/charmbracelet/x/ansi" |
| 10 | ) |
| 11 | |
| 12 | // hardwrapTerminalText measures whole graphemes, including clusters starting |
| 13 | // with ASCII (keycap digits, # and *). ansi.Hardwrap's ASCII fast path counts |
| 14 | // the base separately from its combining suffix and underestimates these. |
| 15 | // Escape sequences pass through without consuming columns or being split. |
| 16 | func hardwrapTerminalText(s string, width int) string { |
| 17 | if width <= 0 || lipgloss.Width(s) <= width { |
| 18 | return s |
| 19 | } |
| 20 | var out strings.Builder |
| 21 | var state byte |
| 22 | column := 0 |
| 23 | for len(s) > 0 { |
| 24 | seq, cells, n, next := terminalSequence(s, state) |
| 25 | if cells > 0 { |
| 26 | if column > 0 && column+cells > width { |
| 27 | out.WriteByte('\n') |
| 28 | column = 0 |
| 29 | } |
| 30 | column += cells |
| 31 | } else if seq == "\n" || seq == "\r" { |
| 32 | column = 0 |
| 33 | } |
| 34 | out.WriteString(seq) |
| 35 | s, state = s[n:], next |
| 36 | } |
| 37 | return out.String() |
| 38 | } |
| 39 | |
| 40 | // wrapTerminalText uses the same complete clusters as the screen buffer, then |
| 41 | // balances SGR and hyperlinks at each break. Running the upstream word wrapper |
| 42 | // first is too late: it can already have split an ASCII base from its suffix. |
| 43 | func wrapTerminalText(s string, width int) string { |
| 44 | if width <= 0 { |
| 45 | return s |
| 46 | } |
| 47 | s = strings.ReplaceAll(strings.ReplaceAll(s, "\r\n", "\n"), "\t", " ") |
| 48 | var balanced strings.Builder |
| 49 | writer := lipgloss.NewWrapWriter(&balanced) |
| 50 | _, _ = writer.Write([]byte(wordwrapTerminalText(s, width))) // strings.Builder cannot fail |
| 51 | _ = writer.Close() |
| 52 | rows := strings.Split(balanced.String(), "\n") |
| 53 | for i := range rows { |
| 54 | rows[i] = padRight(rows[i], width) |
| 55 | } |
| 56 | return strings.Join(rows, "\n") |
| 57 | } |
| 58 | |
| 59 | func terminalSequence(s string, state byte) (seq string, cells, n int, next byte) { |
| 60 | seq, cells, n, next = ansi.DecodeSequence(s, state, nil) |
| 61 | if cells > 0 && n == 1 && len(s) > 1 && s[1] >= utf8.RuneSelf { |
| 62 | seq, cells = ansi.FirstGraphemeCluster(s, ansi.GraphemeWidth) |
| 63 | n = len(seq) |
| 64 | } |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | func wordwrapTerminalText(s string, width int) string { |
| 69 | var out, word, spaces strings.Builder |
| 70 | column, wordWidth, spaceWidth := 0, 0, 0 |
| 71 | var state byte |
| 72 | flush := func() { |
| 73 | out.WriteString(spaces.String()) |
| 74 | out.WriteString(word.String()) |
| 75 | column += spaceWidth + wordWidth |
| 76 | spaces.Reset() |
| 77 | word.Reset() |
| 78 | spaceWidth, wordWidth = 0, 0 |
| 79 | } |
| 80 | newline := func() { |
| 81 | out.WriteByte('\n') |
| 82 | column = 0 |
| 83 | spaces.Reset() |
| 84 | spaceWidth = 0 |
| 85 | } |
| 86 | for len(s) > 0 { |
| 87 | seq, cells, n, next := terminalSequence(s, state) |
| 88 | r, _ := utf8.DecodeRuneInString(seq) |
| 89 | switch { |
| 90 | case seq == "\n": |
| 91 | flush() |
| 92 | newline() |
| 93 | case cells > 0 && unicode.IsSpace(r) && r != '\u00a0': |
| 94 | if word.Len() > 0 { |
| 95 | flush() |
| 96 | } |
| 97 | spaces.WriteString(seq) |
| 98 | spaceWidth += cells |
| 99 | case cells > 0: |
| 100 | if wordWidth+cells > width { |
| 101 | flush() |
| 102 | } |
| 103 | word.WriteString(seq) |
| 104 | wordWidth += cells |
| 105 | if column+spaceWidth+wordWidth > width { |
| 106 | newline() |
| 107 | } |
| 108 | if seq == "-" || wordWidth == width { |
| 109 | flush() |
| 110 | } |
| 111 | default: |
| 112 | word.WriteString(seq) |
| 113 | } |
| 114 | s, state = s[n:], next |
| 115 | } |
| 116 | flush() |
| 117 | // Indentation and trailing spaces are content too. Bound those without |
| 118 | // discarding them or asking the terminal to perform an implicit wrap. |
| 119 | return hardwrapTerminalText(out.String(), width) |
| 120 | } |
| 121 |