Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bubbletea program middleware
This allows passing a program handler that returns tea.Program to allow for more advanced use cases such as using p.Send() to send messages to tea.Program. Fixes: #37
- Loading branch information
1 parent
bea68c3
commit 3a1ac79
Showing
2 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
// An example Bubble Tea server. This will put an ssh session into alt screen | ||
// and continually print up to date terminal information. | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/wish" | ||
bm "github.com/charmbracelet/wish/bubbletea" | ||
lm "github.com/charmbracelet/wish/logging" | ||
"github.com/gliderlabs/ssh" | ||
"github.com/muesli/termenv" | ||
) | ||
|
||
const host = "localhost" | ||
const port = 23234 | ||
|
||
func main() { | ||
s, err := wish.NewServer( | ||
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)), | ||
wish.WithHostKeyPath(".ssh/term_info_ed25519"), | ||
wish.WithMiddleware( | ||
myCustomBubbleteaMiddleware(), | ||
lm.Middleware(), | ||
), | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
log.Printf("Starting SSH server on %s:%d", host, port) | ||
go func() { | ||
if err = s.ListenAndServe(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
}() | ||
|
||
<-done | ||
log.Println("Stopping SSH server") | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer func() { cancel() }() | ||
if err := s.Shutdown(ctx); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
// You can write your own custom bubbletea middleware that wraps tea.Program. | ||
// Make sure you set the program input and output to ssh.Session. | ||
func myCustomBubbleteaMiddleware() wish.Middleware { | ||
newProg := func(m tea.Model, opts ...tea.ProgramOption) *tea.Program { | ||
p := tea.NewProgram(m, opts...) | ||
go func() { | ||
for { | ||
select { | ||
case <-time.After(1 * time.Second): | ||
p.Send(timeMsg(time.Now())) | ||
} | ||
} | ||
}() | ||
return p | ||
} | ||
teaHandler := func(s ssh.Session) *tea.Program { | ||
pty, _, active := s.Pty() | ||
if !active { | ||
fmt.Println("no active terminal, skipping") | ||
s.Exit(1) | ||
return nil | ||
} | ||
m := model{ | ||
term: pty.Term, | ||
width: pty.Window.Width, | ||
height: pty.Window.Height, | ||
time: time.Now(), | ||
} | ||
return newProg(m, tea.WithInput(s), tea.WithOutput(s), tea.WithAltScreen()) | ||
} | ||
return bm.MiddlewareWithProgramHandler(teaHandler, termenv.ANSI256) | ||
} | ||
|
||
// Just a generic tea.Model to demo terminal information of ssh. | ||
type model struct { | ||
term string | ||
width int | ||
height int | ||
time time.Time | ||
} | ||
|
||
type timeMsg time.Time | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case timeMsg: | ||
m.time = time.Time(msg) | ||
case tea.WindowSizeMsg: | ||
m.height = msg.Height | ||
m.width = msg.Width | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "q", "ctrl+c": | ||
return m, tea.Quit | ||
} | ||
} | ||
return m, nil | ||
} | ||
|
||
func (m model) View() string { | ||
s := "Your term is %s\n" | ||
s += "Your window size is x: %d y: %d\n" | ||
s += "Time: " + m.time.Format(time.RFC1123) + "\n\n" | ||
s += "Press 'q' to quit\n" | ||
return fmt.Sprintf(s, m.term, m.width, m.height) | ||
} |