session.go

 1package server
 2
 3import (
 4	"fmt"
 5
 6	tea "github.com/charmbracelet/bubbletea"
 7	appCfg "github.com/charmbracelet/soft-serve/config"
 8	"github.com/charmbracelet/soft-serve/ui"
 9	bm "github.com/charmbracelet/wish/bubbletea"
10	"github.com/gliderlabs/ssh"
11)
12
13type Session struct {
14	tea.Model
15	*tea.Program
16	ssh.Session
17	Cfg         *appCfg.Config
18	width       int
19	height      int
20	initialRepo string
21}
22
23func (s *Session) Config() *appCfg.Config {
24	return s.Cfg
25}
26
27func (s *Session) Send(msg tea.Msg) {
28	s.Program.Send(msg)
29}
30
31func (s *Session) Width() int {
32	return s.width
33}
34
35func (s *Session) Height() int {
36	return s.height
37}
38
39func (s *Session) InitialRepo() string {
40	return s.initialRepo
41}
42
43func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
44	return func(s ssh.Session) *tea.Program {
45		pty, _, active := s.Pty()
46		if !active {
47			fmt.Println("not active")
48			return nil
49		}
50		sess := &Session{
51			Session:     s,
52			Cfg:         ac,
53			width:       pty.Window.Width,
54			height:      pty.Window.Height,
55			initialRepo: "",
56		}
57		cmd := s.Command()
58		switch len(cmd) {
59		case 0:
60			sess.initialRepo = ""
61		case 1:
62			sess.initialRepo = cmd[0]
63		}
64		if ac.Cfg.Callbacks != nil {
65			ac.Cfg.Callbacks.Tui("new session")
66		}
67		m := ui.New(sess)
68		p := tea.NewProgram(m,
69			tea.WithInput(s),
70			tea.WithOutput(s),
71			tea.WithAltScreen(),
72			tea.WithoutCatchPanics(),
73		)
74		sess.Model = m
75		sess.Program = p
76		return p
77	}
78}