Improved printed of long lines

This commit is contained in:
Laurence Withers 2023-05-13 12:45:29 +01:00
parent b481db737e
commit 333b6dbc27
2 changed files with 61 additions and 43 deletions

View File

@ -37,8 +37,8 @@ func (d *Display) TruncatedMarker() aurora.Value {
return d.a.Magenta("…")
}
func (d *Display) TruncatedBytes(byteCount int) aurora.Value {
return d.a.Faint(fmt.Sprintf("(%d bytes)", byteCount))
func (d *Display) TruncatedChars(charCount int) aurora.Value {
return d.a.Faint(fmt.Sprintf("(%d chars)", charCount))
}
func (d *Display) BadUTF8Char() aurora.Value {

102
file.go
View File

@ -119,56 +119,73 @@ func file(path string, data []byte) {
b.Reset()
fmt.Fprintf(&b, "%4d: ", display.LineNumber(lineNum))
if loc[0] < 128 {
escape(&b, line[0:loc[0]])
} else {
start := loc[0] - 128
for i := 0; i < 5; i++ {
if utf8.RuneStart(line[start]) {
break
}
start++
}
b.WriteString(display.TruncatedBytes(start).String())
b.WriteString(display.TruncatedMarker().String())
escape(&b, line[start:loc[0]])
}
if loc[1]-loc[0] < 128 {
before := line[0:loc[0]]
matched := line[loc[0]:loc[1]]
after := line[loc[1]:]
if utf8.RuneCount(line) < longLine {
b2.Reset()
escape(&b2, line[loc[0]:loc[1]])
escape(&b2, matched)
escape(&b, before)
b.WriteString(display.Match(b2.String()).String())
escape(&b, after)
} else {
n := utf8.RuneCount(before)
if n < 64 {
escape(&b, before)
} else {
var nbytes int
for i := 0; i < 64; i++ {
_, s := utf8.DecodeLastRune(before[:len(before)-nbytes])
nbytes += s
}
b.WriteString(display.TruncatedChars(n - 64).String())
b.WriteString(display.TruncatedMarker().String())
escape(&b, before[len(before)-nbytes:])
}
n = utf8.RuneCount(matched)
if n < 64 {
b2.Reset()
escape(&b2, matched)
b.WriteString(display.Match(b2.String()).String())
} else {
var nbytes int
for i := 0; i < 32; i++ {
_, s := utf8.DecodeRune(matched[nbytes:])
nbytes += s
}
b2.Reset()
escape(&b2, matched[:nbytes])
b.WriteString(display.Match(b2.String()).String())
if loc[1]+128 > len(line) {
escape(&b, line[loc[1]:])
} else {
end := loc[1] + 128
for i := 0; i < 5; i++ {
if utf8.RuneStart(line[end]) {
break
}
end--
}
escape(&b, line[loc[1]:end])
b.WriteString(display.TruncatedBytes(len(line) - end).String())
b.WriteString(display.TruncatedMarker().String())
}
b.WriteString(display.TruncatedChars(n - 64).String())
b.WriteString(display.TruncatedMarker().String())
} else {
end := loc[1]
for i := 0; i < 5; i++ {
if utf8.RuneStart(line[end]) {
break
nbytes = 0
for i := 0; i < 32; i++ {
_, s := utf8.DecodeLastRune(matched[:len(matched)-nbytes])
nbytes += s
}
end--
}
b2.Reset()
escape(&b2, line[loc[0]:end])
escape(&b2, matched[len(matched)-nbytes:])
b.WriteString(display.Match(b2.String()).String())
}
n = utf8.RuneCount(after)
if n < 64 {
escape(&b, after)
} else {
var nbytes int
for i := 0; i < 64; i++ {
_, s := utf8.DecodeRune(after[nbytes:])
nbytes += s
}
escape(&b, after[:nbytes])
b.WriteString(display.TruncatedMarker().String())
b.WriteString(display.TruncatedBytes(len(line) - end).String())
b.WriteString(display.TruncatedChars(n - 64).String())
}
}
@ -202,8 +219,9 @@ func isBinary(data []byte) bool {
return false
}
const longLine = 256
func isMinified(data []byte) bool {
const longLine = 256
bytesToExamine := 4096
var lineLength int