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/keymap"
12 "github.com/charmbracelet/soft-serve/ui/styles"
13 bm "github.com/charmbracelet/wish/bubbletea"
14 "github.com/gliderlabs/ssh"
15 zone "github.com/lrstanley/bubblezone"
16)
17
18// SessionHandler is the soft-serve bubbletea ssh session handler.
19func SessionHandler(ac *appCfg.Config) bm.ProgramHandler {
20 return func(s ssh.Session) *tea.Program {
21 pty, _, active := s.Pty()
22 if !active {
23 return nil
24 }
25 cmd := s.Command()
26 initialRepo := ""
27 if len(cmd) == 1 {
28 initialRepo = cmd[0]
29 }
30 if ac.Cfg.Callbacks != nil {
31 ac.Cfg.Callbacks.Tui("new session")
32 }
33 envs := s.Environ()
34 envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
35 output := osc52.NewOutput(s, envs)
36 c := common.Common{
37 Copy: output,
38 Styles: styles.DefaultStyles(),
39 KeyMap: keymap.DefaultKeyMap(),
40 Width: pty.Window.Width,
41 Height: pty.Window.Height,
42 Zone: zone.New(),
43 }
44 m := ui.New(
45 ac,
46 s,
47 c,
48 initialRepo,
49 )
50 p := tea.NewProgram(m,
51 tea.WithInput(s),
52 tea.WithOutput(s),
53 tea.WithAltScreen(),
54 tea.WithoutCatchPanics(),
55 tea.WithMouseCellMotion(),
56 )
57 return p
58 }
59}