MCPcopy
hub / github.com/spf13/pflag / wrap

Function wrap

flag.go:658–702  ·  view source on GitHub ↗

Wraps the string `s` to a maximum width `w` with leading indent `i`. The first line is not indented (this is assumed to be done by caller). Pass `w` == 0 to do no wrapping

(i, w int, s string)

Source from the content-addressed store, hash-verified

656// `i`. The first line is not indented (this is assumed to be done by
657// caller). Pass `w` == 0 to do no wrapping
658func wrap(i, w int, s string) string {
659 if w == 0 {
660 return strings.Replace(s, "\n", "\n"+strings.Repeat(" ", i), -1)
661 }
662
663 // space between indent i and end of line width w into which
664 // we should wrap the text.
665 wrap := w - i
666
667 var r, l string
668
669 // Not enough space for sensible wrapping. Wrap as a block on
670 // the next line instead.
671 if wrap < 24 {
672 i = 16
673 wrap = w - i
674 r += "\n" + strings.Repeat(" ", i)
675 }
676 // If still not enough space then don't even try to wrap.
677 if wrap < 24 {
678 return strings.Replace(s, "\n", r, -1)
679 }
680
681 // Try to avoid short orphan words on the final line, by
682 // allowing wrapN to go a bit over if that would fit in the
683 // remainder of the line.
684 slop := 5
685 wrap = wrap - slop
686
687 // Handle first line, which is indented by the caller (or the
688 // special case above)
689 l, s = wrapN(wrap, slop, s)
690 r = r + strings.Replace(l, "\n", "\n"+strings.Repeat(" ", i), -1)
691
692 // Now wrap the rest
693 for s != "" {
694 var t string
695
696 t, s = wrapN(wrap, slop, s)
697 r = r + "\n" + strings.Repeat(" ", i) + strings.Replace(t, "\n", "\n"+strings.Repeat(" ", i), -1)
698 }
699
700 return r
701
702}
703
704// FlagUsagesWrapped returns a string containing the usage information
705// for all flags in the FlagSet. Wrapped to `cols` columns (0 for no

Callers 1

FlagUsagesWrappedMethod · 0.85

Calls 2

wrapNFunction · 0.85
ReplaceMethod · 0.65

Tested by

no test coverage detected