Skip to content

Commit

Permalink
Move ProgramOptions to BubbleTeaHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Jul 30, 2021
1 parent 77c72c0 commit e938b1c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
lm "smoothie/server/middleware/logging"
"smoothie/tui"

tea "github.com/charmbracelet/bubbletea"
"github.com/meowgorithm/babyenv"
)

Expand All @@ -28,7 +27,7 @@ func main() {
s, err := server.NewServer(
cfg.Port,
cfg.KeyPath,
bm.Middleware(tui.SessionHandler, tea.WithAltScreen()),
bm.Middleware(tui.SessionHandler),
gm.Middleware(cfg.RepoPath, cfg.RepoAuthPath),
lm.Middleware(),
)
Expand Down
6 changes: 4 additions & 2 deletions server/middleware/bubbletea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/gliderlabs/ssh"
)

func Middleware(bth func(ssh.Session) tea.Model, opts ...tea.ProgramOption) middleware.Middleware {
type BubbleTeaHandler func(ssh.Session) (tea.Model, []tea.ProgramOption)

func Middleware(bth BubbleTeaHandler) middleware.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
m := bth(s)
m, opts := bth(s)
if m != nil {
opts = append(opts, tea.WithInput(s), tea.WithOutput(s))
p := tea.NewProgram(m, opts...)
Expand Down
8 changes: 4 additions & 4 deletions tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func (e errMsg) Error() string {
return e.err.Error()
}

func SessionHandler(s ssh.Session) tea.Model {
func SessionHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
if len(s.Command()) == 0 {
pty, changes, active := s.Pty()
if !active {
return nil
return nil, nil
}
return NewModel(pty.Window.Width, pty.Window.Height, changes)
return NewModel(pty.Window.Width, pty.Window.Height, changes), []tea.ProgramOption{tea.WithAltScreen()}
}
return nil
return nil, nil
}

type Model struct {
Expand Down

0 comments on commit e938b1c

Please sign in to comment.