Skip to content

Commit

Permalink
feat: add set-window-title command
Browse files Browse the repository at this point in the history
Set the terminal window title using termenv.

Fixes: #610
  • Loading branch information
aymanbagabas committed Nov 21, 2022
1 parent 79c76c6 commit 150fb32
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
35 changes: 35 additions & 0 deletions examples/set-window-title/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
p.Send(cmd())
}
}()

case setWindowTitleMsg:
p.SetWindowTitle(string(msg))
}

// Process internal messages for the renderer.
Expand Down

0 comments on commit 150fb32

Please sign in to comment.