main.go
This file
Path
examples/focus-blur/main.go
67 lines · 1.1 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
1package main23// A simple program that handled losing and acquiring focus.45import (6 "log"78 tea "charm.land/bubbletea/v2"9)1011func main() {12 p := tea.NewProgram(model{13 focused: true,14 reporting: true,15 })16 if _, err := p.Run(); err != nil {17 log.Fatal(err)18 }19}2021type model struct {22 focused bool23 reporting bool24}2526func (m model) Init() tea.Cmd {27 return nil28}2930func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {31 switch msg := msg.(type) {32 case tea.FocusMsg:33 m.focused = true34 case tea.BlurMsg:35 m.focused = false36 case tea.KeyPressMsg:37 switch msg.String() {38 case "t":39 m.reporting = !m.reporting40 case "ctrl+c", "q":41 return m, tea.Quit42 }43 }4445 return m, nil46}4748func (m model) View() tea.View {49 s := "Hi. Focus report is currently "50 if m.reporting {51 s += "enabled"52 } else {53 s += "disabled"54 }55 s += ".\n\n"5657 if m.reporting {58 if m.focused {59 s += "This program is currently focused!"60 } else {61 s += "This program is currently blurred!"62 }63 }64 v := tea.NewView(s + "\n\nTo quit sooner press ctrl-c, or t to toggle focus reporting...\n")65 v.ReportFocus = m.reporting66 return v67}