// trimToCells trims the provided text so that it fits the specified amount of cells.
108
-
func trimToCells(text string, maxCells int, om OverrunMode) string {
107
+
// TrimText trims the provided text so that it fits the specified amount of cells.
108
+
func TrimText(text string, maxCells int, om OverrunMode) (string,error) {
109
109
if maxCells < 1 {
110
-
return ""
110
+
return "", fmt.Errorf("maxCells(%d) cannot be less than one", maxCells)
111
+
}
112
+
113
+
textCells := runewidth.StringWidth(text)
114
+
if textCells <= maxCells {
115
+
// Nothing to do if the text fits.
116
+
return text, nil
117
+
}
118
+
119
+
switch om {
120
+
case OverrunModeStrict:
121
+
return "", fmt.Errorf("the requested text %q takes %d cells to draw, space is available for only %d cells and overrun mode is %v", text, textCells, maxCells, om)
// Only write the rune if it still fits, i.e. don't cut
135
+
// full-width runes in half.
136
+
if cur+rw == maxCells {
137
+
b.WriteRune(r)
138
+
}
121
139
case om == OverrunModeThreeDot:
122
140
b.WriteRune('…')
123
141
}
124
142
break
125
143
}
126
-
b.WriteRune(r)
127
-
cells += rw
128
-
}
129
-
return b.String()
130
-
}
131
144
132
-
// bounds enforces the text bounds based on the specified overrun mode.
133
-
// Returns test that can be safely drawn within the bounds.
134
-
func bounds(text string, maxCells int, om OverrunMode) (string, error) {
135
-
cells := runewidth.StringWidth(text)
136
-
if cells <= maxCells {
137
-
return text, nil
138
-
}
139
-
140
-
switch om {
141
-
case OverrunModeStrict:
142
-
return "", fmt.Errorf("the requested text %q takes %d cells to draw, space is available for only %d cells and overrun mode is %v", text, cells, maxCells, om)