Skip to content
Myr

options.go

This file

Path

options.go

175 lines · 5.5 KiB · Go

Last changed by

docs: fix command and environment godoc examples (#1833)Hsnbefore adoption33f6a40

History of this file

Take it

1package tea23import (4	"context"5	"io"6	"sync/atomic"78	"github.com/charmbracelet/colorprofile"9)1011// ProgramOption is used to set options when initializing a Program. Program can12// accept a variable number of options.13//14// Example usage:15//16//	p := NewProgram(model, WithInput(someInput), WithOutput(someOutput))17type ProgramOption func(*Program)1819// WithContext lets you specify a context in which to run the Program. This is20// useful if you want to cancel the execution from outside. When a Program gets21// cancelled it will exit with an error ErrProgramKilled.22func WithContext(ctx context.Context) ProgramOption {23	return func(p *Program) {24		p.externalCtx = ctx25	}26}2728// WithOutput sets the output which, by default, is stdout. In most cases you29// won't need to use this.30func WithOutput(output io.Writer) ProgramOption {31	return func(p *Program) {32		p.output = output33	}34}3536// WithInput sets the input which, by default, is stdin. In most cases you37// won't need to use this. To disable input entirely pass nil.38//39//	p := NewProgram(model, WithInput(nil))40//41// Note that with input disabled, no terminal query is ever sent, since the42// reply could not be read: commands that expect a response, such as43// [RequestBackgroundColor], [RequestCursorPosition], [ReadClipboard], or44// [RequestTerminalVersion], are skipped, as is the startup capability probe.45// A model waiting on the reply message of any of those commands will never46// receive it.47func WithInput(input io.Reader) ProgramOption {48	return func(p *Program) {49		p.input = input50		p.disableInput = input == nil51	}52}5354// WithEnvironment sets the environment variables that the program will use.55// This useful when the program is running in a remote session (e.g. SSH) and56// you want to pass the environment variables from the remote session to the57// program.58//59// Example:60//61//	var sess ssh.Session // ssh.Session is a type from the github.com/charmbracelet/ssh package62//	pty, _, _ := sess.Pty()63//	environ := append(sess.Environ(), "TERM="+pty.Term)64//	p := tea.NewProgram(model, tea.WithEnvironment(environ))65func WithEnvironment(env []string) ProgramOption {66	return func(p *Program) {67		p.environ = env68	}69}7071// WithoutSignalHandler disables the signal handler that Bubble Tea sets up for72// Programs. This is useful if you want to handle signals yourself.73func WithoutSignalHandler() ProgramOption {74	return func(p *Program) {75		p.disableSignalHandler = true76	}77}7879// WithoutCatchPanics disables the panic catching that Bubble Tea does by80// default. If panic catching is disabled the terminal will be in a fairly81// unusable state after a panic because Bubble Tea will not perform its usual82// cleanup on exit.83func WithoutCatchPanics() ProgramOption {84	return func(p *Program) {85		p.disableCatchPanics = true86	}87}8889// WithoutSignals will ignore OS signals.90// This is mainly useful for testing.91func WithoutSignals() ProgramOption {92	return func(p *Program) {93		atomic.StoreUint32(&p.ignoreSignals, 1)94	}95}9697// WithoutRenderer disables the renderer. When this is set output and log98// statements will be plainly sent to stdout (or another output if one is set)99// without any rendering and redrawing logic. In other words, printing and100// logging will behave the same way it would in a non-TUI commandline tool.101// This can be useful if you want to use the Bubble Tea framework for a non-TUI102// application, or to provide an additional non-TUI mode to your Bubble Tea103// programs. For example, your program could behave like a daemon if output is104// not a TTY.105func WithoutRenderer() ProgramOption {106	return func(p *Program) {107		p.disableRenderer = true108	}109}110111// WithFilter supplies an event filter that will be invoked before Bubble Tea112// processes a tea.Msg. The event filter can return any tea.Msg which will then113// get handled by Bubble Tea instead of the original event. If the event filter114// returns nil, the event will be ignored and Bubble Tea will not process it.115//116// As an example, this could be used to prevent a program from shutting down if117// there are unsaved changes.118//119// Example:120//121//	func filter(m tea.Model, msg tea.Msg) tea.Msg {122//		if _, ok := msg.(tea.QuitMsg); !ok {123//			return msg124//		}125//126//		model := m.(myModel)127//		if model.hasChanges {128//			return nil129//		}130//131//		return msg132//	}133//134//	p := tea.NewProgram(Model{}, tea.WithFilter(filter));135//136//	if _,err := p.Run(); err != nil {137//		fmt.Println("Error running program:", err)138//		os.Exit(1)139//	}140func WithFilter(filter func(Model, Msg) Msg) ProgramOption {141	return func(p *Program) {142		p.filter = filter143	}144}145146// WithFPS sets a custom maximum FPS at which the renderer should run. If147// less than 1, the default value of 60 will be used. If over 120, the FPS148// will be capped at 120.149func WithFPS(fps int) ProgramOption {150	return func(p *Program) {151		p.fps = fps152	}153}154155// WithColorProfile sets the color profile that the program will use. This is156// useful when you want to force a specific color profile. By default, Bubble157// Tea will try to detect the terminal's color profile from environment158// variables and terminfo capabilities. Use [tea.WithEnvironment] to set custom159// environment variables.160func WithColorProfile(profile colorprofile.Profile) ProgramOption {161	return func(p *Program) {162		p.profile = &profile163	}164}165166// WithWindowSize sets the initial size of the terminal window. This is useful167// when you need to set the initial size of the terminal window, for example168// during testing or when you want to run your program in a non-interactive169// environment.170func WithWindowSize(width, height int) ProgramOption {171	return func(p *Program) {172		p.width = width173		p.height = height174	}175}