Skip to content

Commit

Permalink
feat: set the program environment variables
Browse files Browse the repository at this point in the history
This gives the option to provide a custom set of environment variables
to use in the Bubble Tea program. When running Bubble Tea in a remote
session such as SSH, we need to be able to pass the SSH session's
environment variables rather than the server ones.
  • Loading branch information
aymanbagabas committed Jul 18, 2024
1 parent 7c1bfc0 commit 135ccf5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func WithInputTTY() ProgramOption {
}
}

// WithEnvironment sets the environment variables that the program will use.
func WithEnvironment(env []string) ProgramOption {
return func(p *Program) {
p.environ = env
}
}

// WithoutSignalHandler disables the signal handler that Bubble Tea sets up for
// Programs. This is useful if you want to handle signals yourself.
func WithoutSignalHandler() ProgramOption {
Expand Down
8 changes: 8 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ type Program struct {
previousOutputState *term.State
renderer renderer

// the environment variables for the program, defaults to os.Environ().
environ []string

// where to read inputs from, this will usually be os.Stdin.
input io.Reader
// ttyInput is null if input is not a TTY.
Expand Down Expand Up @@ -222,6 +225,11 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
p.output = os.Stdout
}

// if no environment was set, set it to os.Environ()
if p.environ == nil {
p.environ = os.Environ()
}

return p
}

Expand Down

0 comments on commit 135ccf5

Please sign in to comment.