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