fix(ui): styling after line breaks

Ayman Bagabas created

Change summary

go.mod                     |  2 +-
go.sum                     |  3 ++-
ui/components/code/code.go | 18 +++++++-----------
3 files changed, 10 insertions(+), 13 deletions(-)

Detailed changes

go.mod 🔗

@@ -66,6 +66,6 @@ require (
 	github.com/yuin/goldmark-emoji v1.0.1 // indirect
 	golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
 	golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
-	golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
+	golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
 	gopkg.in/warnings.v0 v0.1.2 // indirect
 )

go.sum 🔗

@@ -201,8 +201,9 @@ golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1ch
 golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8=
+golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=

ui/components/code/code.go 🔗

@@ -12,7 +12,6 @@ import (
 	"github.com/charmbracelet/lipgloss"
 	"github.com/charmbracelet/soft-serve/ui/common"
 	vp "github.com/charmbracelet/soft-serve/ui/components/viewport"
-	"github.com/muesli/reflow/wrap"
 	"github.com/muesli/termenv"
 )
 
@@ -227,18 +226,15 @@ func (r *Code) renderFile(path, content string, width int) (string, error) {
 	}
 	c := s.String()
 	if r.showLineNumber {
-		c = withLineNumber(c)
+		var ml int
+		c, ml = withLineNumber(c)
+		width -= ml
 	}
-	// FIXME: this is a hack to reset formatting at the end of every line.
-	c = wrap.String(c, width)
-	f := strings.Split(c, "\n")
-	for i, l := range f {
-		f[i] = l + "\x1b[0m"
-	}
-	return strings.Join(f, "\n"), nil
+	// fix styling when after line breaks.
+	return lipgloss.NewStyle().Width(width).Render(c), nil
 }
 
-func withLineNumber(s string) string {
+func withLineNumber(s string) (string, int) {
 	lines := strings.Split(s, "\n")
 	// NB: len() is not a particularly safe way to count string width (because
 	// it's counting bytes instead of runes) but in this case it's okay
@@ -255,5 +251,5 @@ func withLineNumber(s string) string {
 			lines[i] = fmt.Sprintf(" %s %s %s", digit, bar, l)
 		}
 	}
-	return strings.Join(lines, "\n")
+	return strings.Join(lines, "\n"), mll
 }