What I do
- •Implement logging to file (standard TUI debugging).
- •Setup
teatestfor automated TUI testing. - •Handle terminal recovery logic.
Best Practices
- •Log to File: Since
fmt.Printlnbreaks the TUI, usetea.LogToFile("debug.log", "prefix")for development. - •Testing with Teatest: Use
github.com/charmbracelet/bubbletea/teatestto simulate keypresses and assert the final view string. - •Panic Recovery: Use
tea.WithAltScreen()and ensure errors are returned throughp.Run()rather than callingos.Exitinside the model.
Example: File Logging
go
if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
log.Fatal(err)
}
defer f.Close()
}