From be35f67f422c632ddb4f4a5be69e31f5c917d889 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Jan 2024 13:31:47 -0500 Subject: [PATCH] fix: reset api --- bubbletea/tea.go | 25 +++---------------------- bubbletea/tea_other.go | 16 ++++++++++++++++ bubbletea/tea_unix.go | 24 ++++++++++++++++++++++++ examples/go.mod | 2 +- examples/go.sum | 4 ++++ examples/wish-exec/main.go | 3 ++- examples/wish-exec/other.go | 15 +++++++++++++++ examples/wish-exec/unix.go | 15 +++++++++++++++ go.mod | 2 +- go.sum | 4 ++++ 10 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 bubbletea/tea_other.go create mode 100644 bubbletea/tea_unix.go create mode 100644 examples/wish-exec/other.go create mode 100644 examples/wish-exec/unix.go diff --git a/bubbletea/tea.go b/bubbletea/tea.go index ac02763c..7cb98250 100644 --- a/bubbletea/tea.go +++ b/bubbletea/tea.go @@ -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 @@ -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. @@ -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 @@ -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), - } -} diff --git a/bubbletea/tea_other.go b/bubbletea/tea_other.go new file mode 100644 index 00000000..c61e5923 --- /dev/null +++ b/bubbletea/tea_other.go @@ -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), + } +} diff --git a/bubbletea/tea_unix.go b/bubbletea/tea_unix.go new file mode 100644 index 00000000..3fd77203 --- /dev/null +++ b/bubbletea/tea_unix.go @@ -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), + } +} diff --git a/examples/go.mod b/examples/go.mod index c37553c6..58e14d8b 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -7,7 +7,7 @@ require ( github.com/charmbracelet/bubbletea v0.25.0 github.com/charmbracelet/lipgloss v0.9.1 github.com/charmbracelet/log v0.3.1 - github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254 + github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d github.com/charmbracelet/wish v0.5.0 github.com/muesli/termenv v0.15.2 github.com/spf13/cobra v1.7.0 diff --git a/examples/go.sum b/examples/go.sum index c810176a..e93290c6 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -25,6 +25,10 @@ github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMT github.com/charmbracelet/log v0.3.1/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g= github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254 h1:LQEx1n2MuC2Kzgetq8TW7v2JDXN/+o3BsqloJmKgoiY= github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= +github.com/charmbracelet/ssh v0.0.0-20240108184918-e75da4447a78 h1:OPhgZLveCrbwJqVyPj0+bFMi7ncEdXtvhiq+m8y9kVs= +github.com/charmbracelet/ssh v0.0.0-20240108184918-e75da4447a78/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= +github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d h1:ykEOWspdc2kLCGF10gTJ015n5Q1VZZ5Q59oU+/RBm2s= +github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= diff --git a/examples/wish-exec/main.go b/examples/wish-exec/main.go index 426771b3..8502b2d2 100644 --- a/examples/wish-exec/main.go +++ b/examples/wish-exec/main.go @@ -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")), diff --git a/examples/wish-exec/other.go b/examples/wish-exec/other.go new file mode 100644 index 00000000..8f2e1c4e --- /dev/null +++ b/examples/wish-exec/other.go @@ -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)) +} diff --git a/examples/wish-exec/unix.go b/examples/wish-exec/unix.go new file mode 100644 index 00000000..fd7a5923 --- /dev/null +++ b/examples/wish-exec/unix.go @@ -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)) +} diff --git a/go.mod b/go.mod index b5e97343..a195d070 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/charmbracelet/keygen v0.5.0 github.com/charmbracelet/lipgloss v0.9.1 github.com/charmbracelet/log v0.3.1 - github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254 + github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d github.com/go-git/go-git/v5 v5.11.0 github.com/google/go-cmp v0.6.0 github.com/hashicorp/golang-lru/v2 v2.0.7 diff --git a/go.sum b/go.sum index 513431a0..43254e2d 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,10 @@ github.com/charmbracelet/log v0.3.1 h1:TjuY4OBNbxmHWSwO3tosgqs5I3biyY8sQPny/eCMT github.com/charmbracelet/log v0.3.1/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g= github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254 h1:LQEx1n2MuC2Kzgetq8TW7v2JDXN/+o3BsqloJmKgoiY= github.com/charmbracelet/ssh v0.0.0-20240108173924-d928369dd254/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= +github.com/charmbracelet/ssh v0.0.0-20240108184918-e75da4447a78 h1:OPhgZLveCrbwJqVyPj0+bFMi7ncEdXtvhiq+m8y9kVs= +github.com/charmbracelet/ssh v0.0.0-20240108184918-e75da4447a78/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= +github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d h1:ykEOWspdc2kLCGF10gTJ015n5Q1VZZ5Q59oU+/RBm2s= +github.com/charmbracelet/ssh v0.0.0-20240108190445-72503be29e9d/go.mod h1:H/cBXf4vsbGUlCLiR8cNGP7lpv8wuHbgD3GnWJ8quHA= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=