Skip to content

Commit

Permalink
fix: reset api
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 8, 2024
1 parent b3f3747 commit dea3814
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
25 changes: 3 additions & 22 deletions bubbletea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"context"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/muesli/termenv"
)

// BubbleTeaHandler is the function Bubble Tea apps implement to hook into the
Expand All @@ -22,7 +20,7 @@ type BubbleTeaHandler = Handler // nolint: revive
// Handler is the function Bubble Tea apps implement to hook into the
// SSH Middleware. This will create a new tea.Program for every connection and
// start it with the tea.ProgramOptions returned.
type Handler func(ssh.Session, *lipgloss.Renderer) (tea.Model, []tea.ProgramOption)
type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)

// Middleware takes a Handler and hooks the input and output for the
// ssh.Session into the tea.Program.
Expand All @@ -32,15 +30,13 @@ type Handler func(ssh.Session, *lipgloss.Renderer) (tea.Model, []tea.ProgramOpti
func Middleware(bth Handler) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
tty, windowChanges, ok := s.Pty()
_, windowChanges, ok := s.Pty()
if !ok {
wish.Fatalln(s, "no active terminal, skipping")
return
}

renderer := lipgloss.NewRenderer(tty.Slave, termenv.WithColorCache(true))

m, hopts := bth(s, renderer)
m, hopts := bth(s)
if m == nil {
log.Error("no model returned by the handler")
return
Expand Down Expand Up @@ -78,18 +74,3 @@ func Middleware(bth Handler) wish.Middleware {
}
}
}

func makeIOOpts(s ssh.Session) []tea.ProgramOption {
pty, _, ok := s.Pty()
if !ok || s.EmulatedPty() {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
}
}

return []tea.ProgramOption{
tea.WithInput(pty.Slave),
tea.WithOutput(pty.Slave),
}
}
16 changes: 16 additions & 0 deletions bubbletea/tea_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !linux && !darwin && !freebsd && !dragonfly && !netbsd && !openbsd && !solaris
// +build !linux,!darwin,!freebsd,!dragonfly,!netbsd,!openbsd,!solaris

package bubbletea

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
)

func makeIOOpts(s ssh.Session) []tea.ProgramOption {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
}
}
24 changes: 24 additions & 0 deletions bubbletea/tea_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package bubbletea

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
)

func makeIOOpts(s ssh.Session) []tea.ProgramOption {
pty, _, ok := s.Pty()
if !ok || s.EmulatedPty() {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
}
}

return []tea.ProgramOption{
tea.WithInput(pty.Slave),
tea.WithOutput(pty.Slave),
}
}
3 changes: 2 additions & 1 deletion examples/wish-exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func main() {
}
}

func teaHandler(s ssh.Session, renderer *lipgloss.Renderer) (tea.Model, []tea.ProgramOption) {
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
renderer := newRenderer(s)
m := model{
sess: s,
style: renderer.NewStyle().Foreground(lipgloss.Color("8")),
Expand Down
15 changes: 15 additions & 0 deletions examples/wish-exec/other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !linux && !darwin && !freebsd && !dragonfly && !netbsd && !openbsd && !solaris
// +build !linux,!darwin,!freebsd,!dragonfly,!netbsd,!openbsd,!solaris

// TODO: support Windows
package main

import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh"
"github.com/muesli/termenv"
)

func newRenderer(s ssh.Session) *lipgloss.Renderer {
return lipgloss.NewRenderer(s, termenv.WithColorCache(true))
}
15 changes: 15 additions & 0 deletions examples/wish-exec/unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package main

import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh"
"github.com/muesli/termenv"
)

func newRenderer(s ssh.Session) *lipgloss.Renderer {
pty, _, _ := s.Pty()
return lipgloss.NewRenderer(pty.Slave, termenv.WithColorCache(true))
}

0 comments on commit dea3814

Please sign in to comment.