WrapText produces lines with text capped at maxChars it will keep words intact and respects newlines.
(text string, maxChars int)
| 255 | // WrapText produces lines with text capped at maxChars |
| 256 | // it will keep words intact and respects newlines. |
| 257 | func WrapText(text string, maxChars int) string { |
| 258 | res := "" |
| 259 | lines := strings.Split(text, "\n") |
| 260 | for _, v := range lines { |
| 261 | runes := []rune(strings.TrimSpace(v)) |
| 262 | for l := len(runes); l >= 0; l = len(runes) { |
| 263 | if maxChars >= l { |
| 264 | res = res + string(runes) + "\n" |
| 265 | break |
| 266 | } |
| 267 | |
| 268 | i := runeSpacePosRev(runes[:maxChars]) |
| 269 | if i == 0 { |
| 270 | i = runeSpacePos(runes) |
| 271 | } |
| 272 | |
| 273 | res = res + string(runes[:i]) + "\n" |
| 274 | if l == i { |
| 275 | break |
| 276 | } |
| 277 | runes = runes[i+1:] |
| 278 | } |
| 279 | } |
| 280 | return res[:len(res)-1] |
| 281 | } |
| 282 | |
| 283 | // InitStructFields produces Go code to initialize a struct and its fields from |
| 284 | // the given init arguments. |