MCPcopy Create free account
hub / github.com/goadesign/goa / WrapText

Function WrapText

codegen/funcs.go:257–281  ·  view source on GitHub ↗

WrapText produces lines with text capped at maxChars it will keep words intact and respects newlines.

(text string, maxChars int)

Source from the content-addressed store, hash-verified

255// WrapText produces lines with text capped at maxChars
256// it will keep words intact and respects newlines.
257func 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.

Callers 2

CommentFunction · 0.85
TestWrapTextFunction · 0.85

Calls 2

runeSpacePosRevFunction · 0.85
runeSpacePosFunction · 0.85

Tested by 1

TestWrapTextFunction · 0.68