Skip to content

Commit

Permalink
fix: use lipgloss renderers
Browse files Browse the repository at this point in the history
Fixes: #468
  • Loading branch information
aymanbagabas committed Mar 8, 2024
1 parent d54c6de commit 920e4a7
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 118 deletions.
31 changes: 21 additions & 10 deletions pkg/jobs/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"
"runtime"
"strings"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/git"
Expand Down Expand Up @@ -62,16 +63,26 @@ func (m mirrorPull) Func(ctx context.Context) func() {
name := repo.Name()
wq.Add(name, func() {
repo := repo
cmd := git.NewCommand("remote", "update", "--prune").WithContext(ctx)
cmd.AddEnvs(
fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
filepath.Join(cfg.DataPath, "ssh", "known_hosts"),
cfg.SSH.ClientKeyPath,
),
)

if _, err := cmd.RunInDir(r.Path); err != nil {
logger.Error("error running git remote update", "repo", name, "err", err)

cmds := []string{
"fetch --prune", // fetch prune before updating remote
"gc --aggressive --prune=now", // aggressive garbage collection
"remote update --prune", // update remote and prune remote refs
}

for _, c := range cmds {
args := strings.Split(c, " ")
cmd := git.NewCommand(args...).WithContext(ctx)
cmd.AddEnvs(
fmt.Sprintf(`GIT_SSH_COMMAND=ssh -o UserKnownHostsFile="%s" -o StrictHostKeyChecking=no -i "%s"`,
filepath.Join(cfg.DataPath, "ssh", "known_hosts"),
cfg.SSH.ClientKeyPath,
),
)

if _, err := cmd.RunInDir(r.Path); err != nil {
logger.Error("error running git remote update", "repo", name, "err", err)
}
}

if cfg.LFS.Enabled {
Expand Down
5 changes: 3 additions & 2 deletions pkg/ssh/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/ui/common"
Expand All @@ -11,12 +12,12 @@ import (
)

// blobCommand returns a command that prints the contents of a file.
func blobCommand() *cobra.Command {
func blobCommand(renderer *lipgloss.Renderer) *cobra.Command {
var linenumber bool
var color bool
var raw bool

styles := styles.DefaultStyles()
styles := styles.DefaultStyles(renderer)
cmd := &cobra.Command{
Use: "blob REPOSITORY [REFERENCE] [PATH]",
Aliases: []string{"cat", "show"},
Expand Down
5 changes: 3 additions & 2 deletions pkg/ssh/cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

gansi "github.com/charmbracelet/glamour/ansi"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/ui/common"
Expand All @@ -14,7 +15,7 @@ import (
)

// commitCommand returns a command that prints the contents of a commit.
func commitCommand() *cobra.Command {
func commitCommand(renderer *lipgloss.Renderer) *cobra.Command {
var color bool
var patchOnly bool

Expand Down Expand Up @@ -54,7 +55,7 @@ func commitCommand() *cobra.Command {
return err
}

commonStyle := styles.DefaultStyles()
commonStyle := styles.DefaultStyles(renderer)
style := commonStyle.Log

s := strings.Builder{}
Expand Down
7 changes: 4 additions & 3 deletions pkg/ssh/cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"fmt"
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/proto"
"github.com/spf13/cobra"
)

// RepoCommand returns a command for managing repositories.
func RepoCommand() *cobra.Command {
func RepoCommand(renderer *lipgloss.Renderer) *cobra.Command {
cmd := &cobra.Command{
Use: "repo",
Aliases: []string{"repos", "repository", "repositories"},
Short: "Manage repositories",
}

cmd.AddCommand(
blobCommand(),
blobCommand(renderer),
branchCommand(),
collabCommand(),
commitCommand(),
commitCommand(renderer),
createCommand(),
deleteCommand(),
descriptionCommand(),
Expand Down
11 changes: 10 additions & 1 deletion pkg/ssh/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ssh

import (
"fmt"
"os"
"time"

"github.com/charmbracelet/log"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/charmbracelet/soft-serve/pkg/store"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
"github.com/muesli/termenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,6 +90,12 @@ func CommandMiddleware(sh ssh.Handler) ssh.Handler {
ctx := s.Context()
cfg := config.FromContext(ctx)

renderer := bm.MakeRenderer(s)
if testrun, ok := os.LookupEnv("SOFT_SERVE_NO_COLOR"); ok && testrun == "1" {
// Disable colors when running tests.
renderer.SetColorProfile(termenv.Ascii)
}

args := s.Command()
cliCommandCounter.WithLabelValues(cmd.CommandName(args)).Inc()
rootCmd := &cobra.Command{
Expand All @@ -101,7 +110,7 @@ func CommandMiddleware(sh ssh.Handler) ssh.Handler {
cmd.GitUploadPackCommand(),
cmd.GitUploadArchiveCommand(),
cmd.GitReceivePackCommand(),
cmd.RepoCommand(),
cmd.RepoCommand(renderer),
cmd.SettingsCommand(),
cmd.UserCommand(),
cmd.InfoCommand(),
Expand Down
11 changes: 9 additions & 2 deletions pkg/ssh/session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ssh

import (
"os"
"time"

tea "github.com/charmbracelet/bubbletea"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
"github.com/muesli/termenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand Down Expand Up @@ -52,8 +54,13 @@ func SessionHandler(s ssh.Session) *tea.Program {
}
}

output := bm.MakeRenderer(s)
c := common.NewCommon(ctx, output, pty.Window.Width, pty.Window.Height)
renderer := bm.MakeRenderer(s)
if testrun, ok := os.LookupEnv("SOFT_SERVE_NO_COLOR"); ok && testrun == "1" {
// Disable colors when running tests.
renderer.SetColorProfile(termenv.Ascii)
}

c := common.NewCommon(ctx, renderer, pty.Window.Width, pty.Window.Height)
c.SetValue(common.ConfigKey, cfg)
m := NewUI(c, initialRepo)
opts := bm.MakeOptions(s)
Expand Down
18 changes: 10 additions & 8 deletions pkg/ui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Common struct {
Styles *styles.Styles
KeyMap *keymap.KeyMap
Zone *zone.Manager
Renderer *lipgloss.Renderer
Output *termenv.Output
Logger *log.Logger
HideCloneCmd bool
Expand All @@ -45,14 +46,15 @@ func NewCommon(ctx context.Context, out *lipgloss.Renderer, width, height int) C
ctx = context.TODO()
}
return Common{
ctx: ctx,
Width: width,
Height: height,
Output: out.Output(),
Styles: styles.DefaultStyles(),
KeyMap: keymap.DefaultKeyMap(),
Zone: zone.New(),
Logger: log.FromContext(ctx).WithPrefix("ui"),
ctx: ctx,
Width: width,
Height: height,
Renderer: out,
Output: out.Output(),
Styles: styles.DefaultStyles(out),
KeyMap: keymap.DefaultKeyMap(),
Zone: zone.New(),
Logger: log.FromContext(ctx).WithPrefix("ui"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/components/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *Code) Init() tea.Cmd {
// https://github.com/muesli/reflow/issues/43
//
// TODO: solve this upstream in Glamour/Reflow.
content = lipgloss.NewStyle().Width(w).Render(content)
content = r.common.Renderer.NewStyle().Width(w).Render(content)

r.Viewport.Model.SetContent(content)

Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/components/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *Model) View() string {
Width(maxWidth).
Render(v)

return lipgloss.NewStyle().MaxWidth(s.common.Width).
return s.common.Renderer.NewStyle().MaxWidth(s.common.Width).
Render(
lipgloss.JoinHorizontal(lipgloss.Top,
key,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/components/tabs/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (t *Tabs) View() string {
s.WriteString(sep.String())
}
}
return lipgloss.NewStyle().
return t.common.Renderer.NewStyle().
MaxWidth(t.common.Width).
Render(s.String())
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/ui/pages/repo/filesitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/fs"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/key"
Expand All @@ -22,7 +23,11 @@ type FileItem struct {

// ID returns the ID of the file item.
func (i FileItem) ID() string {
return i.entry.Name()
name := i.entry.Name()
if n, err := strconv.Unquote(name); err == nil {
name = n
}
return name
}

// Title returns the title of the file item.
Expand Down Expand Up @@ -139,7 +144,7 @@ func (d FileItemDelegate) Render(w io.Writer, m list.Model, index int, listItem
name = nameStyle.Render(name)
size = sizeStyle.Render(size)
modeStr := modeStyle.Render(mode.String())
truncate := lipgloss.NewStyle().MaxWidth(m.Width() -
truncate := d.common.Renderer.NewStyle().MaxWidth(m.Width() -
s.Selector.GetHorizontalFrameSize() -
s.Selector.GetWidth())
fmt.Fprint(w,
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (r *Repo) headerView() string {
if r.selectedRepo == nil {
return ""
}
truncate := lipgloss.NewStyle().MaxWidth(r.common.Width)
truncate := r.common.Renderer.NewStyle().MaxWidth(r.common.Width)
header := r.selectedRepo.ProjectName()
if header == "" {
header = r.selectedRepo.Name()
Expand Down
8 changes: 4 additions & 4 deletions pkg/ui/pages/selection/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func New(c common.Common) *Selection {
ts[i] = b.String()
}
t := tabs.New(c, ts)
t.TabSeparator = lipgloss.NewStyle()
t.TabSeparator = c.Renderer.NewStyle()
t.TabInactive = c.Styles.TopLevelNormalTab.Copy()
t.TabActive = c.Styles.TopLevelActiveTab.Copy()
t.TabDot = c.Styles.TopLevelActiveTabDot.Copy()
Expand Down Expand Up @@ -289,15 +289,15 @@ func (s *Selection) View() string {
wm, hm := s.getMargins()
switch s.activePane {
case selectorPane:
ss := lipgloss.NewStyle().
ss := s.common.Renderer.NewStyle().
Width(s.common.Width - wm).
Height(s.common.Height - hm)
view = ss.Render(s.selector.View())
case readmePane:
rs := lipgloss.NewStyle().
rs := s.common.Renderer.NewStyle().
Height(s.common.Height - hm)
status := fmt.Sprintf("☰ %.f%%", s.readme.ScrollPercent()*100)
readmeStatus := lipgloss.NewStyle().
readmeStatus := s.common.Renderer.NewStyle().
Align(lipgloss.Right).
Width(s.common.Width - wm).
Foreground(s.common.Styles.InactiveBorderColor).
Expand Down
Loading

0 comments on commit 920e4a7

Please sign in to comment.