forked from Mirrors/bubbletea
Tidy up comments
This commit is contained in:
parent
860f623112
commit
0e7cd09bf6
49
tea.go
49
tea.go
|
@ -27,11 +27,11 @@ import (
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Msg represents an action and is usually the result of an IO operation. It
|
// Msg contain data from the result of a IO operation. Msgs trigger the update
|
||||||
// triggers the Update function, and henceforth, the UI.
|
// function and, henceforth, the UI.
|
||||||
type Msg interface{}
|
type Msg interface{}
|
||||||
|
|
||||||
// Model contains the program's state as well as it's core functions.
|
// Model contains the program's state as well as its core functions.
|
||||||
type Model interface {
|
type Model interface {
|
||||||
// Init is the first function that will be called. It returns an optional
|
// Init is the first function that will be called. It returns an optional
|
||||||
// initial command. To not perform an initial command return nil.
|
// initial command. To not perform an initial command return nil.
|
||||||
|
@ -46,12 +46,13 @@ type Model interface {
|
||||||
View() string
|
View() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cmd is an IO operation. If it's nil it's considered a no-op. Use it for
|
// Cmd is an IO operation that returns a message when it's complete. If it's
|
||||||
// things like HTTP requests, timers, saving and loading from disk, and so on.
|
// 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
|
// Note that there's almost never a reason to use a command to send a message
|
||||||
// part of your program. Instead, it can almost always be done in the update
|
// to another part of your program. That can almost always be done in the
|
||||||
// function.
|
// update function.
|
||||||
type Cmd func() Msg
|
type Cmd func() Msg
|
||||||
|
|
||||||
// Options to customize the program during its initialization. These are
|
// Options to customize the program during its initialization. These are
|
||||||
|
@ -139,7 +140,7 @@ func Quit() Msg {
|
||||||
type quitMsg struct{}
|
type quitMsg struct{}
|
||||||
|
|
||||||
// EnterAltScreen is a special command that tells the Bubble Tea program to
|
// EnterAltScreen is a special command that tells the Bubble Tea program to
|
||||||
// enter alternate screen buffer.
|
// enter the alternate screen buffer.
|
||||||
//
|
//
|
||||||
// Because commands run asynchronously, this command should not be used in your
|
// Because commands run asynchronously, this command should not be used in your
|
||||||
// model's Init function. To initialize your program with the altscreen enabled
|
// model's Init function. To initialize your program with the altscreen enabled
|
||||||
|
@ -240,7 +241,7 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
|
||||||
CatchPanics: true,
|
CatchPanics: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply all options to program
|
// Apply all options to the program.
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(p)
|
opt(p)
|
||||||
}
|
}
|
||||||
|
@ -257,7 +258,7 @@ func (p *Program) Start() error {
|
||||||
errs = make(chan error)
|
errs = make(chan error)
|
||||||
)
|
)
|
||||||
|
|
||||||
// channels for managing goroutine lifecycles
|
// Channels for managing goroutine lifecycles.
|
||||||
var (
|
var (
|
||||||
readLoopDone = make(chan struct{})
|
readLoopDone = make(chan struct{})
|
||||||
sigintLoopDone = make(chan struct{})
|
sigintLoopDone = make(chan struct{})
|
||||||
|
@ -270,9 +271,9 @@ func (p *Program) Start() error {
|
||||||
select {
|
select {
|
||||||
case <-readLoopDone:
|
case <-readLoopDone:
|
||||||
case <-time.After(500 * time.Millisecond):
|
case <-time.After(500 * time.Millisecond):
|
||||||
// the read loop hangs, which means the input cancelReader's
|
// The read loop hangs, which means the input
|
||||||
// cancel function has returned true even though it was not
|
// cancelReader's cancel function has returned true even
|
||||||
// able to cancel the read
|
// though it was not able to cancel the read.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<-cmdLoopDone
|
<-cmdLoopDone
|
||||||
|
@ -372,7 +373,7 @@ func (p *Program) Start() error {
|
||||||
p.EnableMouseAllMotion()
|
p.EnableMouseAllMotion()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize program
|
// Initialize the program.
|
||||||
model := p.initialModel
|
model := p.initialModel
|
||||||
if initCmd := model.Init(); initCmd != nil {
|
if initCmd := model.Init(); initCmd != nil {
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -386,11 +387,11 @@ func (p *Program) Start() error {
|
||||||
close(initSignalDone)
|
close(initSignalDone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start renderer
|
// Start the renderer.
|
||||||
p.renderer.start()
|
p.renderer.start()
|
||||||
p.renderer.setAltScreen(p.altScreenActive)
|
p.renderer.setAltScreen(p.altScreenActive)
|
||||||
|
|
||||||
// Render initial view
|
// Render the initial view.
|
||||||
p.renderer.write(model.View())
|
p.renderer.write(model.View())
|
||||||
|
|
||||||
cancelReader, err := newCancelReader(p.input)
|
cancelReader, err := newCancelReader(p.input)
|
||||||
|
@ -400,7 +401,7 @@ func (p *Program) Start() error {
|
||||||
|
|
||||||
defer cancelReader.Close() // nolint:errcheck
|
defer cancelReader.Close() // nolint:errcheck
|
||||||
|
|
||||||
// Subscribe to user input
|
// Subscribe to user input.
|
||||||
if p.input != nil {
|
if p.input != nil {
|
||||||
go func() {
|
go func() {
|
||||||
defer close(readLoopDone)
|
defer close(readLoopDone)
|
||||||
|
@ -427,7 +428,7 @@ func (p *Program) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if f, ok := p.output.(*os.File); ok {
|
if f, ok := p.output.(*os.File); ok {
|
||||||
// Get initial terminal size and send it to the program
|
// Get the initial terminal size and send it to the program.
|
||||||
go func() {
|
go func() {
|
||||||
w, h, err := term.GetSize(int(f.Fd()))
|
w, h, err := term.GetSize(int(f.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -440,13 +441,13 @@ func (p *Program) Start() error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Listen for window resizes
|
// Listen for window resizes.
|
||||||
go listenForResize(ctx, f, p.msgs, errs, resizeLoopDone)
|
go listenForResize(ctx, f, p.msgs, errs, resizeLoopDone)
|
||||||
} else {
|
} else {
|
||||||
close(resizeLoopDone)
|
close(resizeLoopDone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process commands
|
// Process commands.
|
||||||
go func() {
|
go func() {
|
||||||
defer close(cmdLoopDone)
|
defer close(cmdLoopDone)
|
||||||
|
|
||||||
|
@ -475,7 +476,7 @@ func (p *Program) Start() error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Handle updates and draw
|
// Handle updates and draw.
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-errs:
|
case err := <-errs:
|
||||||
|
@ -486,7 +487,7 @@ func (p *Program) Start() error {
|
||||||
return err
|
return err
|
||||||
case msg := <-p.msgs:
|
case msg := <-p.msgs:
|
||||||
|
|
||||||
// Handle special internal messages
|
// Handle special internal messages.
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case quitMsg:
|
case quitMsg:
|
||||||
cancelContext()
|
cancelContext()
|
||||||
|
@ -523,7 +524,7 @@ func (p *Program) Start() error {
|
||||||
hideCursor(p.output)
|
hideCursor(p.output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process internal messages for the renderer
|
// Process internal messages for the renderer.
|
||||||
if r, ok := p.renderer.(*standardRenderer); ok {
|
if r, ok := p.renderer.(*standardRenderer); ok {
|
||||||
r.handleMessages(msg)
|
r.handleMessages(msg)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue