From 333b6dbc27b811c72216ac1f9483d40e8813b453 Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Sat, 13 May 2023 12:45:29 +0100 Subject: [PATCH] Improved printed of long lines --- display.go | 4 +-- file.go | 100 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/display.go b/display.go index f7b1d9e..ca52888 100644 --- a/display.go +++ b/display.go @@ -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 { diff --git a/file.go b/file.go index 4ef63cb..a3311d0 100644 --- a/file.go +++ b/file.go @@ -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]]) - b.WriteString(display.Match(b2.String()).String()) + escape(&b2, matched) - if loc[1]+128 > len(line) { - escape(&b, line[loc[1]:]) + 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 { - end := loc[1] + 128 - for i := 0; i < 5; i++ { - if utf8.RuneStart(line[end]) { - break - } - end-- + var nbytes int + for i := 0; i < 64; i++ { + _, s := utf8.DecodeLastRune(before[:len(before)-nbytes]) + nbytes += s } - escape(&b, line[loc[1]:end]) - b.WriteString(display.TruncatedBytes(len(line) - end).String()) + b.WriteString(display.TruncatedChars(n - 64).String()) b.WriteString(display.TruncatedMarker().String()) + escape(&b, before[len(before)-nbytes:]) } - } else { - end := loc[1] - for i := 0; i < 5; i++ { - if utf8.RuneStart(line[end]) { - break + 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 } - 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() - escape(&b2, line[loc[0]:end]) - b.WriteString(display.Match(b2.String()).String()) - b.WriteString(display.TruncatedMarker().String()) - b.WriteString(display.TruncatedBytes(len(line) - end).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.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