Skip to content

Commit

Permalink
feat(ui): use termenv to copy text to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 99447bc commit e00b19d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
29 changes: 24 additions & 5 deletions server/ssh/session.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ssh

import (
"fmt"
"strings"

"github.com/aymanbagabas/go-osc52"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/soft-serve/server/backend"
"github.com/charmbracelet/soft-serve/server/config"
Expand All @@ -13,6 +12,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 @@ -46,9 +46,8 @@ func SessionHandler(cfg *config.Config) bm.ProgramHandler {
}
}

envs := s.Environ()
envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
output := osc52.NewOutput(s, envs)
envs := &sessionEnv{s}
output := termenv.NewOutput(s, termenv.WithColorCache(true), termenv.WithEnvironment(envs))
c := common.NewCommon(s.Context(), output, pty.Window.Width, pty.Window.Height)
c.SetValue(common.ConfigKey, cfg)
m := ui.New(c, initialRepo)
Expand All @@ -65,3 +64,23 @@ func SessionHandler(cfg *config.Config) bm.ProgramHandler {
return p
}
}

var _ termenv.Environ = &sessionEnv{}

type sessionEnv struct {
ssh.Session
}

func (s *sessionEnv) Environ() []string {
pty, _, _ := s.Pty()
return append(s.Session.Environ(), "TERM="+pty.Term)
}

func (s *sessionEnv) Getenv(key string) string {
for _, env := range s.Environ() {
if strings.HasPrefix(env, key+"=") {
return strings.TrimPrefix(env, key+"=")
}
}
return ""
}
8 changes: 4 additions & 4 deletions ui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package common
import (
"context"

"github.com/aymanbagabas/go-osc52"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/charmbracelet/soft-serve/ui/keymap"
"github.com/charmbracelet/soft-serve/ui/styles"
"github.com/charmbracelet/ssh"
zone "github.com/lrstanley/bubblezone"
"github.com/muesli/termenv"
)

type contextKey struct {
Expand All @@ -28,20 +28,20 @@ type Common struct {
Width, Height int
Styles *styles.Styles
KeyMap *keymap.KeyMap
Copy *osc52.Output
Zone *zone.Manager
Output *termenv.Output
}

// NewCommon returns a new Common struct.
func NewCommon(ctx context.Context, copy *osc52.Output, width, height int) Common {
func NewCommon(ctx context.Context, out *termenv.Output, width, height int) Common {
if ctx == nil {
ctx = context.TODO()
}
return Common{
ctx: ctx,
Width: width,
Height: height,
Copy: copy,
Output: out,
Styles: styles.DefaultStyles(),
KeyMap: keymap.DefaultKeyMap(),
Zone: zone.New(),
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (r *Repo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case CopyMsg:
txt := msg.Text
if cfg := r.common.Config(); cfg != nil {
r.common.Copy.Copy(txt)
r.common.Output.Copy(txt)
}
cmds = append(cmds, func() tea.Msg {
return statusbar.StatusBarMsg{
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/selection/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (d *ItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
switch {
case key.Matches(msg, d.common.KeyMap.Copy):
d.copiedIdx = idx
d.common.Copy.Copy(item.Command())
d.common.Output.Copy(item.Command())
return m.SetItem(idx, item)
}
}
Expand Down

0 comments on commit e00b19d

Please sign in to comment.