Overview
TheProgram type is the core of a Bubble Tea application. It manages the event loop, rendering, input handling, and lifecycle of your terminal user interface.
Creating a Program
NewProgram
Creates a new Program with the given model and options.The initial model for the program. This is the only required parameter.
Variable number of options to configure the program. See ProgramOption for available options.
A pointer to the newly created Program instance.
Program Methods
Run
Initializes the program and runs its event loops, blocking until it gets terminated.The final model state when the program exits.
An error if the program was terminated abnormally. Common errors include:
ErrProgramPanic: Program recovered from a panicErrProgramKilled: Program was killedErrInterrupted: Program received SIGINT or InterruptMsg
Send
Sends a message to the main update function, allowing messages to be injected from outside the program.The message to send to the update function.
If the program hasn’t started yet, this will be a blocking operation. If the program has already terminated, this will be a no-op.
Quit
Convenience function for quitting Bubble Tea programs from outside.To quit from within a Bubble Tea program, use the
Quit() command in your Update function instead.Kill
Stops the program immediately and restores the former terminal state. The final render is skipped.Wait
Waits/blocks until the underlying Program finishes shutting down.Println
Prints above the Program. This output is unmanaged and will persist across renders.Values to print, similar to
fmt.Println.If the altscreen is active, no output will be printed.
Printf
Prints above the Program with formatting. Similar tolog.Printf, the message is printed on its own line.
Format template string.
Values for the format template.
ReleaseTerminal
Restores the original terminal state and cancels the input reader. Use withRestoreTerminal() to temporarily release control.
Error if terminal state restoration fails.
RestoreTerminal
Reinitializes the Program’s input reader and restores the terminal to the state when the program was running.Error if terminal restoration fails.
ProgramOption
Program options are functions that configure a Program. They are passed toNewProgram.
WithContext
Specifies a context in which to run the Program. Useful for cancellation from outside.WithInput
Sets the input source (defaults toos.Stdin). Pass nil to disable input entirely.
WithOutput
Sets the output destination (defaults toos.Stdout).
WithEnvironment
Sets environment variables that the program will use. Useful for remote sessions (e.g., SSH).WithoutSignalHandler
Disables the signal handler that Bubble Tea sets up. Use when you want to handle signals yourself.WithoutCatchPanics
Disables panic catching. If disabled, the terminal will be unusable after a panic.WithoutRenderer
Disables the renderer. Output and log statements are sent plainly to stdout without rendering logic.WithFilter
Supplies an event filter invoked before Bubble Tea processes messages. Returnnil to ignore a message.
WithFPS
Sets a custom maximum FPS for the renderer (default: 60, max: 120).WithColorProfile
Forces a specific color profile instead of auto-detection.WithWindowSize
Sets the initial terminal window size. Useful for testing or non-interactive environments.Error Types
The following errors can be returned byProgram.Run():
ErrProgramPanic
ErrProgramKilled
Kill() or context cancellation.
ErrInterrupted
InterruptMsg.