forked from Mirrors/bubbletea
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:
parent
bc1c475eb0
commit
2bcb0af2e2
17
commands.go
17
commands.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
3
tea.go
|
@ -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.
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue