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	"github.com/charmbracelet/soft-serve/ui/common"
10	"github.com/charmbracelet/soft-serve/ui/keymap"
11	"github.com/charmbracelet/soft-serve/ui/styles"
12	bm "github.com/charmbracelet/wish/bubbletea"
13	"github.com/gliderlabs/ssh"
14)
15
16type Session struct {
17	tea.Model
18	*tea.Program
19	session ssh.Session
20	Cfg     *appCfg.Config
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) PublicKey() ssh.PublicKey {
32	return s.session.PublicKey()
33}
34
35func (s *Session) Session() ssh.Session {
36	return s.session
37}
38
39func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
40	return func(s ssh.Session) *tea.Program {
41		pty, _, active := s.Pty()
42		if !active {
43			fmt.Println("not active")
44			return nil
45		}
46		sess := &Session{
47			session: s,
48			Cfg:     ac,
49		}
50		cmd := s.Command()
51		initialRepo := ""
52		if len(cmd) == 1 {
53			initialRepo = cmd[0]
54		}
55		if ac.Cfg.Callbacks != nil {
56			ac.Cfg.Callbacks.Tui("new session")
57		}
58		c := common.Common{
59			Styles: styles.DefaultStyles(),
60			KeyMap: keymap.DefaultKeyMap(),
61			Width:  pty.Window.Width,
62			Height: pty.Window.Height,
63		}
64		m := ui.New(
65			sess,
66			c,
67			initialRepo,
68		)
69		p := tea.NewProgram(m,
70			tea.WithInput(s),
71			tea.WithOutput(s),
72			tea.WithAltScreen(),
73			tea.WithoutCatchPanics(),
74			tea.WithMouseCellMotion(),
75		)
76		sess.Model = m
77		sess.Program = p
78		return p
79	}
80}