Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than c07fa72 point at it.

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 1–7why, one step back
1package main
23// A simple program that handled losing and acquiring focus.45import (6 "log"7

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 8why, one step back
8 tea "charm.land/bubbletea/v2"

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 9–14why, one step back
9)
1011func main() {12 p := tea.NewProgram(model{13 focused: true,14 reporting: true,

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 15why, one step back
15 })

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 16–35why, one step back
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 = false

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 36why, one step back
36 case tea.KeyPressMsg:

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 37–47why, one step back
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}47

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 48why, one step back
48func (m model) View() tea.View {

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

lines 49–63why, one step back
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 }

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 64–66why, one step back
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 v

chore(ci): sync golangci-lint configaymanbagabasbefore adoptionc07fa72

line 67why, one step back
67}