diff --git a/tea.go b/tea.go index 129fe8f..96b6aae 100644 --- a/tea.go +++ b/tea.go @@ -27,11 +27,11 @@ import ( "golang.org/x/term" ) -// Msg represents an action and is usually the result of an IO operation. It -// triggers the Update function, and henceforth, the UI. +// Msg contain data from the result of a IO operation. Msgs trigger the update +// function and, henceforth, the UI. 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 { // Init is the first function that will be called. It returns an optional // initial command. To not perform an initial command return nil. @@ -46,12 +46,13 @@ type Model interface { View() string } -// 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. +// Cmd is an IO operation that returns a message when it's complete. 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. +// Note that there's almost never a reason to use a command to send a message +// to another part of your program. That can almost always be done in the +// update function. type Cmd func() Msg // Options to customize the program during its initialization. These are @@ -139,7 +140,7 @@ func Quit() Msg { type quitMsg struct{} // 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 // 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, } - // Apply all options to program + // Apply all options to the program. for _, opt := range opts { opt(p) } @@ -257,7 +258,7 @@ func (p *Program) Start() error { errs = make(chan error) ) - // channels for managing goroutine lifecycles + // Channels for managing goroutine lifecycles. var ( readLoopDone = make(chan struct{}) sigintLoopDone = make(chan struct{}) @@ -270,9 +271,9 @@ func (p *Program) Start() error { select { case <-readLoopDone: case <-time.After(500 * time.Millisecond): - // the read loop hangs, which means the input cancelReader's - // cancel function has returned true even though it was not - // able to cancel the read + // The read loop hangs, which means the input + // cancelReader's cancel function has returned true even + // though it was not able to cancel the read. } } <-cmdLoopDone @@ -372,7 +373,7 @@ func (p *Program) Start() error { p.EnableMouseAllMotion() } - // Initialize program + // Initialize the program. model := p.initialModel if initCmd := model.Init(); initCmd != nil { go func() { @@ -386,11 +387,11 @@ func (p *Program) Start() error { close(initSignalDone) } - // Start renderer + // Start the renderer. p.renderer.start() p.renderer.setAltScreen(p.altScreenActive) - // Render initial view + // Render the initial view. p.renderer.write(model.View()) cancelReader, err := newCancelReader(p.input) @@ -400,7 +401,7 @@ func (p *Program) Start() error { defer cancelReader.Close() // nolint:errcheck - // Subscribe to user input + // Subscribe to user input. if p.input != nil { go func() { defer close(readLoopDone) @@ -427,7 +428,7 @@ func (p *Program) Start() error { } 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() { w, h, err := term.GetSize(int(f.Fd())) 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) } else { close(resizeLoopDone) } - // Process commands + // Process commands. go func() { defer close(cmdLoopDone) @@ -475,7 +476,7 @@ func (p *Program) Start() error { } }() - // Handle updates and draw + // Handle updates and draw. for { select { case err := <-errs: @@ -486,7 +487,7 @@ func (p *Program) Start() error { return err case msg := <-p.msgs: - // Handle special internal messages + // Handle special internal messages. switch msg := msg.(type) { case quitMsg: cancelContext() @@ -523,7 +524,7 @@ func (p *Program) Start() error { hideCursor(p.output) } - // Process internal messages for the renderer + // Process internal messages for the renderer. if r, ok := p.renderer.(*standardRenderer); ok { r.handleMessages(msg) }