Skip to content
Myr

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

16 files+265 added−65 removed

added:examples/focus-blur/main.go+66 added
1package main23// A simple program that handled losing and acquiring focus.45import (6 "log"78 tea "github.com/charmbracelet/bubbletea"9)1011func 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}2122type model struct {23 focused bool24 reporting bool25}2627func (m model) Init() tea.Cmd {28 return nil29}3031func (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 }4546 return m, nil47}4849func (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"5758 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 lines
21 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 // indirect
5 unmodified lines
37 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 // indirect
6 unmodified lines
modified:examples/go.sum+4 added-12 removed
20 unmodified lines
21github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=22github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=23github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=24github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=25github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=26github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=25github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=26github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=27github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b h1:MnAMdlwSltxJyULnrYbkZpp4k58Co7Tah3ciKhSNo0Q=28github.com/charmbracelet/x/exp/golden v0.0.0-20240815200342-61de596daa2b/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=29github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28 h1:sOWKNRjt8uOEVgPiJVIJCse1+mUDM2F/vYY6W0Go640=30github.com/charmbracelet/x/exp/teatest v0.0.0-20240521184646-23081fb03b28/go.mod h1:l1w+LTJZCCozeGzMEWGxRw6Mo2DfcZUvupz8HGubdes=31github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=32github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=33github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=34github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=35github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=36github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=31github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=32github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=33github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=34github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=35github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=36github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
31 unmodified lines
68github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=69github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=70github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=71github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=76github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=77github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=72github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=73github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=74github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=75github.com/yuin/goldmark-emoji v1.0.3 h1:aLRkLHOuBR2czCY4R8olwMjID+tENfhyFDMCRhbIQY4=76github.com/yuin/goldmark-emoji v1.0.3/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=83golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=84golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=77golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=78golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=79golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=80golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
8 unmodified lines
added:focus.go+9 added
1package tea23// FocusMsg represents a terminal focus message.4// This occurs when the terminal gains focus.5type FocusMsg struct{}67// BlurMsg represents a terminal blur message.8// This occurs when the terminal loses focus.9type BlurMsg struct{}
modified:go.mod+2 added-5 removed
2 unmodified lines
3go 1.1845require (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.2
2 unmodified lines
15)1617require (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
1github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=4github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=5github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=6github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=7github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=8github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=9github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=10github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=11github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=12github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=5github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY=6github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=7github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=8github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=9github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=11github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=12github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
11 unmodified lines
24github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=25github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=27github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=32github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=33github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=34golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=28golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=29golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=30golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4 unmodified lines
modified:key_sequences.go+12 added
115 unmodified lines
116 }117118 return true, inputLen, KeyMsg(k)119}120121// detectReportFocus detects a focus report sequence.122// nolint: gomnd123func 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 lines
133 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 lines
250 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 line
315 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 lines
347 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 lines
370 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 lines
482 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 line
498 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 lines
modified:key.go+7 added
626 unmodified lines
627 }628 }629 }630631 // Detect focus events.632 var foundRF bool633 foundRF, w, msg = detectReportFocus(b)634 if foundRF {635 return w, msg636 }637638 // Detect bracketed paste.639 var foundbp bool640 foundbp, w, msg = detectBracketedPaste(b)641 if foundbp {
74 unmodified lines
modified:nil_renderer.go+3 added
21 unmodified lines
22func (n nilRenderer) enableMouseSGRMode() {}23func (n nilRenderer) disableMouseSGRMode() {}24func (n nilRenderer) bracketedPasteActive() bool { return false }25func (n nilRenderer) setWindowTitle(_ string) {}26func (n nilRenderer) reportFocus() bool { return false }27func (n nilRenderer) enableReportFocus() {}28func (n nilRenderer) disableReportFocus() {}
modified:options.go+9 added
232 unmodified lines
233 return func(p *Program) {234 p.fps = fps235 }236}237238// 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.241func WithReportFocus() ProgramOption {242 return func(p *Program) {243 p.startupOptions |= withReportFocus244 }245}
modified:renderer.go+9 added
68 unmodified lines
69 bracketedPasteActive() bool7071 // setWindowTitle sets the terminal window title.72 setWindowTitle(string)7374 // reportFocus returns whether reporting focus events is enabled.75 reportFocus() bool7677 // enableReportFocus reports focus events to the program.78 enableReportFocus()7980 // disableReportFocus stops reporting focus events to the program.81 disableReportFocus()82}8384// repaintMsg forces a full repaint.85type repaintMsg struct{}
modified:screen.go+20 added
142 unmodified lines
143// bracketed paste should be disabled. You can send an144// disableBracketedPasteMsg with DisableBracketedPaste.145type disableBracketedPasteMsg struct{}146147// enableReportFocusMsg is an internal message that signals to enable focus148// reporting. You can send an enableReportFocusMsg with EnableReportFocus.149type enableReportFocusMsg struct{}150151// EnableReportFocus is a special command that tells the Bubble Tea program to152// report focus events to the program.153func EnableReportFocus() Msg {154 return enableReportFocusMsg{}155}156157// disableReportFocusMsg is an internal message that signals to disable focus158// reporting. You can send an disableReportFocusMsg with DisableReportFocus.159type disableReportFocusMsg struct{}160161// DisableReportFocus is a special command that tells the Bubble Tea program to162// stop reporting focus events to the program.163func DisableReportFocus() Msg {164 return disableReportFocusMsg{}165}166167// 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 lines
modified:standard_renderer.go+26 added
45 unmodified lines
4647 // whether or not we're currently using bracketed paste48 bpActive bool4950 // reportingFocus whether reporting focus events is enabled51 reportingFocus bool5253 // renderer dimensions; usually the size of the window54 width int55 height int56
403 unmodified lines
460461 return r.bpActive462}463464func (r *standardRenderer) enableReportFocus() {465 r.mtx.Lock()466 defer r.mtx.Unlock()467468 r.execute(ansi.EnableReportFocus)469 r.reportingFocus = true470}471472func (r *standardRenderer) disableReportFocus() {473 r.mtx.Lock()474 defer r.mtx.Unlock()475476 r.execute(ansi.DisableReportFocus)477 r.reportingFocus = false478}479480func (r *standardRenderer) reportFocus() bool {481 r.mtx.Lock()482 defer r.mtx.Unlock()483484 return r.reportingFocus485}486487// setWindowTitle sets the terminal window title.488func (r *standardRenderer) setWindowTitle(title string) {489 r.execute(ansi.SetWindowTitle(title))490}
265 unmodified lines
modified:tea.go+15 added
95 unmodified lines
96 // recover from panics, print the stack trace, and disable raw mode. This97 // feature is on by default.98 withoutCatchPanics99 withoutBracketedPaste100 withReportFocus101)102103// channelHandlers manages the series of channels returned by various processes.104// It allows us to wait for those processes to terminate before exiting the
62 unmodified lines
167 altScreenWasActive bool168 ignoreSignals uint32169170 bpWasActive bool // was the bracketed paste mode active before releasing the terminal?171 reportFocus bool // was focus reporting active before releasing the terminal?172173 filter func(Model, Msg) Msg174175 // fps is the frames per second we should set on the renderer, if
215 unmodified lines
391392 case disableBracketedPasteMsg:393 p.renderer.disableBracketedPaste()394395 case enableReportFocusMsg:396 p.renderer.enableReportFocus()397398 case disableReportFocusMsg:399 p.renderer.disableReportFocus()400401 case execMsg:402 // NB: this blocks.403 p.exec(msg.cmd, msg.fn)404
144 unmodified lines
549 } 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 }556557 // Start the renderer.558 p.renderer.start()559
144 unmodified lines
704 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 }710711 return p.restoreTerminalState()712}
21 unmodified lines
734 }735 if p.bpWasActive {736 p.renderer.enableBracketedPaste()737 }738 if p.reportFocus {739 p.renderer.enableReportFocus()740 }741742 // 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 as
29 unmodified lines
modified:tty.go+4 added
42 unmodified lines
43 p.renderer.disableBracketedPaste()44 p.renderer.showCursor()45 p.disableMouse()4647 if p.renderer.reportFocus() {48 p.renderer.disableReportFocus()49 }5051 if p.renderer.altScreen() {52 p.renderer.exitAltScreen()5354 // give the terminal a moment to catch up
82 unmodified lines