feat: support focus-blur events (#1122)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: support focus-blur events This backports @caarlos0 focus-blur changes from v2-exp to v1 https://github.com/charmbracelet/bubbletea/pull/1081 Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * Update key_sequences.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: go mod tidy ---------
authored by Ayman Bagabas · committed by GitHub · parent a5437ac · Browse files · 16 files·+265 added−65 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
- Co-authored-by: Christian Rocha <christian@rocha.is>
Diff stats
16 files+265 added−65 removed
- other:+265 added−65 removed
Provenance
- Commit
e58efab- Tree
60aebc2- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a5437ac
16 files changed+265 added−65 removed
added:examples/focus-blur/main.go+66 added
1+package main2+3+// A simple program that handled losing and acquiring focus.4+5+import (6+ "log"7+8+ tea "github.com/charmbracelet/bubbletea"9+)10+11+func main() {12+ p := tea.NewProgram(model{13+ // assume we start focused...14+ focused: true,15+ reporting: true,16+ }, tea.WithReportFocus())17+ if _, err := p.Run(); err != nil {18+ log.Fatal(err)19+ }20+}21+22+type model struct {23+ focused bool24+ reporting bool25+}26+27+func (m model) Init() tea.Cmd {28+ return nil29+}30+31+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {32+ switch msg := msg.(type) {33+ case tea.FocusMsg:34+ m.focused = true35+ case tea.BlurMsg:36+ m.focused = false37+ case tea.KeyMsg:38+ switch msg.String() {39+ case "t":40+ m.reporting = !m.reporting41+ case "ctrl+c", "q":42+ return m, tea.Quit43+ }44+ }45+46+ return m, nil47+}48+49+func (m model) View() string {50+ s := "Hi. Focus report is currently "51+ if m.reporting {52+ s += "enabled"53+ } else {54+ s += "disabled"55+ }56+ s += ".\n\n"57+58+ if m.reporting {59+ if m.focused {60+ s += "This program is currently focused!"61+ } else {62+ s += "This program is currently blurred!"63+ }64+ }65+ return s + "\n\nTo quit sooner press ctrl-c, or t to toggle focus reporting...\n"66+}modified:examples/go.mod+2 added-5 removed
20 unmodified lines21 github.com/atotto/clipboard v0.1.4 // indirect22 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect23 github.com/aymanbagabas/go-udiff v0.2.0 // indirect24 github.com/aymerick/douceur v0.2.0 // indirect25- github.com/charmbracelet/x/ansi v0.1.4 // indirect25+ github.com/charmbracelet/x/ansi v0.2.3 // indirect26 github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b // indirect27- github.com/charmbracelet/x/input v0.1.0 // indirect28- github.com/charmbracelet/x/term v0.1.1 // indirect29- github.com/charmbracelet/x/windows v0.1.0 // indirect27+ github.com/charmbracelet/x/term v0.2.0 // indirect28 github.com/dlclark/regexp2 v1.11.0 // indirect29 github.com/dustin/go-humanize v1.0.1 // indirect30 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect31 github.com/gorilla/css v1.0.1 // indirect5 unmodified lines37 github.com/muesli/reflow v0.3.0 // indirect38 github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect39 github.com/rivo/uniseg v0.4.7 // indirect40 github.com/sahilm/fuzzy v0.1.1 // indirect43- github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect41 github.com/yuin/goldmark v1.7.4 // indirect42 github.com/yuin/goldmark-emoji v1.0.3 // indirect43 golang.org/x/net v0.27.0 // indirect44 golang.org/x/sync v0.8.0 // indirect6 unmodified linesmodified:examples/go.sum+4 added-12 removed
20 unmodified lines21 github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=22 github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=23 github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=24 github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=25-github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=26-github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=25+github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=26+github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=27 github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b h1:MnAMdlwSltxJyULnrYbkZpp4k58Co7Tah3ciKhSNo0Q=28 github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=29 github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28 h1:sOWKNRjt8uOEVgPiJVIJCse1+mUDM2F/vYY6W0Go640=30 github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28/go.mod h1:l1w+LTJZCCozeGzMEWGxRw6Mo2DfcZUvupz8HGubdes=31-github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=32-github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=33-github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=34-github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=35-github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=36-github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=31+github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=32+github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=33 github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=34 github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=35 github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=36 github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=31 unmodified lines68 github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=69 github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=70 github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=71 github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=76-github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=77-github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=72 github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=73 github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=74 github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=75 github.com/yuin/goldmark-emoji v1.0.3 h1:aLRkLHOuBR2czCY4R8olwMjID+tENfhyFDMCRhbIQY4=76 github.com/yuin/goldmark-emoji v1.0.3/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=83-golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=84-golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=77 golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=78 golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=79 golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=80 golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=8 unmodified linesadded:focus.go+9 added
1+package tea2+3+// FocusMsg represents a terminal focus message.4+// This occurs when the terminal gains focus.5+type FocusMsg struct{}6+7+// BlurMsg represents a terminal blur message.8+// This occurs when the terminal loses focus.9+type BlurMsg struct{}modified:go.mod+2 added-5 removed
2 unmodified lines3 go 1.184 5 require (6 github.com/charmbracelet/lipgloss v0.13.07- github.com/charmbracelet/x/ansi v0.1.48- github.com/charmbracelet/x/term v0.1.17+ github.com/charmbracelet/x/ansi v0.2.38+ github.com/charmbracelet/x/term v0.2.09 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f10 github.com/mattn/go-localereader v0.0.111 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f612 github.com/muesli/cancelreader v0.2.22 unmodified lines15 )16 17 require (18 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect19- github.com/charmbracelet/x/input v0.1.0 // indirect20- github.com/charmbracelet/x/windows v0.1.0 // indirect19 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect20 github.com/mattn/go-isatty v0.0.20 // indirect21 github.com/mattn/go-runewidth v0.0.15 // indirect22 github.com/muesli/termenv v0.15.2 // indirect23 github.com/rivo/uniseg v0.4.7 // indirect26- github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect24 golang.org/x/text v0.3.8 // indirect25 )modified:go.sum+4 added-11 removed
1 github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2 github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3 github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=4 github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=5-github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=6-github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=7-github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=8-github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=9-github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=10-github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=11-github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=12-github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=5+github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=6+github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=7+github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=8+github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=9 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=11 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=12 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=11 unmodified lines24 github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=25 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26 github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=27 github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=32-github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=33-github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=34-golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=28 golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=29 golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=30 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=4 unmodified linesmodified:key_sequences.go+12 added
115 unmodified lines116 }117 118 return true, inputLen, KeyMsg(k)119 }120+121+// detectReportFocus detects a focus report sequence.122+// nolint: gomnd123+func detectReportFocus(input []byte) (hasRF bool, width int, msg Msg) {124+ switch {125+ case bytes.Equal(input, []byte("\x1b[I")):126+ return true, 3, FocusMsg{}127+ case bytes.Equal(input, []byte("\x1b[O")):128+ return true, 3, BlurMsg{}129+ }130+ return false, 0, nil131+}modified:key_test.go+73 added-32 removed
132 unmodified lines133 td := buildBaseSeqTests()134 // Add tests for the inputs that detectOneMsg() can parse, but135 // detectSequence() cannot.136 td = append(td,137+ // focus/blur138+ seqTest{139+ []byte{'\x1b', '[', 'I'},140+ FocusMsg{},141+ },142+ seqTest{143+ []byte{'\x1b', '[', 'O'},144+ BlurMsg{},145+ },146 // Mouse event.147 seqTest{148 []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},149 MouseMsg{X: 32, Y: 16, Type: MouseWheelUp, Button: MouseButtonWheelUp, Action: MouseActionPress},100 unmodified lines250 in []byte251 out []Msg252 }253 testData := []test{254+ {245- {"a",255+ "a",256 []byte{'a'},257 []Msg{258 KeyMsg{259 Type: KeyRunes,260 Runes: []rune{'a'},261 },262 },263 },254- {" ",264+ {265+ " ",266 []byte{' '},267 []Msg{268 KeyMsg{269 Type: KeySpace,270 Runes: []rune{' '},271 },272 },273 },274+ {263- {"a alt+a",275+ "a alt+a",276 []byte{'a', '\x1b', 'a'},277 []Msg{278 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},279 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}, Alt: true},280 },281 },282+ {270- {"a alt+a a",283+ "a alt+a a",284 []byte{'a', '\x1b', 'a', 'a'},285 []Msg{286 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},287 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}, Alt: true},288 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},289 },290 },291+ {278- {"ctrl+a",292+ "ctrl+a",293 []byte{byte(keySOH)},294 []Msg{295 KeyMsg{296 Type: KeyCtrlA,297 },298 },299 },300+ {286- {"ctrl+a ctrl+b",301+ "ctrl+a ctrl+b",302 []byte{byte(keySOH), byte(keySTX)},303 []Msg{304 KeyMsg{Type: KeyCtrlA},305 KeyMsg{Type: KeyCtrlB},306 },307 },308+ {293- {"alt+a",309+ "alt+a",310 []byte{byte(0x1b), 'a'},311 []Msg{312 KeyMsg{313 Type: KeyRunes,1 unmodified line315 Runes: []rune{'a'},316 },317 },318 },319+ {303- {"abcd",320+ "abcd",321 []byte{'a', 'b', 'c', 'd'},322 []Msg{323 KeyMsg{324 Type: KeyRunes,325 Runes: []rune{'a', 'b', 'c', 'd'},326 },327 },328 },329+ {312- {"up",330+ "up",331 []byte("\x1b[A"),332 []Msg{333 KeyMsg{334 Type: KeyUp,335 },336 },337 },338+ {320- {"wheel up",339+ "wheel up",340 []byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},341 []Msg{342 MouseMsg{343 X: 32,3 unmodified lines347 Action: MouseActionPress,348 },349 },350 },351+ {332- {"left motion release",352+ "left motion release",353 []byte{354 '\x1b', '[', 'M', byte(32) + 0b0010_0000, byte(32 + 33), byte(16 + 33),355 '\x1b', '[', 'M', byte(32) + 0b0000_0011, byte(64 + 33), byte(32 + 33),356 },13 unmodified lines370 Action: MouseActionRelease,371 }),372 },373 },374+ {354- {"shift+tab",375+ "shift+tab",376 []byte{'\x1b', '[', 'Z'},377 []Msg{378 KeyMsg{379 Type: KeyShiftTab,380 },381 },382 },383+ {362- {"enter",384+ "enter",385 []byte{'\r'},386 []Msg{KeyMsg{Type: KeyEnter}},387 },388+ {366- {"alt+enter",389+ "alt+enter",390 []byte{'\x1b', '\r'},391 []Msg{392 KeyMsg{393 Type: KeyEnter,394 Alt: true,395 },396 },397 },398+ {375- {"insert",399+ "insert",400 []byte{'\x1b', '[', '2', '~'},401 []Msg{402 KeyMsg{403 Type: KeyInsert,404 },405 },406 },407+ {383- {"alt+ctrl+a",408+ "alt+ctrl+a",409 []byte{'\x1b', byte(keySOH)},410 []Msg{411 KeyMsg{412 Type: KeyCtrlA,413 Alt: true,414 },415 },416 },417+ {392- {"?CSI[45 45 45 45 88]?",418+ "?CSI[45 45 45 45 88]?",419 []byte{'\x1b', '[', '-', '-', '-', '-', 'X'},420 []Msg{unknownCSISequenceMsg([]byte{'\x1b', '[', '-', '-', '-', '-', 'X'})},421 },422 // Powershell sequences.423+ {397- {"up",424+ "up",425 []byte{'\x1b', 'O', 'A'},426 []Msg{KeyMsg{Type: KeyUp}},427 },428+ {401- {"down",429+ "down",430 []byte{'\x1b', 'O', 'B'},431 []Msg{KeyMsg{Type: KeyDown}},432 },433+ {405- {"right",434+ "right",435 []byte{'\x1b', 'O', 'C'},436 []Msg{KeyMsg{Type: KeyRight}},437 },438+ {409- {"left",439+ "left",440 []byte{'\x1b', 'O', 'D'},441 []Msg{KeyMsg{Type: KeyLeft}},442 },443+ {413- {"alt+enter",444+ "alt+enter",445 []byte{'\x1b', '\x0d'},446 []Msg{KeyMsg{Type: KeyEnter, Alt: true}},447 },448+ {417- {"alt+backspace",449+ "alt+backspace",450 []byte{'\x1b', '\x7f'},451 []Msg{KeyMsg{Type: KeyBackspace, Alt: true}},452 },453+ {421- {"ctrl+@",454+ "ctrl+@",455 []byte{'\x00'},456 []Msg{KeyMsg{Type: KeyCtrlAt}},457 },458+ {425- {"alt+ctrl+@",459+ "alt+ctrl+@",460 []byte{'\x1b', '\x00'},461 []Msg{KeyMsg{Type: KeyCtrlAt, Alt: true}},462 },463+ {429- {"esc",464+ "esc",465 []byte{'\x1b'},466 []Msg{KeyMsg{Type: KeyEsc}},467 },468+ {433- {"alt+esc",469+ "alt+esc",470 []byte{'\x1b', '\x1b'},471 []Msg{KeyMsg{Type: KeyEsc, Alt: true}},472 },473+ {437- {"[a b] o",474+ "[a b] o",475 []byte{476 '\x1b', '[', '2', '0', '0', '~',477 'a', ' ', 'b',478 '\x1b', '[', '2', '0', '1', '~',3 unmodified lines482 KeyMsg{Type: KeyRunes, Runes: []rune("a b"), Paste: true},483 KeyMsg{Type: KeyRunes, Runes: []rune("o")},484 },485 },486+ {449- {"[a\x03\nb]",487+ "[a\x03\nb]",488 []byte{489 '\x1b', '[', '2', '0', '0', '~',490 'a', '\x03', '\n', 'b',453- '\x1b', '[', '2', '0', '1', '~'},491+ '\x1b', '[', '2', '0', '1', '~',492+ },493 []Msg{494 KeyMsg{Type: KeyRunes, Runes: []rune("a\x03\nb"), Paste: true},495 },496 },1 unmodified line498 if runtime.GOOS != "windows" {499 // Sadly, utf8.DecodeRune([]byte(0xfe)) returns a valid rune on windows.500 // This is incorrect, but it makes our test fail if we try it out.501 testData = append(testData,463- test{"?0xfe?",502+ test{503+ "?0xfe?",504 []byte{'\xfe'},505 []Msg{unknownInputByteMsg(0xfe)},506 },467- test{"a ?0xfe? b",507+ test{508+ "a ?0xfe? b",509 []byte{'a', '\xfe', ' ', 'b'},510 []Msg{511 KeyMsg{Type: KeyRunes, Runes: []rune{'a'}},512 unknownInputByteMsg(0xfe),223 unmodified linesmodified:key.go+7 added
626 unmodified lines627 }628 }629 }630 631+ // Detect focus events.632+ var foundRF bool633+ foundRF, w, msg = detectReportFocus(b)634+ if foundRF {635+ return w, msg636+ }637+638 // Detect bracketed paste.639 var foundbp bool640 foundbp, w, msg = detectBracketedPaste(b)641 if foundbp {74 unmodified linesmodified:nil_renderer.go+3 added
21 unmodified lines22 func (n nilRenderer) enableMouseSGRMode() {}23 func (n nilRenderer) disableMouseSGRMode() {}24 func (n nilRenderer) bracketedPasteActive() bool { return false }25 func (n nilRenderer) setWindowTitle(_ string) {}26+func (n nilRenderer) reportFocus() bool { return false }27+func (n nilRenderer) enableReportFocus() {}28+func (n nilRenderer) disableReportFocus() {}modified:options.go+9 added
232 unmodified lines233 return func(p *Program) {234 p.fps = fps235 }236 }237+238+// WithReportFocus enables reporting when the terminal gains and lost focus.239+//240+// You can then check for FocusMsg and BlurMsg in your model's Update method.241+func WithReportFocus() ProgramOption {242+ return func(p *Program) {243+ p.startupOptions |= withReportFocus244+ }245+}modified:renderer.go+9 added
68 unmodified lines69 bracketedPasteActive() bool70 71 // setWindowTitle sets the terminal window title.72 setWindowTitle(string)73+74+ // reportFocus returns whether reporting focus events is enabled.75+ reportFocus() bool76+77+ // enableReportFocus reports focus events to the program.78+ enableReportFocus()79+80+ // disableReportFocus stops reporting focus events to the program.81+ disableReportFocus()82 }83 84 // repaintMsg forces a full repaint.85 type repaintMsg struct{}modified:screen.go+20 added
142 unmodified lines143 // bracketed paste should be disabled. You can send an144 // disableBracketedPasteMsg with DisableBracketedPaste.145 type disableBracketedPasteMsg struct{}146 147+// enableReportFocusMsg is an internal message that signals to enable focus148+// reporting. You can send an enableReportFocusMsg with EnableReportFocus.149+type enableReportFocusMsg struct{}150+151+// EnableReportFocus is a special command that tells the Bubble Tea program to152+// report focus events to the program.153+func EnableReportFocus() Msg {154+ return enableReportFocusMsg{}155+}156+157+// disableReportFocusMsg is an internal message that signals to disable focus158+// reporting. You can send an disableReportFocusMsg with DisableReportFocus.159+type disableReportFocusMsg struct{}160+161+// DisableReportFocus is a special command that tells the Bubble Tea program to162+// stop reporting focus events to the program.163+func DisableReportFocus() Msg {164+ return disableReportFocusMsg{}165+}166+167 // EnterAltScreen enters the alternate screen buffer, which consumes the entire168 // terminal window. ExitAltScreen will return the terminal to its former state.169 //170 // Deprecated: Use the WithAltScreen ProgramOption instead.78 unmodified linesmodified:standard_renderer.go+26 added
45 unmodified lines46 47 // whether or not we're currently using bracketed paste48 bpActive bool49 50+ // reportingFocus whether reporting focus events is enabled51+ reportingFocus bool52+53 // renderer dimensions; usually the size of the window54 width int55 height int56 403 unmodified lines460 461 return r.bpActive462 }463 464+func (r *standardRenderer) enableReportFocus() {465+ r.mtx.Lock()466+ defer r.mtx.Unlock()467+468+ r.execute(ansi.EnableReportFocus)469+ r.reportingFocus = true470+}471+472+func (r *standardRenderer) disableReportFocus() {473+ r.mtx.Lock()474+ defer r.mtx.Unlock()475+476+ r.execute(ansi.DisableReportFocus)477+ r.reportingFocus = false478+}479+480+func (r *standardRenderer) reportFocus() bool {481+ r.mtx.Lock()482+ defer r.mtx.Unlock()483+484+ return r.reportingFocus485+}486+487 // setWindowTitle sets the terminal window title.488 func (r *standardRenderer) setWindowTitle(title string) {489 r.execute(ansi.SetWindowTitle(title))490 }265 unmodified linesmodified:tea.go+15 added
95 unmodified lines96 // recover from panics, print the stack trace, and disable raw mode. This97 // feature is on by default.98 withoutCatchPanics99 withoutBracketedPaste100+ withReportFocus101 )102 103 // channelHandlers manages the series of channels returned by various processes.104 // It allows us to wait for those processes to terminate before exiting the62 unmodified lines167 altScreenWasActive bool168 ignoreSignals uint32169 170 bpWasActive bool // was the bracketed paste mode active before releasing the terminal?171+ reportFocus bool // was focus reporting active before releasing the terminal?172 173 filter func(Model, Msg) Msg174 175 // fps is the frames per second we should set on the renderer, if215 unmodified lines391 392 case disableBracketedPasteMsg:393 p.renderer.disableBracketedPaste()394 395+ case enableReportFocusMsg:396+ p.renderer.enableReportFocus()397+398+ case disableReportFocusMsg:399+ p.renderer.disableReportFocus()400+401 case execMsg:402 // NB: this blocks.403 p.exec(msg.cmd, msg.fn)404 144 unmodified lines549 } else if p.startupOptions&withMouseAllMotion != 0 {550 p.renderer.enableMouseAllMotion()551 p.renderer.enableMouseSGRMode()552 }553+ if p.startupOptions&withReportFocus != 0 {554+ p.renderer.enableReportFocus()555+ }556 557 // Start the renderer.558 p.renderer.start()559 144 unmodified lines704 if p.renderer != nil {705 p.renderer.stop()706 p.altScreenWasActive = p.renderer.altScreen()707 p.bpWasActive = p.renderer.bracketedPasteActive()708+ p.reportFocus = p.renderer.reportFocus()709 }710 711 return p.restoreTerminalState()712 }21 unmodified lines734 }735 if p.bpWasActive {736 p.renderer.enableBracketedPaste()737 }738+ if p.reportFocus {739+ p.renderer.enableReportFocus()740+ }741 742 // If the output is a terminal, it may have been resized while another743 // process was at the foreground, in which case we may not have received744 // SIGWINCH. Detect any size change now and propagate the new size as29 unmodified linesmodified:tty.go+4 added
42 unmodified lines43 p.renderer.disableBracketedPaste()44 p.renderer.showCursor()45 p.disableMouse()46 47+ if p.renderer.reportFocus() {48+ p.renderer.disableReportFocus()49+ }50+51 if p.renderer.altScreen() {52 p.renderer.exitAltScreen()53 54 // give the terminal a moment to catch up82 unmodified lines