a43daef96cf84d90fc26788a85378172e9f5464c

Author
Christian Rocha <christian@rocha.is>
Committer
Christian Rocha <christian@rocha.is>
Date

Message

fix: don't print extra newlines when printing with line numbers

Diff

 1diff --git a/server/middleware.go b/server/middleware.go
 2index e6d6b1bcc186f682c5fad72d2e8080f69431f330..48ca66bc89f90a1ecea4b034d4bb1906aefa21a4 100644
 3--- a/server/middleware.go
 4+++ b/server/middleware.go
 5@@ -151,12 +151,16 @@ func withLineNumber(s string, color bool) string {
 6 	mll := len(fmt.Sprintf("%d", len(lines)))
 7 	for i, l := range lines {
 8 		digit := fmt.Sprintf("%*d", mll, i)
 9-		line := "│"
10+		bar := "│"
11 		if color {
12 			digit = lineDigitStyle.Render(digit)
13-			line = lineBarStyle.Render(line)
14+			bar = lineBarStyle.Render(bar)
15+		}
16+		if i < len(lines)-1 || len(l) != 0 {
17+			// If the final line was a newline we'll get an empty string for
18+			// the final line, so drop the newline altogether.
19+			lines[i] = fmt.Sprintf(" %s %s %s", digit, bar, l)
20 		}
21-		lines[i] = fmt.Sprintf(" %s %s %s", digit, line, l)
22 	}
23 	return strings.Join(lines, "\n")
24 }