From 91a7412d1f4c307f1f799e0ec4d94647067c2f03 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 1 Jun 2022 17:10:49 -0400 Subject: [PATCH] fix(ui): styling after line breaks --- go.mod | 2 +- go.sum | 3 ++- ui/components/code/code.go | 18 +++++++----------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 678babb8b8975f10ce6543f23de2b65526859eed..c5540e378d46acd65a6ef479bf32d796eca73c2c 100755 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 253ee4e7e0d6f2e76dbb38300517f0495b36e028..76bc068077fec221b3e37cd9daf2b19647b47982 100644 --- a/go.sum +++ b/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= diff --git a/ui/components/code/code.go b/ui/components/code/code.go index c25f462ad7adcb53c72097788451aab54d9d4c5c..1b653c638f7ee2d8e99c80ec2548f95c12955b3e 100644 --- a/ui/components/code/code.go +++ b/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 }