From fa23c9caebf61cc68ad884552601d59ee6c47621 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 5 Dec 2023 12:46:51 -0500 Subject: [PATCH] fix(ui): dry glamour chroma renderer --- pkg/ssh/cmd/commit.go | 10 +--------- pkg/ssh/ssh.go | 4 ++-- pkg/ui/common/format.go | 6 +----- pkg/ui/common/style.go | 20 ++++++++++++++++++++ pkg/ui/components/code/code.go | 5 +---- pkg/ui/pages/repo/log.go | 10 +--------- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/pkg/ssh/cmd/commit.go b/pkg/ssh/cmd/commit.go index 058029dfe43e262b7cc51991aaa9ecce09349a75..4f91b2fed9ef2ca334d266ee4c43b8770ac73a06 100644 --- a/pkg/ssh/cmd/commit.go +++ b/pkg/ssh/cmd/commit.go @@ -10,7 +10,6 @@ import ( "github.com/charmbracelet/soft-serve/pkg/backend" "github.com/charmbracelet/soft-serve/pkg/ui/common" "github.com/charmbracelet/soft-serve/pkg/ui/styles" - "github.com/muesli/termenv" "github.com/spf13/cobra" ) @@ -108,13 +107,6 @@ func commitCommand() *cobra.Command { return cmd } -func renderCtx() gansi.RenderContext { - return gansi.NewRenderContext(gansi.Options{ - ColorProfile: termenv.TrueColor, - Styles: common.StyleConfig(), - }) -} - func renderDiff(patch string, color bool) string { c := patch @@ -127,7 +119,7 @@ func renderDiff(patch string, color bool) string { Language: "diff", } - err := diffChroma.Render(&pr, renderCtx()) + err := diffChroma.Render(&pr, common.StyleRenderer()) if err != nil { s.WriteString(fmt.Sprintf("\n%s", err.Error())) diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index f56ccbf6e0a4c8ee483d0a09a9396202985904f0..225deb9b49b4bfc23ff3785688d2420d34ac1e8b 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -15,11 +15,11 @@ import ( "github.com/charmbracelet/soft-serve/pkg/db" "github.com/charmbracelet/soft-serve/pkg/proto" "github.com/charmbracelet/soft-serve/pkg/store" + "github.com/charmbracelet/soft-serve/pkg/ui/common" "github.com/charmbracelet/ssh" "github.com/charmbracelet/wish" bm "github.com/charmbracelet/wish/bubbletea" rm "github.com/charmbracelet/wish/recover" - "github.com/muesli/termenv" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" gossh "golang.org/x/crypto/ssh" @@ -70,7 +70,7 @@ func NewSSHServer(ctx context.Context) (*SSHServer, error) { rm.MiddlewareWithLogger( logger, // BubbleTea middleware. - bm.MiddlewareWithProgramHandler(SessionHandler, termenv.ANSI256), + bm.MiddlewareWithProgramHandler(SessionHandler, common.DefaultColorProfile), // CLI middleware. CommandMiddleware, // Logging middleware. diff --git a/pkg/ui/common/format.go b/pkg/ui/common/format.go index 9649eb03a5bb9a9d980140757620d5f8c63880f8..96bae8bd34b5be65306dd920eb2a917ea257c6a9 100644 --- a/pkg/ui/common/format.go +++ b/pkg/ui/common/format.go @@ -7,7 +7,6 @@ import ( "github.com/alecthomas/chroma/lexers" gansi "github.com/charmbracelet/glamour/ansi" "github.com/charmbracelet/soft-serve/pkg/ui/styles" - "github.com/muesli/termenv" ) // FormatLineNumber adds line numbers to a string. @@ -48,10 +47,7 @@ func FormatHighlight(p, c string) (string, error) { r := strings.Builder{} styles := StyleConfig() styles.CodeBlock.Margin = &zero - rctx := gansi.NewRenderContext(gansi.Options{ - Styles: styles, - ColorProfile: termenv.TrueColor, - }) + rctx := StyleRendererWithStyles(styles) err := formatter.Render(&r, rctx) if err != nil { return "", err diff --git a/pkg/ui/common/style.go b/pkg/ui/common/style.go index 8b91d9afa08df6466414edaae1bb7da6ad686956..cdb9ed3dcf91a47ef9f691000ebd095d9580c5cf 100644 --- a/pkg/ui/common/style.go +++ b/pkg/ui/common/style.go @@ -3,6 +3,12 @@ package common import ( "github.com/charmbracelet/glamour" gansi "github.com/charmbracelet/glamour/ansi" + "github.com/muesli/termenv" +) + +var ( + // DefaultColorProfile is the default color profile used by the SSH server. + DefaultColorProfile = termenv.ANSI256 ) func strptr(s string) *string { @@ -25,3 +31,17 @@ func StyleConfig() gansi.StyleConfig { s.CodeBlock.Chroma.Error.BackgroundColor = noColor return s } + +// StyleRenderer returns a new Glamour renderer with the DefaultColorProfile. +func StyleRenderer() gansi.RenderContext { + return StyleRendererWithStyles(StyleConfig()) +} + +// StyleRendererWithStyles returns a new Glamour renderer with the +// DefaultColorProfile and styles. +func StyleRendererWithStyles(styles gansi.StyleConfig) gansi.RenderContext { + return gansi.NewRenderContext(gansi.Options{ + ColorProfile: DefaultColorProfile, + Styles: styles, + }) +} diff --git a/pkg/ui/components/code/code.go b/pkg/ui/components/code/code.go index 56c580345ff5cdaf4ef39cd5cce54cc79f337498..d90ed7e0e5138d43ecbaaacf625380e7d409805f 100644 --- a/pkg/ui/components/code/code.go +++ b/pkg/ui/components/code/code.go @@ -51,10 +51,7 @@ func New(c common.Common, content, extension string) *Code { } st := common.StyleConfig() r.styleConfig = st - r.renderContext = gansi.NewRenderContext(gansi.Options{ - ColorProfile: termenv.TrueColor, - Styles: st, - }) + r.renderContext = common.StyleRendererWithStyles(st) r.SetSize(c.Width, c.Height) return r } diff --git a/pkg/ui/pages/repo/log.go b/pkg/ui/pages/repo/log.go index ef4e5589e8f7ad2306b0cf001c2bf93d00746218..292128d961f11c5e6f76b1599de912dba645e192 100644 --- a/pkg/ui/pages/repo/log.go +++ b/pkg/ui/pages/repo/log.go @@ -18,7 +18,6 @@ import ( "github.com/charmbracelet/soft-serve/pkg/ui/components/viewport" "github.com/charmbracelet/soft-serve/pkg/ui/styles" "github.com/muesli/reflow/wrap" - "github.com/muesli/termenv" ) var waitBeforeLoading = time.Millisecond * 100 @@ -476,13 +475,6 @@ func (l *Log) loadDiffCmd() tea.Msg { return LogDiffMsg(diff) } -func renderCtx() gansi.RenderContext { - return gansi.NewRenderContext(gansi.Options{ - ColorProfile: termenv.TrueColor, - Styles: common.StyleConfig(), - }) -} - func (l *Log) renderCommit(c *git.Commit) string { s := strings.Builder{} // FIXME: lipgloss prints empty lines when CRLF is used @@ -518,7 +510,7 @@ func renderDiff(diff *git.Diff, width int) string { Code: diff.Patch(), Language: "diff", } - err := diffChroma.Render(&pr, renderCtx()) + err := diffChroma.Render(&pr, common.StyleRenderer()) if err != nil { s.WriteString(fmt.Sprintf("\n%s", err.Error())) } else {