package ansiimport ()// Strip removes ANSI escape codes from a string.func ( string) string {var (bytes.Buffer// buffer for collecting printable charactersint// rune indexint// rune width = parser.GroundState// initial state )// This implements a subset of the Parser to only collect runes and // printable characters.for := rangelen() {if == parser.Utf8State {// During this state, collect rw bytes to form a valid rune in the // buffer. After getting all the rune bytes into the buffer, // transition to GroundState and reset the counters. .WriteByte([]) ++if < {continue } = parser.GroundState = 0 = 0continue } , := parser.Table.Transition(, [])switch {caseparser.CollectAction:if == parser.Utf8State {// This action happens when we transition to the Utf8State. = utf8ByteLen([]) .WriteByte([]) ++ }caseparser.PrintAction, parser.ExecuteAction:// collects printable ASCII and non-printable characters .WriteByte([]) }// Transition to the next state. // The Utf8State is managed separately above.if != parser.Utf8State { = } }return .String()}// StringWidth returns the width of a string in cells. This is the number of// cells that the string will occupy when printed in a terminal. ANSI escape// codes are ignored and wide characters (such as East Asians and emojis) are// accounted for.// This treats the text as a sequence of grapheme clusters.func ( string) int {returnstringWidth(GraphemeWidth, )}// StringWidthWc returns the width of a string in cells. This is the number of// cells that the string will occupy when printed in a terminal. ANSI escape// codes are ignored and wide characters (such as East Asians and emojis) are// accounted for.// This treats the text as a sequence of wide characters and runes.func ( string) int {returnstringWidth(WcWidth, )}func ( Method, string) int {if == "" {return0 }var ( = parser.GroundState// initial stateint )for := 0; < len(); ++ { , := parser.Table.Transition(, [])if == parser.PrintAction || == parser.Utf8State {if == WcWidth {// Wcwidth sums codepoints, so where the cluster boundaries // fall makes no difference to the total. Skipping the // segmentation is a good deal cheaper. , := utf8.DecodeRuneInString([:]) += wcRuneWidth() += - 1 = parser.GroundStatecontinue } , := FirstGraphemeCluster([:], ) += += len() - 1 = parser.GroundStatecontinue }if == parser.PrintAction { ++ } = }return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)