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("…") return d.a.Magenta("…")
} }
func (d *Display) TruncatedBytes(byteCount int) aurora.Value { func (d *Display) TruncatedChars(charCount int) aurora.Value {
return d.a.Faint(fmt.Sprintf("(%d bytes)", byteCount)) return d.a.Faint(fmt.Sprintf("(%d chars)", charCount))
} }
func (d *Display) BadUTF8Char() aurora.Value { func (d *Display) BadUTF8Char() aurora.Value {

100
file.go
View File

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