feat: add set-window-title command (#611)

Set the terminal window title using termenv.

Fixes: https://github.com/charmbracelet/bubbletea/issues/610
This commit is contained in:
Ayman Bagabas 2023-12-04 08:50:27 -08:00 committed by GitHub
parent bc1c475eb0
commit 2bcb0af2e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 1 deletions

View File

@ -170,3 +170,20 @@ func Sequentially(cmds ...Cmd) Cmd {
return nil
}
}
// setWindowTitleMsg is an internal message used to set the window title.
type setWindowTitleMsg string
// SetWindowTitle produces a command that sets the terminal title.
//
// For example:
//
// func (m model) Init() Cmd {
// // Set title.
// return tea.SetWindowTitle("My App")
// }
func SetWindowTitle(title string) Cmd {
return func() Msg {
return setWindowTitleMsg(title)
}
}

View File

@ -0,0 +1,35 @@
package main
// A simple example illustrating how to set a window title.
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
)
type model struct{}
func (m model) Init() tea.Cmd {
return tea.SetWindowTitle("Bubble Tea Example")
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tea.KeyMsg:
return m, tea.Quit
}
return m, nil
}
func (m model) View() string {
return "\nPress any key to quit."
}
func main() {
if _, err := tea.NewProgram(model{}).Run(); err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
}
}

View File

@ -167,3 +167,8 @@ func (p *Program) EnableMouseAllMotion() {
func (p *Program) DisableMouseAllMotion() {
p.renderer.disableMouseAllMotion()
}
// SetWindowTitle sets the terminal window title.
func (p *Program) SetWindowTitle(title string) {
p.output.SetWindowTitle(title)
}

3
tea.go
View File

@ -388,6 +388,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
p.Send(msg)
}
}()
case setWindowTitleMsg:
p.SetWindowTitle(string(msg))
}
// Process internal messages for the renderer.

View File

@ -25,7 +25,7 @@ func initialModel() model {
}
func (m model) Init() tea.Cmd {
return nil
return tea.SetWindowTitle("Grocery List")
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {