diff options
| author | Paul Buetow <paul@buetow.org> | 2021-11-02 08:11:40 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-11-02 08:11:40 +0200 |
| commit | 1ec88deea93047a9d1a366e032b2a54aa3cd362b (patch) | |
| tree | 465f30673aa097a2b369e7c3d2032b041ff4f8e6 /internal/color | |
| parent | 2e9ce81c47d45dd1f2c607df6e19bdfdc3bb3cc8 (diff) | |
Bugfix: Dealing correctly with files without newline characters, also add more tests
Diffstat (limited to 'internal/color')
| -rw-r--r-- | internal/color/paint.go | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/internal/color/paint.go b/internal/color/paint.go index 7735d87..4c9d2bc 100644 --- a/internal/color/paint.go +++ b/internal/color/paint.go @@ -39,9 +39,13 @@ func PaintStrAttr(text string, attr Attribute) string { func Paint(sb *strings.Builder, text string, fg FgColor, bg BgColor) { sb.WriteString(string(fg)) sb.WriteString(string(bg)) - sb.WriteString(text) + trimmed := strings.TrimSuffix(text, "\n") + sb.WriteString(trimmed) sb.WriteString(string(BgDefault)) sb.WriteString(string(FgDefault)) + if trimmed != text { + sb.WriteByte('\n') + } } // Reset background and foreground colors. @@ -62,10 +66,14 @@ func PaintWithAttr(sb *strings.Builder, text string, fg FgColor, bg BgColor, sb.WriteString(string(fg)) sb.WriteString(string(bg)) sb.WriteString(string(attr)) - sb.WriteString(text) + trimmed := strings.TrimSuffix(text, "\n") + sb.WriteString(trimmed) sb.WriteString(string(AttrReset)) sb.WriteString(string(BgDefault)) sb.WriteString(string(FgDefault)) + if trimmed != text { + sb.WriteByte('\n') + } } // PaintWithAttrs is similar to PaintWithAttr, but it takes multiple attributes. @@ -77,10 +85,14 @@ func PaintWithAttrs(sb *strings.Builder, text string, fg FgColor, bg BgColor, for _, attr := range attrs { sb.WriteString(string(attr)) } - sb.WriteString(text) + trimmed := strings.TrimSuffix(text, "\n") + sb.WriteString(trimmed) sb.WriteString(string(AttrReset)) sb.WriteString(string(BgDefault)) sb.WriteString(string(FgDefault)) + if trimmed != text { + sb.WriteByte('\n') + } } // ResetWithAttr resets background, foreground and attributes. |
