e00b19de2733e50f7807f35b564060ec771e4ed2

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

Message

feat(ui): use termenv to copy text to clipboard

Diff

  1diff --git a/server/ssh/session.go b/server/ssh/session.go
  2index 92b65dde70e93b393a30b5259fefe29de71ab40b..d9fffccdbaad04b624eae2556ae731aa9445dee5 100644
  3--- a/server/ssh/session.go
  4+++ b/server/ssh/session.go
  5@@ -1,9 +1,8 @@
  6 package ssh
  7 
  8 import (
  9-	"fmt"
 10+	"strings"
 11 
 12-	"github.com/aymanbagabas/go-osc52"
 13 	tea "github.com/charmbracelet/bubbletea"
 14 	"github.com/charmbracelet/soft-serve/server/backend"
 15 	"github.com/charmbracelet/soft-serve/server/config"
 16@@ -13,6 +12,7 @@ import (
 17 	"github.com/charmbracelet/ssh"
 18 	"github.com/charmbracelet/wish"
 19 	bm "github.com/charmbracelet/wish/bubbletea"
 20+	"github.com/muesli/termenv"
 21 	"github.com/prometheus/client_golang/prometheus"
 22 	"github.com/prometheus/client_golang/prometheus/promauto"
 23 )
 24@@ -46,9 +46,8 @@ func SessionHandler(cfg *config.Config) bm.ProgramHandler {
 25 			}
 26 		}
 27 
 28-		envs := s.Environ()
 29-		envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
 30-		output := osc52.NewOutput(s, envs)
 31+		envs := &sessionEnv{s}
 32+		output := termenv.NewOutput(s, termenv.WithColorCache(true), termenv.WithEnvironment(envs))
 33 		c := common.NewCommon(s.Context(), output, pty.Window.Width, pty.Window.Height)
 34 		c.SetValue(common.ConfigKey, cfg)
 35 		m := ui.New(c, initialRepo)
 36@@ -65,3 +64,23 @@ func SessionHandler(cfg *config.Config) bm.ProgramHandler {
 37 		return p
 38 	}
 39 }
 40+
 41+var _ termenv.Environ = &sessionEnv{}
 42+
 43+type sessionEnv struct {
 44+	ssh.Session
 45+}
 46+
 47+func (s *sessionEnv) Environ() []string {
 48+	pty, _, _ := s.Pty()
 49+	return append(s.Session.Environ(), "TERM="+pty.Term)
 50+}
 51+
 52+func (s *sessionEnv) Getenv(key string) string {
 53+	for _, env := range s.Environ() {
 54+		if strings.HasPrefix(env, key+"=") {
 55+			return strings.TrimPrefix(env, key+"=")
 56+		}
 57+	}
 58+	return ""
 59+}
 60diff --git a/ui/common/common.go b/ui/common/common.go
 61index 660b6fdabba2f703fb0a22ea2dfcd13fd85528b7..78b4a992bb1ee46cb9f2edb62392e74633d8ebd3 100644
 62--- a/ui/common/common.go
 63+++ b/ui/common/common.go
 64@@ -3,13 +3,13 @@ package common
 65 import (
 66 	"context"
 67 
 68-	"github.com/aymanbagabas/go-osc52"
 69 	"github.com/charmbracelet/soft-serve/git"
 70 	"github.com/charmbracelet/soft-serve/server/config"
 71 	"github.com/charmbracelet/soft-serve/ui/keymap"
 72 	"github.com/charmbracelet/soft-serve/ui/styles"
 73 	"github.com/charmbracelet/ssh"
 74 	zone "github.com/lrstanley/bubblezone"
 75+	"github.com/muesli/termenv"
 76 )
 77 
 78 type contextKey struct {
 79@@ -28,12 +28,12 @@ type Common struct {
 80 	Width, Height int
 81 	Styles        *styles.Styles
 82 	KeyMap        *keymap.KeyMap
 83-	Copy          *osc52.Output
 84 	Zone          *zone.Manager
 85+	Output        *termenv.Output
 86 }
 87 
 88 // NewCommon returns a new Common struct.
 89-func NewCommon(ctx context.Context, copy *osc52.Output, width, height int) Common {
 90+func NewCommon(ctx context.Context, out *termenv.Output, width, height int) Common {
 91 	if ctx == nil {
 92 		ctx = context.TODO()
 93 	}
 94@@ -41,7 +41,7 @@ func NewCommon(ctx context.Context, copy *osc52.Output, width, height int) Commo
 95 		ctx:    ctx,
 96 		Width:  width,
 97 		Height: height,
 98-		Copy:   copy,
 99+		Output: out,
100 		Styles: styles.DefaultStyles(),
101 		KeyMap: keymap.DefaultKeyMap(),
102 		Zone:   zone.New(),
103diff --git a/ui/pages/repo/repo.go b/ui/pages/repo/repo.go
104index 38119d9d520f74364ec2c5cbafce5b11e61b0263..5e280b2b24242390147845bf6cbf054f4469d9fa 100644
105--- a/ui/pages/repo/repo.go
106+++ b/ui/pages/repo/repo.go
107@@ -239,7 +239,7 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
108 	case CopyMsg:
109 		txt := msg.Text
110 		if cfg := r.common.Config(); cfg != nil {
111-			r.common.Copy.Copy(txt)
112+			r.common.Output.Copy(txt)
113 		}
114 		cmds = append(cmds, func() tea.Msg {
115 			return statusbar.StatusBarMsg{
116diff --git a/ui/pages/selection/item.go b/ui/pages/selection/item.go
117index 7d24d5a6647a899316e25994daef510fd0fb6bf8..46b76b5cdcdfe03fe2a32c34ad3d591fe7f18029 100644
118--- a/ui/pages/selection/item.go
119+++ b/ui/pages/selection/item.go
120@@ -136,7 +136,7 @@ func (d *ItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
121 		switch {
122 		case key.Matches(msg, d.common.KeyMap.Copy):
123 			d.copiedIdx = idx
124-			d.common.Copy.Copy(item.Command())
125+			d.common.Output.Copy(item.Command())
126 			return m.SetItem(idx, item)
127 		}
128 	}