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 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}
34func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
35 return func(s ssh.Session) *tea.Program {
36 pty, _, active := s.Pty()
37 if !active {
38 fmt.Println("not active")
39 return nil
40 }
41 sess := &Session{
42 Session: s,
43 Cfg: ac,
44 }
45 cmd := s.Command()
46 initialRepo := ""
47 if len(cmd) == 1 {
48 initialRepo = cmd[0]
49 }
50 if ac.Cfg.Callbacks != nil {
51 ac.Cfg.Callbacks.Tui("new session")
52 }
53 c := common.Common{
54 Styles: styles.DefaultStyles(),
55 Keymap: keymap.DefaultKeyMap(),
56 Width: pty.Window.Width,
57 Height: pty.Window.Height,
58 }
59 m := ui.New(
60 sess,
61 c,
62 initialRepo,
63 )
64 p := tea.NewProgram(m,
65 tea.WithInput(s),
66 tea.WithOutput(s),
67 tea.WithAltScreen(),
68 tea.WithoutCatchPanics(),
69 tea.WithMouseCellMotion(),
70 )
71 sess.Model = m
72 sess.Program = p
73 return p
74 }
75}