NewProgram to configure input/output, context, rendering, and more.
Usage
ProgramOption
A function type used to set options when initializing a Program. Programs can accept a variable number of options.Example:
Context Options
WithContext
Specifies a context in which to run the Program. This is useful if you want to cancel the execution from outside. When a Program gets cancelled it will exit with an error When to use:
ErrProgramKilled.Parameters:ctx- The context to use for program execution
- You need to cancel the program from external code
- You want to implement timeouts
- You need to coordinate shutdown with other goroutines
Input/Output Options
WithOutput
Sets the output which, by default, is stdout. In most cases you won’t need to use this.Parameters:When to use:
output- The io.Writer to use for program output
- Testing: Capture output to a buffer for assertions
- Logging: Redirect output to a file or custom writer
- Multiple outputs: Use io.MultiWriter to send output to multiple destinations
WithInput
Sets the input which, by default, is stdin. In most cases you won’t need to use this. To disable input entirely, pass nil.Parameters:When to use:
input- The io.Reader to use for program input, or nil to disable input
- Testing: Provide predetermined input
- Non-interactive mode: Disable input when running as a daemon
- Custom input source: Read from a file or network connection
Environment Options
WithEnvironment
Sets the environment variables that the program will use. This is useful when the program is running in a remote session (e.g. SSH) and you want to pass the environment variables from the remote session to the program.Parameters:When to use:
env- A slice of strings in the format “KEY=value”
- Running in SSH sessions or remote environments
- Testing with specific environment variables
- Overriding terminal capabilities or color settings
Signal Handling Options
WithoutSignalHandler
Disables the signal handler that Bubble Tea sets up for Programs. This is useful if you want to handle signals yourself.Example:When to use:
- You need custom signal handling logic
- Your application has its own signal management
- You’re embedding Bubble Tea in a larger application with existing signal handlers
WithoutSignals
Ignores OS signals entirely. This is mainly useful for testing.Example:When to use:
- Testing: Prevent signals from interfering with tests
- Controlled environments: Running in containers or sandboxes where signals should be ignored
Error Handling Options
WithoutCatchPanics
Disables the panic catching that Bubble Tea does by 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.Example:When to use:
- Debugging: See the full stack trace without Bubble Tea’s panic handling
- Development: Let panics crash the program to identify bugs early
- Custom panic recovery: You have your own panic handling mechanism
Rendering Options
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.Example:When to use:
- Non-interactive mode: Provide a daemon or CLI mode for your application
- Piped output: Detect when output is redirected and disable TUI
- Logging: Use Bubble Tea’s architecture without the full-screen interface
- Continuous output: Stream logs or progress without redraws
WithFPS
Sets a custom maximum FPS at which the renderer should run. If less than 1, the default value of 60 will be used. If over 120, the FPS will be capped at 120.Parameters:When to use:
fps- The target frames per second (1-120)
- Performance optimization: Lower FPS to reduce CPU usage
- Smooth animations: Increase FPS for smoother visual effects
- Resource-constrained environments: Reduce FPS when CPU is limited
WithColorProfile
Sets the color profile that the program will use. This is useful when you want to force a specific color profile. By default, Bubble Tea will try to detect the terminal’s color profile from environment variables and terminfo capabilities.Parameters:When to use:
profile- A colorprofile.Profile (TrueColor, ANSI256, ANSI, or Ascii)
- Testing: Ensure consistent output across different environments
- Legacy terminals: Force compatibility with older terminal emulators
- User preference: Let users choose their color mode
- SSH/remote sessions: Override incorrect terminal detection
tea.WithEnvironment to set custom environment variables that may affect color detection.WithWindowSize
Sets the initial size of the terminal window. This is useful when you need to set the initial size of the terminal window, for example during testing or when you want to run your program in a non-interactive environment.Parameters:When to use:
width- The initial window width in columnsheight- The initial window height in rows
- Testing: Ensure consistent dimensions for snapshot tests
- Non-TTY environments: Provide dimensions when terminal size detection fails
- Screenshots/demos: Set specific dimensions for documentation
Event Filtering Options
WithFilter
Supplies an event filter that will be invoked before Bubble Tea processes a tea.Msg. The event filter can return any tea.Msg which will then get handled by Bubble Tea instead of the original event. If the event filter returns nil, the event will be ignored and Bubble Tea will not process it.Parameters:Example - Transform messages:When to use:
filter- A function that takes a Model and Msg, and returns a Msg (or nil to ignore)
- Preventing accidental exits with unsaved data
- Global event transformation or routing
- Implementing confirmation dialogs for dangerous actions
- Debugging: Log all messages before they’re processed