Skip to content

Commit

Permalink
Clear truncate stuff in ansi
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyonlin authored and muesli committed Nov 3, 2020
1 parent d5f84d7 commit d06e047
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 121 deletions.
69 changes: 11 additions & 58 deletions ansi/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,65 +23,18 @@ func PrintableRuneWidth(s string) int {
var ansi bool

for _, c := range s {
accPrintableRuneWidth(c, &n, &ansi)
}

return n
}

// Truncate truncates a string at the given printable cell width, leaving any
// ansi sequences intact.
func Truncate(s string, w int) string {
return TruncateWithTail(s, w, "")
}

// TruncateWithTail truncates a string at the given printable cell width,
// leaving any ansi sequences intact. A tail is then added to the end of the
// string.
func TruncateWithTail(s string, w int, tail string) string {
if PrintableRuneWidth(s) <= w {
return s
}

const ansiReset = "\x1B[0m"

if tail != "" {
tail += ansiReset
}

tw := PrintableRuneWidth(tail)
w -= tw
if w < 0 {
return tail
}

r := []rune(s)
ansi := false
n := 0
i := 0

for ; i < len(r); i++ {
accPrintableRuneWidth(r[i], &n, &ansi)
if n > w {
break
if c == '\x1B' {
// ANSI escape sequence
ansi = true
} else if ansi {
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated
ansi = false
}
} else {
n += runewidth.RuneWidth(c)
}
}

return string(r[0:i]) + ansiReset + tail
}

// Used to accumulate the printable rune width while tracking whether we're in
// an ansi sequence.
func accPrintableRuneWidth(c rune, n *int, ansi *bool) {
if c == '\x1B' {
// ANSI escape sequence
*ansi = true
} else if *ansi {
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated
*ansi = false
}
} else {
*n += runewidth.RuneWidth(c)
}
return n
}
63 changes: 0 additions & 63 deletions ansi/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansi

import (
"bytes"
"fmt"
"testing"
)

Expand Down Expand Up @@ -30,65 +29,3 @@ func Benchmark_PrintableRuneWidth(b *testing.B) {
}
})
}

func Test_Truncate(t *testing.T) {
t.Parallel()

tests := []struct {
in string
out string
width int
expectedWidth int
}{
{
"\x1B[38;2;249;38;114m你\x1B[7m好\x1B[0m",
"\x1B[38;2;249;38;114m你\x1B[7m\x1B[0m",
2,
2,
},
{
"\x1B[38;2;249;38;114m你\x1B[7m好\x1B[0m",
"\x1B[38;2;249;38;114m\x1B[0m",
1,
0,
},
{
"It’s me!",
"It’s me!",
10,
8,
},
{
"It’s \x1B[7mme!",
"It’s \x1B[7m\x1B[0m",
5,
5,
},
}

for i, tt := range tests {
t.Run(fmt.Sprintf("truncate-%d", i), func(t *testing.T) {
t.Parallel()
res := Truncate(tt.in, tt.width)
if n := PrintableRuneWidth(res); n != tt.expectedWidth {
t.Fatalf("width should be %d, got %d", tt.expectedWidth, n)
}
if res != tt.out {
t.Fatalf("expected '%s' got '%s'\x1B[0m", tt.out, res)
}
})
}
}

// go test -bench=Benchmark_Truncate -benchmem -count=4
func Benchmark_Truncate(b *testing.B) {
s := "\x1B[38;2;249;38;114m你\x1B[7m好\x1B[0m"

b.RunParallel(func(pb *testing.PB) {
b.ReportAllocs()
b.ResetTimer()
for pb.Next() {
Truncate(s, 2)
}
})
}

0 comments on commit d06e047

Please sign in to comment.