Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support FocusIn/FocusOut #152

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ansi/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ const (
RequestMouseAllMotion = "\x1b[?1003$p"
)

// Report Focus is a mode that makes the terminal report focus-in and focus-out events.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-FocusIn_FocusOut
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
const (
EnableReportFocus = "\x1b[?1004h"
DisableReportFocus = "\x1b[?1004l"
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
RequestReportFocus = "\x1b[?1004$p"
)

// SGR Mouse Extension is a mode that determines whether the mouse reports events
// formatted with SGR parameters.
//
Expand Down
9 changes: 9 additions & 0 deletions exp/term/input/focus.go
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package input

type (
FocusEvent struct{}
BlurEvent struct{}
)

func (FocusEvent) String() string { return "focus" }
func (BlurEvent) String() string { return "blur" }
25 changes: 25 additions & 0 deletions exp/term/input/focus_test.go
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package input

import (
"testing"
)

func TestFocus(t *testing.T) {
_, e := ParseSequence([]byte("\x1b[I"))
switch e.(type) {
case FocusEvent:
// ok
default:
t.Error("invalid sequence")
}
}

func TestBlur(t *testing.T) {
_, e := ParseSequence([]byte("\x1b[O"))
switch e.(type) {
case BlurEvent:
// ok
default:
t.Error("invalid sequence")
}
}
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
Expand Down
13 changes: 13 additions & 0 deletions input/focus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package input

// FocusEvent represents a focus event.
type FocusEvent struct{}

// String implements fmt.Stringer.
func (FocusEvent) String() string { return "focus" }

// BlurEvent represents a blur event.
type BlurEvent struct{}

// String implements fmt.Stringer.
func (BlurEvent) String() string { return "blur" }
25 changes: 25 additions & 0 deletions input/focus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package input

import (
"testing"
)

func TestFocus(t *testing.T) {
_, e := ParseSequence([]byte("\x1b[I"))
switch e.(type) {
case FocusEvent:
// ok
default:
t.Error("invalid sequence")
}
}

func TestBlur(t *testing.T) {
_, e := ParseSequence([]byte("\x1b[O"))
switch e.(type) {
case BlurEvent:
// ok
default:
t.Error("invalid sequence")
}
}
13 changes: 13 additions & 0 deletions input/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ func buildBaseSeqTests() []seqTest {
func TestParseSequence(t *testing.T) {
td := buildBaseSeqTests()
td = append(td,
// focus/blur
seqTest{
[]byte{'\x1b', '[', 'I'},
[]Event{
FocusEvent{},
},
},
seqTest{
[]byte{'\x1b', '[', 'O'},
[]Event{
BlurEvent{},
},
},
// Mouse event.
seqTest{
[]byte{'\x1b', '[', 'M', byte(32) + 0b0100_0000, byte(65), byte(49)},
Expand Down
4 changes: 4 additions & 0 deletions input/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ func parseCsi(b []byte) (int, Event) {
}

switch cmd := csi.Command(); cmd {
case 'I':
return i, FocusEvent{}
case 'O':
return i, BlurEvent{}
case 'R':
// Cursor position report OR modified F3
if paramsLen == 0 {
Expand Down
Loading