fa23c9caebf61cc68ad884552601d59ee6c47621

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix(ui): dry glamour chroma renderer

Diff

  1diff --git a/pkg/ssh/cmd/commit.go b/pkg/ssh/cmd/commit.go
  2index 058029dfe43e262b7cc51991aaa9ecce09349a75..4f91b2fed9ef2ca334d266ee4c43b8770ac73a06 100644
  3--- a/pkg/ssh/cmd/commit.go
  4+++ b/pkg/ssh/cmd/commit.go
  5@@ -10,7 +10,6 @@ import (
  6 	"github.com/charmbracelet/soft-serve/pkg/backend"
  7 	"github.com/charmbracelet/soft-serve/pkg/ui/common"
  8 	"github.com/charmbracelet/soft-serve/pkg/ui/styles"
  9-	"github.com/muesli/termenv"
 10 	"github.com/spf13/cobra"
 11 )
 12 
 13@@ -108,13 +107,6 @@ func commitCommand() *cobra.Command {
 14 	return cmd
 15 }
 16 
 17-func renderCtx() gansi.RenderContext {
 18-	return gansi.NewRenderContext(gansi.Options{
 19-		ColorProfile: termenv.TrueColor,
 20-		Styles:       common.StyleConfig(),
 21-	})
 22-}
 23-
 24 func renderDiff(patch string, color bool) string {
 25 	c := patch
 26 
 27@@ -127,7 +119,7 @@ func renderDiff(patch string, color bool) string {
 28 			Language: "diff",
 29 		}
 30 
 31-		err := diffChroma.Render(&pr, renderCtx())
 32+		err := diffChroma.Render(&pr, common.StyleRenderer())
 33 
 34 		if err != nil {
 35 			s.WriteString(fmt.Sprintf("\n%s", err.Error()))
 36diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go
 37index f56ccbf6e0a4c8ee483d0a09a9396202985904f0..225deb9b49b4bfc23ff3785688d2420d34ac1e8b 100644
 38--- a/pkg/ssh/ssh.go
 39+++ b/pkg/ssh/ssh.go
 40@@ -15,11 +15,11 @@ import (
 41 	"github.com/charmbracelet/soft-serve/pkg/db"
 42 	"github.com/charmbracelet/soft-serve/pkg/proto"
 43 	"github.com/charmbracelet/soft-serve/pkg/store"
 44+	"github.com/charmbracelet/soft-serve/pkg/ui/common"
 45 	"github.com/charmbracelet/ssh"
 46 	"github.com/charmbracelet/wish"
 47 	bm "github.com/charmbracelet/wish/bubbletea"
 48 	rm "github.com/charmbracelet/wish/recover"
 49-	"github.com/muesli/termenv"
 50 	"github.com/prometheus/client_golang/prometheus"
 51 	"github.com/prometheus/client_golang/prometheus/promauto"
 52 	gossh "golang.org/x/crypto/ssh"
 53@@ -70,7 +70,7 @@ func NewSSHServer(ctx context.Context) (*SSHServer, error) {
 54 		rm.MiddlewareWithLogger(
 55 			logger,
 56 			// BubbleTea middleware.
 57-			bm.MiddlewareWithProgramHandler(SessionHandler, termenv.ANSI256),
 58+			bm.MiddlewareWithProgramHandler(SessionHandler, common.DefaultColorProfile),
 59 			// CLI middleware.
 60 			CommandMiddleware,
 61 			// Logging middleware.
 62diff --git a/pkg/ui/common/format.go b/pkg/ui/common/format.go
 63index 9649eb03a5bb9a9d980140757620d5f8c63880f8..96bae8bd34b5be65306dd920eb2a917ea257c6a9 100644
 64--- a/pkg/ui/common/format.go
 65+++ b/pkg/ui/common/format.go
 66@@ -7,7 +7,6 @@ import (
 67 	"github.com/alecthomas/chroma/lexers"
 68 	gansi "github.com/charmbracelet/glamour/ansi"
 69 	"github.com/charmbracelet/soft-serve/pkg/ui/styles"
 70-	"github.com/muesli/termenv"
 71 )
 72 
 73 // FormatLineNumber adds line numbers to a string.
 74@@ -48,10 +47,7 @@ func FormatHighlight(p, c string) (string, error) {
 75 	r := strings.Builder{}
 76 	styles := StyleConfig()
 77 	styles.CodeBlock.Margin = &zero
 78-	rctx := gansi.NewRenderContext(gansi.Options{
 79-		Styles:       styles,
 80-		ColorProfile: termenv.TrueColor,
 81-	})
 82+	rctx := StyleRendererWithStyles(styles)
 83 	err := formatter.Render(&r, rctx)
 84 	if err != nil {
 85 		return "", err
 86diff --git a/pkg/ui/common/style.go b/pkg/ui/common/style.go
 87index 8b91d9afa08df6466414edaae1bb7da6ad686956..cdb9ed3dcf91a47ef9f691000ebd095d9580c5cf 100644
 88--- a/pkg/ui/common/style.go
 89+++ b/pkg/ui/common/style.go
 90@@ -3,6 +3,12 @@ package common
 91 import (
 92 	"github.com/charmbracelet/glamour"
 93 	gansi "github.com/charmbracelet/glamour/ansi"
 94+	"github.com/muesli/termenv"
 95+)
 96+
 97+var (
 98+	// DefaultColorProfile is the default color profile used by the SSH server.
 99+	DefaultColorProfile = termenv.ANSI256
100 )
101 
102 func strptr(s string) *string {
103@@ -25,3 +31,17 @@ func StyleConfig() gansi.StyleConfig {
104 	s.CodeBlock.Chroma.Error.BackgroundColor = noColor
105 	return s
106 }
107+
108+// StyleRenderer returns a new Glamour renderer with the DefaultColorProfile.
109+func StyleRenderer() gansi.RenderContext {
110+	return StyleRendererWithStyles(StyleConfig())
111+}
112+
113+// StyleRendererWithStyles returns a new Glamour renderer with the
114+// DefaultColorProfile and styles.
115+func StyleRendererWithStyles(styles gansi.StyleConfig) gansi.RenderContext {
116+	return gansi.NewRenderContext(gansi.Options{
117+		ColorProfile: DefaultColorProfile,
118+		Styles:       styles,
119+	})
120+}
121diff --git a/pkg/ui/components/code/code.go b/pkg/ui/components/code/code.go
122index 56c580345ff5cdaf4ef39cd5cce54cc79f337498..d90ed7e0e5138d43ecbaaacf625380e7d409805f 100644
123--- a/pkg/ui/components/code/code.go
124+++ b/pkg/ui/components/code/code.go
125@@ -51,10 +51,7 @@ func New(c common.Common, content, extension string) *Code {
126 	}
127 	st := common.StyleConfig()
128 	r.styleConfig = st
129-	r.renderContext = gansi.NewRenderContext(gansi.Options{
130-		ColorProfile: termenv.TrueColor,
131-		Styles:       st,
132-	})
133+	r.renderContext = common.StyleRendererWithStyles(st)
134 	r.SetSize(c.Width, c.Height)
135 	return r
136 }
137diff --git a/pkg/ui/pages/repo/log.go b/pkg/ui/pages/repo/log.go
138index ef4e5589e8f7ad2306b0cf001c2bf93d00746218..292128d961f11c5e6f76b1599de912dba645e192 100644
139--- a/pkg/ui/pages/repo/log.go
140+++ b/pkg/ui/pages/repo/log.go
141@@ -18,7 +18,6 @@ import (
142 	"github.com/charmbracelet/soft-serve/pkg/ui/components/viewport"
143 	"github.com/charmbracelet/soft-serve/pkg/ui/styles"
144 	"github.com/muesli/reflow/wrap"
145-	"github.com/muesli/termenv"
146 )
147 
148 var waitBeforeLoading = time.Millisecond * 100
149@@ -476,13 +475,6 @@ func (l *Log) loadDiffCmd() tea.Msg {
150 	return LogDiffMsg(diff)
151 }
152 
153-func renderCtx() gansi.RenderContext {
154-	return gansi.NewRenderContext(gansi.Options{
155-		ColorProfile: termenv.TrueColor,
156-		Styles:       common.StyleConfig(),
157-	})
158-}
159-
160 func (l *Log) renderCommit(c *git.Commit) string {
161 	s := strings.Builder{}
162 	// FIXME: lipgloss prints empty lines when CRLF is used
163@@ -518,7 +510,7 @@ func renderDiff(diff *git.Diff, width int) string {
164 		Code:     diff.Patch(),
165 		Language: "diff",
166 	}
167-	err := diffChroma.Render(&pr, renderCtx())
168+	err := diffChroma.Render(&pr, common.StyleRenderer())
169 	if err != nil {
170 		s.WriteString(fmt.Sprintf("\n%s", err.Error()))
171 	} else {