2020-07-30 11:29:20 -04:00
|
|
|
// Package tea provides a framework for building rich terminal user interfaces
|
|
|
|
// based on the paradigms of The Elm Architecture. It's well-suited for simple
|
|
|
|
// and complex terminal applications, either inline, full-window, or a mix of
|
|
|
|
// both. It's been battle-tested in several large projects and is
|
|
|
|
// production-ready.
|
2020-07-29 20:49:20 -04:00
|
|
|
//
|
|
|
|
// A tutorial is available at https://github.com/charmbracelet/bubbletea/tree/master/tutorials
|
|
|
|
//
|
|
|
|
// Example programs can be found at https://github.com/charmbracelet/bubbletea/tree/master/examples
|
2020-05-25 19:26:40 -04:00
|
|
|
package tea
|
2020-01-10 16:02:04 -05:00
|
|
|
|
|
|
|
import (
|
2021-01-11 16:33:14 -05:00
|
|
|
"context"
|
2020-06-22 15:55:52 -04:00
|
|
|
"fmt"
|
2020-12-23 12:56:13 -05:00
|
|
|
"io"
|
2020-01-25 01:15:56 -05:00
|
|
|
"os"
|
2021-01-11 16:33:14 -05:00
|
|
|
"os/signal"
|
2020-09-21 13:09:32 -04:00
|
|
|
"runtime/debug"
|
2020-06-22 15:55:52 -04:00
|
|
|
"sync"
|
2021-01-11 16:33:14 -05:00
|
|
|
"syscall"
|
2020-01-31 07:47:36 -05:00
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
"github.com/containerd/console"
|
2020-12-23 12:56:13 -05:00
|
|
|
isatty "github.com/mattn/go-isatty"
|
2020-06-22 15:55:52 -04:00
|
|
|
te "github.com/muesli/termenv"
|
2021-04-29 08:24:25 -04:00
|
|
|
"golang.org/x/term"
|
2020-01-10 16:02:04 -05:00
|
|
|
)
|
|
|
|
|
2020-04-10 15:43:18 -04:00
|
|
|
// Msg represents an action and is usually the result of an IO operation. It's
|
2020-06-19 14:14:15 -04:00
|
|
|
// triggers the Update function, and henceforth, the UI.
|
2020-01-10 16:02:04 -05:00
|
|
|
type Msg interface{}
|
|
|
|
|
2020-10-15 16:08:19 -04:00
|
|
|
// Model contains the program's state as well as it's core functions.
|
|
|
|
type Model interface {
|
2020-10-18 12:43:41 -04:00
|
|
|
// Init is the first function that will be called. It returns an optional
|
|
|
|
// initial command. To not perform an initial command return nil.
|
2020-10-15 16:08:19 -04:00
|
|
|
Init() Cmd
|
|
|
|
|
|
|
|
// Update is called when a message is received. Use it to inspect messages
|
|
|
|
// and, in response, update the model and/or send a command.
|
|
|
|
Update(Msg) (Model, Cmd)
|
|
|
|
|
|
|
|
// View renders the program's UI, which is just a string. The view is
|
|
|
|
// rendered after every Update.
|
|
|
|
View() string
|
|
|
|
}
|
2020-01-10 16:02:04 -05:00
|
|
|
|
2020-06-24 12:14:35 -04:00
|
|
|
// Cmd is an IO operation. If it's nil it's considered a no-op. Use it for
|
|
|
|
// things like HTTP requests, timers, saving and loading from disk, and so on.
|
|
|
|
//
|
|
|
|
// There's almost never a need to use a command to send a message to another
|
|
|
|
// part of your program. Instead, it can almost always be done in the update
|
|
|
|
// function.
|
2020-04-22 10:15:04 -04:00
|
|
|
type Cmd func() Msg
|
2020-03-31 16:27:36 -04:00
|
|
|
|
2021-01-17 09:53:47 -05:00
|
|
|
// Batch performs a bunch of commands concurrently with no ordering guarantees
|
2020-03-31 16:08:03 -04:00
|
|
|
// about the results.
|
|
|
|
func Batch(cmds ...Cmd) Cmd {
|
2020-04-01 12:05:05 -04:00
|
|
|
if len(cmds) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-22 10:15:04 -04:00
|
|
|
return func() Msg {
|
2020-03-31 16:08:03 -04:00
|
|
|
return batchMsg(cmds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 09:53:47 -05:00
|
|
|
// ProgramOption is used to set options when initializing a Program. Program can
|
2020-12-23 12:56:13 -05:00
|
|
|
// accept a variable number of options.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
//
|
|
|
|
// p := NewProgram(model, WithInput(someInput), WithOutput(someOutput))
|
|
|
|
type ProgramOption func(*Program)
|
|
|
|
|
|
|
|
// WithOutput sets the output which, by default, is stdout. In most cases you
|
|
|
|
// won't need to use this.
|
2021-03-08 20:34:14 -05:00
|
|
|
func WithOutput(output io.Writer) ProgramOption {
|
2020-12-23 12:56:13 -05:00
|
|
|
return func(m *Program) {
|
|
|
|
m.output = output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithInput sets the input which, by default, is stdin. In most cases you
|
|
|
|
// won't need to use this.
|
|
|
|
func WithInput(input io.Reader) ProgramOption {
|
|
|
|
return func(m *Program) {
|
|
|
|
m.input = input
|
2021-02-26 18:38:52 -05:00
|
|
|
m.inputStatus = customInput
|
2020-12-23 12:56:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutCatchPanics disables the panic catching that Bubble Tea does by
|
2020-12-29 22:21:17 -05:00
|
|
|
// default. If panic catching is disabled the terminal will be in a fairly
|
|
|
|
// unusable state after a panic because Bubble Tea will not perform its usual
|
|
|
|
// cleanup on exit.
|
2020-12-23 12:56:13 -05:00
|
|
|
func WithoutCatchPanics() ProgramOption {
|
|
|
|
return func(m *Program) {
|
|
|
|
m.CatchPanics = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// WithoutRenderer disables the renderer. When this is set output and log
|
|
|
|
// statements will be plainly sent to stdout (or another output if one is set)
|
|
|
|
// without any rendering and redrawing logic. In other words, printing and
|
|
|
|
// logging will behave the same way it would in a non-TUI commandline tool.
|
|
|
|
// This can be useful if you want to use the Bubble Tea framework for a non-TUI
|
|
|
|
// application, or to provide an additional non-TUI mode to your Bubble Tea
|
|
|
|
// programs. For example, your program could behave like a daemon if output is
|
|
|
|
// not a TTY.
|
|
|
|
func WithoutRenderer() ProgramOption {
|
|
|
|
return func(m *Program) {
|
|
|
|
m.renderer = &nilRenderer{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
// inputStatus indicates the current state of the input. By default, input is
|
|
|
|
// stdin, however we'll change this if input's not a TTY. The user can also set
|
|
|
|
// the input.
|
|
|
|
type inputStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultInput = iota // generally, this will be stdin
|
|
|
|
customInput // the user explicitly set the input
|
|
|
|
managedInput // we've opened a TTY for input
|
|
|
|
)
|
|
|
|
|
|
|
|
func (i inputStatus) String() string {
|
|
|
|
return [...]string{
|
|
|
|
"default input",
|
|
|
|
"custom input",
|
|
|
|
"managed input",
|
|
|
|
}[i]
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// Program is a terminal user interface.
|
2020-01-10 16:02:04 -05:00
|
|
|
type Program struct {
|
2020-10-15 16:08:19 -04:00
|
|
|
initialModel Model
|
2020-06-22 15:55:52 -04:00
|
|
|
|
2021-02-26 15:11:23 -05:00
|
|
|
mtx *sync.Mutex
|
2020-12-23 12:56:13 -05:00
|
|
|
|
2020-12-29 22:21:17 -05:00
|
|
|
output io.Writer // where to send output. this will usually be os.Stdout.
|
2020-12-23 12:56:13 -05:00
|
|
|
input io.Reader // this will usually be os.Stdin.
|
2021-02-09 13:33:25 -05:00
|
|
|
renderer renderer
|
2020-07-13 11:39:04 -04:00
|
|
|
altScreenActive bool
|
2020-09-21 13:09:32 -04:00
|
|
|
|
2021-01-17 09:53:47 -05:00
|
|
|
// CatchPanics is incredibly useful for restoring the terminal to a usable
|
2020-09-21 13:09:32 -04:00
|
|
|
// state after a panic occurs. When this is set, Bubble Tea will recover
|
|
|
|
// from panics, print the stack trace, and disable raw mode. This feature
|
|
|
|
// is on by default.
|
|
|
|
CatchPanics bool
|
2021-01-11 16:33:14 -05:00
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
inputStatus inputStatus
|
2021-01-11 16:33:14 -05:00
|
|
|
inputIsTTY bool
|
|
|
|
outputIsTTY bool
|
2021-02-26 18:38:52 -05:00
|
|
|
console console.Console
|
|
|
|
|
|
|
|
// Stores the original reference to stdin for cases where input is not a
|
|
|
|
// TTY on windows and we've automatically opened CONIN$ to receive input.
|
|
|
|
// When the program exits this will be restored.
|
|
|
|
windowsStdin *os.File
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
|
|
|
|
2020-07-29 20:49:20 -04:00
|
|
|
// Quit is a special command that tells the Bubble Tea program to exit.
|
2020-04-22 10:15:04 -04:00
|
|
|
func Quit() Msg {
|
2020-01-10 16:02:04 -05:00
|
|
|
return quitMsg{}
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:45:28 -04:00
|
|
|
// quitMsg in an internal message signals that the program should quit. You can
|
|
|
|
// send a quitMsg with Quit.
|
2020-01-10 16:02:04 -05:00
|
|
|
type quitMsg struct{}
|
|
|
|
|
2021-05-03 14:28:45 -04:00
|
|
|
// EnterAltScreen is a special command that tells the Bubble Tea program to
|
|
|
|
// enter alternate screen buffer.
|
2021-03-08 12:48:34 -05:00
|
|
|
func EnterAltScreen() Msg {
|
|
|
|
return enterAltScreenMsg{}
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:28:45 -04:00
|
|
|
// enterAltScreenMsg in an internal message signals that the program should
|
|
|
|
// enter alternate screen buffer. You can send a enterAltScreenMsg with
|
|
|
|
// EnterAltScreen.
|
2021-03-08 12:48:34 -05:00
|
|
|
type enterAltScreenMsg struct{}
|
|
|
|
|
|
|
|
// ExitAltScreen is a special command that tells the Bubble Tea program to exit
|
|
|
|
// alternate screen buffer.
|
|
|
|
func ExitAltScreen() Msg {
|
|
|
|
return exitAltScreenMsg{}
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:27:20 -04:00
|
|
|
// enableMouseCellMotionMsg is a special command that signals to start
|
|
|
|
// listening for "cell motion" type mouse events (ESC[?1002l). To send an
|
|
|
|
// enableMouseCellMotionMsg, use the EnableMouseCellMotion command.
|
|
|
|
type enableMouseCellMotionMsg struct{}
|
|
|
|
|
|
|
|
// EnableMouseCellMotion is a special command that enables mouse click,
|
|
|
|
// release, wheel, events. Mouse movement events are also captured if a mouse
|
|
|
|
// button is pressed (i.e., drag events).
|
|
|
|
func EnableMouseCellMotion() Msg {
|
|
|
|
return enableMouseCellMotionMsg{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enableMouseAllMotionMsg is a special command that signals to start listening
|
|
|
|
// for "all motion" type mouse events (ESC[?1003l). To send an
|
|
|
|
// enableMouseAllMotionMsg, use the EnableMouseAllMotion command.
|
|
|
|
type enableMouseAllMotionMsg struct{}
|
|
|
|
|
|
|
|
// EnableMouseAllMotion is a special command that enables mouse click, release,
|
|
|
|
// and wheel events. Mouse movement events are delivered regardless of whether
|
|
|
|
// a button is pressed. Many modern terminals support this, but not all. If in
|
|
|
|
// doubt, use EnableMouseCellMotion instead.
|
|
|
|
func EnableMouseAllMotion() Msg {
|
|
|
|
return enableMouseAllMotionMsg{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// disableMouseMsg is an internal message that that signals to stop listening
|
|
|
|
// for mouse events. To send a disableMouseMsg, use the DisableMouse command.
|
|
|
|
type disableMouseMsg struct{}
|
|
|
|
|
|
|
|
// DisableMouse is a special command that stops listening for mouse events.
|
|
|
|
func DisableMouse() Msg {
|
|
|
|
return disableMouseMsg{}
|
|
|
|
}
|
|
|
|
|
2021-03-08 12:48:34 -05:00
|
|
|
// exitAltScreenMsg in an internal message signals that the program should exit
|
|
|
|
// alternate screen buffer. You can send a exitAltScreenMsg with ExitAltScreen.
|
|
|
|
type exitAltScreenMsg struct{}
|
|
|
|
|
2020-06-05 13:45:28 -04:00
|
|
|
// batchMsg is the internal message used to perform a bunch of commands. You
|
|
|
|
// can send a batchMsg with Batch.
|
2020-03-31 16:08:03 -04:00
|
|
|
type batchMsg []Cmd
|
|
|
|
|
2020-10-05 14:42:09 -04:00
|
|
|
// WindowSizeMsg is used to report on the terminal size. It's sent to Update
|
2020-07-29 20:54:15 -04:00
|
|
|
// once initially and then on every terminal resize.
|
2020-06-16 16:41:35 -04:00
|
|
|
type WindowSizeMsg struct {
|
2020-06-17 18:19:27 -04:00
|
|
|
Width int
|
|
|
|
Height int
|
2020-06-16 16:41:35 -04:00
|
|
|
}
|
|
|
|
|
2020-12-03 10:44:10 -05:00
|
|
|
// HideCursor is a special command for manually instructing Bubble Tea to hide
|
|
|
|
// the cursor. In some rare cases, certain operations will cause the terminal
|
|
|
|
// to show the cursor, which is normally hidden for the duration of a Bubble
|
2021-05-03 14:27:20 -04:00
|
|
|
// Tea program's lifetime. You will most likely not need to use this command.
|
2020-12-03 10:44:10 -05:00
|
|
|
func HideCursor() Msg {
|
|
|
|
return hideCursorMsg{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hideCursorMsg is an internal command used to hide the cursor. You can send
|
|
|
|
// this message with HideCursor.
|
|
|
|
type hideCursorMsg struct{}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// NewProgram creates a new Program.
|
2020-12-23 12:56:13 -05:00
|
|
|
func NewProgram(model Model, opts ...ProgramOption) *Program {
|
|
|
|
p := &Program{
|
2021-02-26 15:11:23 -05:00
|
|
|
mtx: &sync.Mutex{},
|
2020-10-15 16:08:19 -04:00
|
|
|
initialModel: model,
|
|
|
|
output: os.Stdout,
|
2020-12-23 12:56:13 -05:00
|
|
|
input: os.Stdin,
|
2020-10-15 16:08:19 -04:00
|
|
|
CatchPanics: true,
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
2020-12-23 12:56:13 -05:00
|
|
|
|
|
|
|
// Apply all options to program
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
|
|
|
|
2020-05-25 08:02:46 -04:00
|
|
|
// Start initializes the program.
|
2020-01-10 16:02:04 -05:00
|
|
|
func (p *Program) Start() error {
|
|
|
|
var (
|
2020-07-13 11:39:04 -04:00
|
|
|
cmds = make(chan Cmd)
|
|
|
|
msgs = make(chan Msg)
|
|
|
|
errs = make(chan error)
|
|
|
|
done = make(chan struct{})
|
2021-01-11 16:33:14 -05:00
|
|
|
|
|
|
|
// If output is a file (e.g. os.Stdout) then this will be set
|
|
|
|
// accordingly. Most of the time you should refer to p.outputIsTTY
|
|
|
|
// rather than do a nil check against the value here.
|
|
|
|
outputAsFile *os.File
|
2020-01-10 16:02:04 -05:00
|
|
|
)
|
|
|
|
|
2021-01-11 16:33:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Is output a terminal?
|
|
|
|
if f, ok := p.output.(*os.File); ok {
|
|
|
|
outputAsFile = f
|
|
|
|
p.outputIsTTY = isatty.IsTerminal(f.Fd())
|
|
|
|
}
|
2020-12-29 22:21:17 -05:00
|
|
|
|
2021-01-11 16:33:14 -05:00
|
|
|
// Is input a terminal?
|
|
|
|
if f, ok := p.input.(*os.File); ok {
|
|
|
|
p.inputIsTTY = isatty.IsTerminal(f.Fd())
|
2020-12-29 22:21:17 -05:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
// If input is not a terminal, and the user hasn't set a custom input, open
|
|
|
|
// a TTY so we can capture input as normal. This will allow things to "just
|
|
|
|
// work" in cases where data was piped or redirected into this application.
|
|
|
|
if !p.inputIsTTY && p.inputStatus != customInput {
|
|
|
|
f, err := openInputTTY()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.input = f
|
|
|
|
p.inputIsTTY = true
|
|
|
|
p.inputStatus = managedInput
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:33:14 -05:00
|
|
|
// Listen for SIGINT. Note that in most cases ^C will not send an
|
|
|
|
// interrupt because the terminal will be in raw mode and thus capture
|
|
|
|
// that keystroke and send it along to Program.Update. If input is not a
|
|
|
|
// TTY, however, ^C will be caught here.
|
|
|
|
go func() {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGINT)
|
|
|
|
defer signal.Stop(sig)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-sig:
|
|
|
|
msgs <- quitMsg{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-09-21 13:09:32 -04:00
|
|
|
if p.CatchPanics {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
p.ExitAltScreen()
|
2021-05-04 21:47:24 -04:00
|
|
|
p.DisableMouseCellMotion()
|
|
|
|
p.DisableMouseAllMotion()
|
2020-10-08 07:54:53 -04:00
|
|
|
fmt.Printf("Caught panic:\n\n%s\n\nRestoring terminal...\n\n", r)
|
2020-09-21 13:09:32 -04:00
|
|
|
debug.PrintStack()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
// If no renderer is set use the standard one.
|
|
|
|
if p.renderer == nil {
|
|
|
|
p.renderer = newRenderer(p.output, p.mtx)
|
|
|
|
}
|
2020-07-13 11:39:04 -04:00
|
|
|
|
2020-12-23 12:56:13 -05:00
|
|
|
// Check if output is a TTY before entering raw mode, hiding the cursor and
|
|
|
|
// so on.
|
2021-01-11 16:33:14 -05:00
|
|
|
{
|
|
|
|
err := p.initTerminal()
|
2020-12-23 12:56:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 19:20:58 -05:00
|
|
|
defer func() {
|
|
|
|
_ = p.restoreTerminal()
|
|
|
|
}()
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 22:18:19 -05:00
|
|
|
// Initialize program
|
2020-10-15 16:08:19 -04:00
|
|
|
model := p.initialModel
|
|
|
|
initCmd := model.Init()
|
2020-06-23 16:11:06 -04:00
|
|
|
if initCmd != nil {
|
2020-01-18 22:18:19 -05:00
|
|
|
go func() {
|
2020-06-23 16:11:06 -04:00
|
|
|
cmds <- initCmd
|
2020-01-18 22:18:19 -05:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-06-05 12:40:44 -04:00
|
|
|
// Start renderer
|
2020-07-13 11:39:04 -04:00
|
|
|
p.renderer.start()
|
2021-02-09 13:33:25 -05:00
|
|
|
p.renderer.setAltScreen(p.altScreenActive)
|
2020-06-05 12:40:44 -04:00
|
|
|
|
2020-01-10 16:02:04 -05:00
|
|
|
// Render initial view
|
2020-10-15 16:08:19 -04:00
|
|
|
p.renderer.write(model.View())
|
2020-01-10 16:02:04 -05:00
|
|
|
|
2020-05-12 17:56:30 -04:00
|
|
|
// Subscribe to user input
|
2021-01-11 16:33:14 -05:00
|
|
|
if p.inputIsTTY {
|
2020-12-23 12:56:13 -05:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := readInput(p.input)
|
|
|
|
if err != nil {
|
|
|
|
// If we get EOF just stop listening for input
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
msgs <- msg
|
2020-04-22 10:32:18 -04:00
|
|
|
}
|
2020-12-23 12:56:13 -05:00
|
|
|
}()
|
|
|
|
}
|
2020-01-10 16:02:04 -05:00
|
|
|
|
2021-01-11 16:33:14 -05:00
|
|
|
if p.outputIsTTY {
|
2020-12-29 22:21:17 -05:00
|
|
|
// Get initial terminal size
|
|
|
|
go func() {
|
2021-04-29 08:24:25 -04:00
|
|
|
w, h, err := term.GetSize(int(outputAsFile.Fd()))
|
2020-12-29 22:21:17 -05:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
msgs <- WindowSizeMsg{w, h}
|
|
|
|
}()
|
2020-06-16 16:41:35 -04:00
|
|
|
|
2020-12-29 22:21:17 -05:00
|
|
|
// Listen for window resizes
|
|
|
|
go listenForResize(outputAsFile, msgs, errs)
|
|
|
|
}
|
2020-06-16 16:41:35 -04:00
|
|
|
|
2020-01-10 16:02:04 -05:00
|
|
|
// Process commands
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case cmd := <-cmds:
|
|
|
|
if cmd != nil {
|
|
|
|
go func() {
|
2020-04-22 10:15:04 -04:00
|
|
|
msgs <- cmd()
|
2020-01-10 16:02:04 -05:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Handle updates and draw
|
|
|
|
for {
|
|
|
|
select {
|
2020-04-22 10:32:18 -04:00
|
|
|
case err := <-errs:
|
|
|
|
close(done)
|
|
|
|
return err
|
2020-01-10 16:02:04 -05:00
|
|
|
case msg := <-msgs:
|
2020-03-31 16:08:03 -04:00
|
|
|
|
2021-05-06 13:48:46 -04:00
|
|
|
// Handle special internal messages
|
|
|
|
switch msg := msg.(type) {
|
2020-12-03 10:44:10 -05:00
|
|
|
case quitMsg:
|
2021-03-08 12:48:34 -05:00
|
|
|
p.ExitAltScreen()
|
2021-05-03 14:27:20 -04:00
|
|
|
p.DisableMouseCellMotion()
|
|
|
|
p.DisableMouseAllMotion()
|
2020-07-13 11:39:04 -04:00
|
|
|
p.renderer.stop()
|
2020-01-10 16:02:04 -05:00
|
|
|
close(done)
|
|
|
|
return nil
|
2021-05-03 14:27:20 -04:00
|
|
|
|
2021-05-06 13:48:46 -04:00
|
|
|
case batchMsg:
|
|
|
|
for _, cmd := range msg {
|
|
|
|
cmds <- cmd
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
2021-03-08 12:48:34 -05:00
|
|
|
case enterAltScreenMsg:
|
|
|
|
p.EnterAltScreen()
|
2021-05-03 14:27:20 -04:00
|
|
|
|
2021-03-08 12:48:34 -05:00
|
|
|
case exitAltScreenMsg:
|
|
|
|
p.ExitAltScreen()
|
2021-05-03 14:27:20 -04:00
|
|
|
|
|
|
|
case enableMouseCellMotionMsg:
|
|
|
|
p.EnableMouseCellMotion()
|
|
|
|
|
|
|
|
case enableMouseAllMotionMsg:
|
|
|
|
p.EnableMouseAllMotion()
|
|
|
|
|
|
|
|
case disableMouseMsg:
|
|
|
|
p.DisableMouseCellMotion()
|
|
|
|
p.DisableMouseAllMotion()
|
|
|
|
|
2020-12-03 10:44:10 -05:00
|
|
|
case hideCursorMsg:
|
|
|
|
hideCursor(p.output)
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:43:06 -04:00
|
|
|
// Process internal messages for the renderer
|
2021-02-09 13:33:25 -05:00
|
|
|
if r, ok := p.renderer.(*standardRenderer); ok {
|
|
|
|
r.handleMessages(msg)
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:11:06 -04:00
|
|
|
var cmd Cmd
|
2020-10-15 16:08:19 -04:00
|
|
|
model, cmd = model.Update(msg) // run update
|
|
|
|
cmds <- cmd // process command (if any)
|
|
|
|
p.renderer.write(model.View()) // send view to renderer
|
2020-01-10 16:02:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:49:20 -04:00
|
|
|
// EnterAltScreen enters the alternate screen buffer, which consumes the entire
|
|
|
|
// terminal window. ExitAltScreen will return the terminal to its former state.
|
2021-05-03 14:28:45 -04:00
|
|
|
//
|
|
|
|
// Deprecated. Use the EnterAltScreen() command instead.
|
2020-06-22 15:55:52 -04:00
|
|
|
func (p *Program) EnterAltScreen() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2021-03-08 12:48:34 -05:00
|
|
|
|
|
|
|
if p.altScreenActive {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:52 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.AltScreenSeq)
|
2020-06-22 20:47:35 -04:00
|
|
|
moveCursor(p.output, 0, 0)
|
2020-07-13 11:39:04 -04:00
|
|
|
|
|
|
|
p.altScreenActive = true
|
|
|
|
if p.renderer != nil {
|
2021-02-09 13:33:25 -05:00
|
|
|
p.renderer.setAltScreen(p.altScreenActive)
|
2020-07-13 11:39:04 -04:00
|
|
|
}
|
2020-02-01 21:02:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 14:28:08 -04:00
|
|
|
// ExitAltScreen exits the alternate screen buffer.
|
2021-05-03 14:28:45 -04:00
|
|
|
//
|
|
|
|
// Deprecated. Use the ExitAltScreen() command instead.
|
2020-06-22 15:55:52 -04:00
|
|
|
func (p *Program) ExitAltScreen() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2021-03-08 12:48:34 -05:00
|
|
|
|
|
|
|
if !p.altScreenActive {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:55:52 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.ExitAltScreenSeq)
|
2020-07-13 11:39:04 -04:00
|
|
|
|
|
|
|
p.altScreenActive = false
|
|
|
|
if p.renderer != nil {
|
2021-02-09 13:33:25 -05:00
|
|
|
p.renderer.setAltScreen(p.altScreenActive)
|
2020-07-13 11:39:04 -04:00
|
|
|
}
|
2020-02-01 21:02:12 -05:00
|
|
|
}
|
2020-06-22 20:30:16 -04:00
|
|
|
|
2020-07-29 20:49:20 -04:00
|
|
|
// EnableMouseCellMotion enables mouse click, release, wheel and motion events
|
2021-05-03 14:27:20 -04:00
|
|
|
// if a mouse button is pressed (i.e., drag events).
|
|
|
|
//
|
|
|
|
// Deprecated. Use the EnableMouseCellMotion() command instead.
|
2020-06-22 20:30:16 -04:00
|
|
|
func (p *Program) EnableMouseCellMotion() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2020-06-25 12:42:31 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.EnableMouseCellMotionSeq)
|
2020-06-22 20:30:16 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 14:27:20 -04:00
|
|
|
// DisableMouseCellMotion disables Mouse Cell Motion tracking. This will be
|
|
|
|
// called automatically when exiting a Bubble Tea program.
|
|
|
|
//
|
|
|
|
// Deprecated. Use the DisableMouse() command instead.
|
2020-06-22 20:30:16 -04:00
|
|
|
func (p *Program) DisableMouseCellMotion() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2020-06-25 12:42:31 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.DisableMouseCellMotionSeq)
|
2020-06-22 20:30:16 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:01:23 -04:00
|
|
|
// EnableMouseAllMotion enables mouse click, release, wheel and motion events,
|
2021-05-03 14:27:20 -04:00
|
|
|
// regardless of whether a mouse button is pressed. Many modern terminals
|
|
|
|
// support this, but not all.
|
|
|
|
//
|
|
|
|
// Deprecated. Use the EnableMouseAllMotion() command instead.
|
2020-06-22 20:30:16 -04:00
|
|
|
func (p *Program) EnableMouseAllMotion() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2020-06-25 12:42:31 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.EnableMouseAllMotionSeq)
|
2020-06-22 20:30:16 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 14:27:20 -04:00
|
|
|
// DisableMouseAllMotion disables All Motion mouse tracking. This will be
|
|
|
|
// called automatically when exiting a Bubble Tea program.
|
|
|
|
//
|
|
|
|
// Deprecated. Use the DisableMouse() command instead.
|
2020-06-22 20:30:16 -04:00
|
|
|
func (p *Program) DisableMouseAllMotion() {
|
|
|
|
p.mtx.Lock()
|
|
|
|
defer p.mtx.Unlock()
|
2020-06-25 12:42:31 -04:00
|
|
|
fmt.Fprintf(p.output, te.CSI+te.DisableMouseAllMotionSeq)
|
2020-06-22 20:30:16 -04:00
|
|
|
}
|