package ansiimport ()// nbsp is a non-breaking space.constnbsp = 0xA0// Hardwrap wraps a string or a block of text to a given line length, breaking// word boundaries. This will preserve ANSI escape codes and will account for// wide-characters in the string.// When preserveSpace is true, spaces at the beginning of a line will be// preserved.// This treats the text as a sequence of graphemes.func ( string, int, bool) string {returnhardwrap(GraphemeWidth, , , )}// HardwrapWc wraps a string or a block of text to a given line length, breaking// word boundaries. This will preserve ANSI escape codes and will account for// wide-characters in the string.// When preserveSpace is true, spaces at the beginning of a line will be// preserved.// This treats the text as a sequence of wide characters and runes.func ( string, int, bool) string {returnhardwrap(WcWidth, , , )}func ( Method, string, int, bool) string {if < 1 {return }var ( []bytebytes.Bufferintbool = parser.GroundState// initial state = []byte() ) := func() { .WriteByte('\n') = 0 } := 0for < len() { , := parser.Table.Transition(, [])if == parser.Utf8State {varint , = FirstGraphemeCluster([:], ) += len()if + > { () }if ! && == 0 && len() <= 4 {// Skip spaces at the beginning of a lineif , := utf8.DecodeRune(); != utf8.RuneError && unicode.IsSpace() { = parser.GroundStatecontinue } } .Write() += = parser.GroundStatecontinue }switch {caseparser.PrintAction, parser.ExecuteAction:if [] == '\n' { () = falsebreak }if +1 > { () = true }// Skip spaces at the beginning of a lineif == 0 {if ! && && unicode.IsSpace(rune([])) {break } = false } .WriteByte([])if == parser.PrintAction { ++ }default: .WriteByte([]) }// We manage the UTF8 state separately manually above.if != parser.Utf8State { = } ++ }return .String()}// Wordwrap wraps a string or a block of text to a given line length, not// breaking word boundaries. This will preserve ANSI escape codes and will// account for wide-characters in the string.// The breakpoints string is a list of characters that are considered// breakpoints for word wrapping. A hyphen (-) is always considered a// breakpoint.//// Note: breakpoints must be a string of 1-cell wide rune characters.//// This treats the text as a sequence of graphemes.func ( string, int, string) string {returnwordwrap(GraphemeWidth, , , )}// WordwrapWc wraps a string or a block of text to a given line length, not// breaking word boundaries. This will preserve ANSI escape codes and will// account for wide-characters in the string.// The breakpoints string is a list of characters that are considered// breakpoints for word wrapping. A hyphen (-) is always considered a// breakpoint.//// Note: breakpoints must be a string of 1-cell wide rune characters.//// This treats the text as a sequence of wide characters and runes.func ( string, int, string) string {returnwordwrap(WcWidth, , , )}func ( Method, string, int, string) string {if < 1 {return }var ( []bytebytes.Bufferbytes.Bufferbytes.Bufferintint = parser.GroundState// initial state = []byte() ) := func() { += .Len() .Write(.Bytes()) .Reset() } := func() {if .Len() == 0 {return } () += .Write(.Bytes()) .Reset() = 0 } := func() { .WriteByte('\n') = 0 .Reset() } := 0for < len() { , := parser.Table.Transition(, [])if == parser.Utf8State { //nolint:nestifvarint , = FirstGraphemeCluster([:], ) += len() , := utf8.DecodeRune()if != utf8.RuneError && unicode.IsSpace() && != nbsp { () .WriteRune() } elseifbytes.ContainsAny(, ) { () () .Write() ++ } else { .Write() += if +.Len()+ > && < { () } } = parser.GroundStatecontinue }switch {caseparser.PrintAction, parser.ExecuteAction: := rune([])switch {case == '\n':if == 0 {if +.Len() > { = 0 } else { .Write(.Bytes()) } .Reset() } () ()caseunicode.IsSpace(): () .WriteByte([])case == '-':fallthroughcaseruneContainsAny(, ): () () .WriteByte([]) ++default: .WriteByte([]) ++if +.Len()+ > && < { () } }default: .WriteByte([]) }// We manage the UTF8 state separately manually above.if != parser.Utf8State { = } ++ } ()return .String()}// Wrap wraps a string or a block of text to a given line length, breaking word// boundaries if necessary. This will preserve ANSI escape codes and will// account for wide-characters in the string. The breakpoints string is a list// of characters that are considered breakpoints for word wrapping. A hyphen// (-) is always considered a breakpoint.//// Note: breakpoints must be a string of 1-cell wide rune characters.//// This treats the text as a sequence of graphemes.func ( string, int, string) string {returnwrap(GraphemeWidth, , , )}// WrapWc wraps a string or a block of text to a given line length, breaking word// boundaries if necessary. This will preserve ANSI escape codes and will// account for wide-characters in the string. The breakpoints string is a list// of characters that are considered breakpoints for word wrapping. A hyphen// (-) is always considered a breakpoint.//// Note: breakpoints must be a string of 1-cell wide rune characters.//// This treats the text as a sequence of wide characters and runes.func ( string, int, string) string {returnwrap(WcWidth, , , )}func ( Method, string, int, string) string {if < 1 {return }var (stringbytes.Bufferbytes.Bufferbytes.Bufferint// width of the space bufferint// written width of the lineint// word buffer len without ANSI escape codes = parser.GroundState// initial state ) := func() {if == 0 && .Len() == 0 {return } += .Write(.Bytes()) .Reset() = 0 } := func() {if .Len() == 0 {return } () += .Write(.Bytes()) .Reset() = 0 } := func() { .WriteByte('\n') = 0 .Reset() = 0 } := 0for < len() { , := parser.Table.Transition(, [])if == parser.Utf8State { //nolint:nestifvarint , = FirstGraphemeCluster([:], ) += len() , := utf8.DecodeRuneInString()switch {case != utf8.RuneError && unicode.IsSpace() && != nbsp: // nbsp is a non-breaking space () .WriteRune() += casestrings.ContainsAny(, ): ()if ++ > { .WriteString() += } else { () .WriteString() += }default:if + > {// Hardwrap the word if it's too long () } .WriteString() += if ++ > { () }if == {// Hardwrap the word if it's too long () } } = parser.GroundStatecontinue }switch {caseparser.PrintAction, parser.ExecuteAction:switch := rune([]); {case == '\n':if == 0 {if + > { = 0 } else {// preserve whitespaces .Write(.Bytes()) } .Reset() = 0 } () ()caseunicode.IsSpace(): () .WriteRune() ++case == '-':fallthroughcaseruneContainsAny(, ): ()if + >= {// We can't fit the breakpoint in the current line, treat // it as part of the word. .WriteRune() ++ } else { () .WriteRune() ++ }default:if == { () } .WriteRune() ++if == {// Hardwrap the word if it's too long () }if ++ > { () } }default: .WriteByte([]) }// We manage the UTF8 state separately manually above.if != parser.Utf8State { = } ++ }if == 0 {if + > { = 0 } else {// preserve whitespaces .Write(.Bytes()) } .Reset() = 0 } ()return .String()}func ( rune, string) bool {for , := range {if == {returntrue } }returnfalse}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)