package ansiimport ()// Cut the string, without adding any prefix or tail strings. This function is// aware of ANSI escape codes and will not break them, and accounts for// wide-characters (such as East-Asian characters and emojis).// This treats the text as a sequence of graphemes.func ( string, , int) string {returncut(GraphemeWidth, , , )}// CutWc the string, without adding any prefix or tail strings. This function is// aware of ANSI escape codes and will not break them, and accounts for// wide-characters (such as East-Asian characters and emojis).// Note that the [left] parameter is inclusive, while [right] isn't,// which is to say it'll return `[left, right)`.//// This treats the text as a sequence of wide characters and runes.func ( string, , int) string {returncut(WcWidth, , , )}func ( Method, string, , int) string {if <= {return"" } := Truncate := TruncateLeftif == WcWidth { = TruncateWc = TruncateWc }if == 0 {return (, , "") }return ((, , ""), , "")}// Truncate truncates a string to a given length, adding a tail to the end if// the string is longer than the given length. This function is aware of ANSI// escape codes and will not break them, and accounts for wide-characters (such// as East-Asian characters and emojis).// This treats the text as a sequence of graphemes.func ( string, int, string) string {returntruncate(GraphemeWidth, , , )}// TruncateWc truncates a string to a given length, adding a tail to the end if// the string is longer than the given length. This function is aware of ANSI// escape codes and will not break them, and accounts for wide-characters (such// as East-Asian characters and emojis).// This treats the text as a sequence of wide characters and runes.func ( string, int, string) string {returntruncate(WcWidth, , , )}func ( Method, string, int, string) string {if := StringWidth(); <= {return } := StringWidth() -= if < 0 {return"" }varstringvarstrings.Builder := 0 := false := parser.GroundState// initial state := 0// Here we iterate over the bytes of the string and collect printable // characters and runes. We also keep track of the width of the string // in cells. // // Once we reach the given length, we start ignoring characters and only // collect ANSI escape codes until we reach the end of string.for < len() { , := parser.Table.Transition(, [])if == parser.Utf8State {// This action happens when we transition to the Utf8State.varint , = FirstGraphemeCluster([:], )// increment the index by the length of the cluster += len() += // Are we ignoring? Skip to the next byteif {continue }// Is this gonna be too wide? // If so write the tail and stop collecting.if > && ! { = true .WriteString() }if > {continue } .WriteString()// Done collecting, now we're back in the ground state. = parser.GroundStatecontinue }switch {caseparser.PrintAction:// Is this gonna be too wide? // If so write the tail and stop collecting.if >= && ! { = true .WriteString() }// Skip to the next byte if we're ignoringif { ++continue }// collects printable ASCII ++fallthroughcaseparser.ExecuteAction:// execute action will be things like \n, which, if outside the cut, // should be ignored.if { ++continue }fallthroughdefault: .WriteByte([]) ++ }// Transition to the next state. = // Once we reach the given length, we start ignoring runes and write // the tail to the buffer.if > && ! { = true .WriteString() } }return .String()}// TruncateLeft truncates a string from the left side by removing n characters,// adding a prefix to the beginning if the string is longer than n.// This function is aware of ANSI escape codes and will not break them, and// accounts for wide-characters (such as East-Asian characters and emojis).// This treats the text as a sequence of graphemes.func ( string, int, string) string {returntruncateLeft(GraphemeWidth, , , )}// TruncateLeftWc truncates a string from the left side by removing n characters,// adding a prefix to the beginning if the string is longer than n.// This function is aware of ANSI escape codes and will not break them, and// accounts for wide-characters (such as East-Asian characters and emojis).// This treats the text as a sequence of wide characters and runes.func ( string, int, string) string {returntruncateLeft(WcWidth, , , )}func ( Method, string, int, string) string {if <= 0 {return }varstringvarstrings.Builder := 0 := true := parser.GroundState := 0for < len() {if ! { .WriteString([:])break } , := parser.Table.Transition(, [])if == parser.Utf8State {varint , = FirstGraphemeCluster([:], ) += len() += if > && { = false .WriteString() }if > { .WriteString() }if {continue } = parser.GroundStatecontinue }switch {caseparser.PrintAction: ++if > && { = false .WriteString() }if { ++continue }fallthroughcaseparser.ExecuteAction:// execute action will be things like \n, which, if outside the cut, // should be ignored.if { ++continue }fallthroughdefault: .WriteByte([]) ++ } = if > && { = false .WriteString() } }return .String()}// ByteToGraphemeRange takes start and stop byte positions and converts them to// grapheme-aware char positions.// You can use this with [Truncate], [TruncateLeft], and [Cut].func ( string, , int) (, int) { , := 0, 0 := graphemes.FromString()for > {if !.Next() {break } += len(.Value()) += max(1, displaywidth.String(.Value())) } = for > {if !.Next() {break } += len(.Value()) += max(1, displaywidth.String(.Value())) } = return , }
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)