Skip to content

Commit

Permalink
fix(ui): dry glamour chroma renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 5, 2023
1 parent 0a38578 commit fa23c9c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
10 changes: 1 addition & 9 deletions pkg/ssh/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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

Expand All @@ -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()))
Expand Down
4 changes: 2 additions & 2 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions pkg/ui/common/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pkg/ui/common/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
})
}
5 changes: 1 addition & 4 deletions pkg/ui/components/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/ui/pages/repo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fa23c9c

Please sign in to comment.