| 1 | package displaywidth |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/clipperhouse/uax29/v2/graphemes" |
| 7 | ) |
| 8 | |
| 9 | // TruncateString truncates a string to the given maxWidth, and appends the |
| 10 | // given tail if the string is truncated. |
| 11 | // |
| 12 | // It ensures the visible width, including the width of the tail, is less than or |
| 13 | // equal to maxWidth. |
| 14 | // |
| 15 | // When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that |
| 16 | // appear after the truncation point are preserved in the output. This ensures |
| 17 | // that escape sequences such as SGR resets are not lost, preventing color |
| 18 | // bleed in terminal output. |
| 19 | // |
| 20 | // [Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values |
| 21 | // (0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them |
| 22 | // during truncation can shift byte boundaries and form unintended visible |
| 23 | // characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width |
| 24 | // measurement. |
| 25 | func (options Options) TruncateString(s string, maxWidth int, tail string) string { |
| 26 | // We deliberately ignore ControlSequences8Bit for truncation, see above. |
| 27 | options.ControlSequences8Bit = false |
| 28 | |
| 29 | maxWidthWithoutTail := maxWidth - options.String(tail) |
| 30 | |
| 31 | var pos, total int |
| 32 | g := graphemes.FromString(s) |
| 33 | g.AnsiEscapeSequences = options.ControlSequences |
| 34 | |
| 35 | for g.Next() { |
| 36 | gw := graphemeWidth(g.Value(), options) |
| 37 | if total+gw <= maxWidthWithoutTail { |
| 38 | pos = g.End() |
| 39 | } |
| 40 | total += gw |
| 41 | if total > maxWidth { |
| 42 | if options.ControlSequences { |
| 43 | // Build result with trailing 7-bit ANSI escape sequences preserved |
| 44 | var b strings.Builder |
| 45 | b.Grow(len(s) + len(tail)) // at most original + tail |
| 46 | b.WriteString(s[:pos]) |
| 47 | b.WriteString(tail) |
| 48 | |
| 49 | rem := graphemes.FromString(s[pos:]) |
| 50 | rem.AnsiEscapeSequences = options.ControlSequences |
| 51 | |
| 52 | for rem.Next() { |
| 53 | v := rem.Value() |
| 54 | // Only preserve 7-bit escapes (ESC = 0x1B) that measure |
| 55 | // as zero-width on their own; some sequences (e.g. SOS) |
| 56 | // are only valid in their original context. |
| 57 | if len(v) > 0 && v[0] == 0x1B && options.String(v) == 0 { |
| 58 | b.WriteString(v) |
| 59 | } |
| 60 | } |
| 61 | return b.String() |
| 62 | } |
| 63 | return s[:pos] + tail |
| 64 | } |
| 65 | } |
| 66 | // No truncation |
| 67 | return s |
| 68 | } |
| 69 | |
| 70 | // TruncateString truncates a string to the given maxWidth, and appends the |
| 71 | // given tail if the string is truncated. |
| 72 | // |
| 73 | // It ensures the total width, including the width of the tail, is less than or |
| 74 | // equal to maxWidth. |
| 75 | func TruncateString(s string, maxWidth int, tail string) string { |
| 76 | return DefaultOptions.TruncateString(s, maxWidth, tail) |
| 77 | } |
| 78 | |
| 79 | // TruncateBytes truncates a []byte to the given maxWidth, and appends the |
| 80 | // given tail if the []byte is truncated. |
| 81 | // |
| 82 | // It ensures the visible width, including the width of the tail, is less than or |
| 83 | // equal to maxWidth. |
| 84 | // |
| 85 | // When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that |
| 86 | // appear after the truncation point are preserved in the output. This ensures |
| 87 | // that escape sequences such as SGR resets are not lost, preventing color |
| 88 | // bleed in terminal output. |
| 89 | // |
| 90 | // [Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values |
| 91 | // (0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them |
| 92 | // during truncation can shift byte boundaries and form unintended visible |
| 93 | // characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width |
| 94 | // measurement. |
| 95 | func (options Options) TruncateBytes(s []byte, maxWidth int, tail []byte) []byte { |
| 96 | // We deliberately ignore ControlSequences8Bit for truncation, see above. |
| 97 | options.ControlSequences8Bit = false |
| 98 | |
| 99 | maxWidthWithoutTail := maxWidth - options.Bytes(tail) |
| 100 | |
| 101 | var pos, total int |
| 102 | g := graphemes.FromBytes(s) |
| 103 | g.AnsiEscapeSequences = options.ControlSequences |
| 104 | |
| 105 | for g.Next() { |
| 106 | gw := graphemeWidth(g.Value(), options) |
| 107 | if total+gw <= maxWidthWithoutTail { |
| 108 | pos = g.End() |
| 109 | } |
| 110 | total += gw |
| 111 | if total > maxWidth { |
| 112 | if options.ControlSequences { |
| 113 | // Build result with trailing 7-bit ANSI escape sequences preserved |
| 114 | result := make([]byte, 0, len(s)+len(tail)) // at most original + tail |
| 115 | result = append(result, s[:pos]...) |
| 116 | result = append(result, tail...) |
| 117 | |
| 118 | rem := graphemes.FromBytes(s[pos:]) |
| 119 | rem.AnsiEscapeSequences = options.ControlSequences |
| 120 | |
| 121 | for rem.Next() { |
| 122 | v := rem.Value() |
| 123 | // Only preserve 7-bit escapes (ESC = 0x1B) that measure |
| 124 | // as zero-width on their own; some sequences (e.g. SOS) |
| 125 | // are only valid in their original context. |
| 126 | if len(v) > 0 && v[0] == 0x1B && options.Bytes(v) == 0 { |
| 127 | result = append(result, v...) |
| 128 | } |
| 129 | } |
| 130 | return result |
| 131 | } |
| 132 | result := make([]byte, 0, pos+len(tail)) |
| 133 | result = append(result, s[:pos]...) |
| 134 | result = append(result, tail...) |
| 135 | return result |
| 136 | } |
| 137 | } |
| 138 | // No truncation |
| 139 | return s |
| 140 | } |
| 141 | |
| 142 | // TruncateBytes truncates a []byte to the given maxWidth, and appends the |
| 143 | // given tail if the []byte is truncated. |
| 144 | // |
| 145 | // It ensures the total width, including the width of the tail, is less than or |
| 146 | // equal to maxWidth. |
| 147 | func TruncateBytes(s []byte, maxWidth int, tail []byte) []byte { |
| 148 | return DefaultOptions.TruncateBytes(s, maxWidth, tail) |
| 149 | } |