1package server
 2
 3import (
 4	"fmt"
 5
 6	"github.com/aymanbagabas/go-osc52"
 7	tea "github.com/charmbracelet/bubbletea"
 8	appCfg "github.com/charmbracelet/soft-serve/config"
 9	"github.com/charmbracelet/soft-serve/ui"
10	"github.com/charmbracelet/soft-serve/ui/common"
11	"github.com/charmbracelet/soft-serve/ui/git"
12	"github.com/charmbracelet/soft-serve/ui/keymap"
13	"github.com/charmbracelet/soft-serve/ui/styles"
14	bm "github.com/charmbracelet/wish/bubbletea"
15	"github.com/gliderlabs/ssh"
16)
17
18type Session struct {
19	tea.Model
20	*tea.Program
21	session ssh.Session
22	Cfg     *appCfg.Config
23}
24
25func (s *Session) Config() *appCfg.Config {
26	return s.Cfg
27}
28
29func (s *Session) Send(msg tea.Msg) {
30	s.Program.Send(msg)
31}
32
33func (s *Session) PublicKey() ssh.PublicKey {
34	return s.session.PublicKey()
35}
36
37func (s *Session) Session() ssh.Session {
38	return s.session
39}
40
41func (s *Session) Source() git.GitRepoSource {
42	return &source{s.Cfg.Source}
43}
44
45func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
46	return func(s ssh.Session) *tea.Program {
47		pty, _, active := s.Pty()
48		if !active {
49			fmt.Println("not active")
50			return nil
51		}
52		sess := &Session{
53			session: s,
54			Cfg:     ac,
55		}
56		cmd := s.Command()
57		initialRepo := ""
58		if len(cmd) == 1 {
59			initialRepo = cmd[0]
60		}
61		if ac.Cfg.Callbacks != nil {
62			ac.Cfg.Callbacks.Tui("new session")
63		}
64		envs := s.Environ()
65		envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
66		output := osc52.NewOutput(s, envs)
67		c := common.Common{
68			Copy:   output,
69			Styles: styles.DefaultStyles(),
70			KeyMap: keymap.DefaultKeyMap(),
71			Width:  pty.Window.Width,
72			Height: pty.Window.Height,
73		}
74		m := ui.New(
75			sess,
76			c,
77			initialRepo,
78		)
79		p := tea.NewProgram(m,
80			tea.WithInput(s),
81			tea.WithOutput(s),
82			tea.WithAltScreen(),
83			tea.WithoutCatchPanics(),
84			tea.WithMouseCellMotion(),
85		)
86		sess.Model = m
87		sess.Program = p
88		return p
89	}
90}