Skip to content

Commit

Permalink
feat: set the program environment variables (#1063)
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 Aug 12, 2024
1 parent 7ddeec9 commit cae9acd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ func WithInputTTY() ProgramOption {
}
}

// WithEnvironment sets the environment variables that the program will use.
// This 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.
//
// Example:
//
// var sess ssh.Session // ssh.Session is a type from the github.com/charmbracelet/ssh package
// pty, _, _ := sess.Pty()
// environ := append(sess.Environ(), "TERM="+pty.Term)
// p := tea.NewProgram(model, tea.WithEnvironment(environ)
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 cae9acd

Please sign in to comment.