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

(v2) Export different input mode commands and messages #1119

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 7 additions & 2 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
KeyIsoLevel3Shift
KeyIsoLevel5Shift

// Special names in C0

Check failure on line 178 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

KeyBackspace = rune(ansi.DEL)
KeyTab = rune(ansi.HT)
Expand All @@ -184,7 +184,7 @@
KeyEscape = rune(ansi.ESC)
KeyEsc = KeyEscape

// Special names in G0

Check failure on line 187 in key.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

KeySpace = rune(ansi.SP)
)
Expand Down Expand Up @@ -354,10 +354,15 @@
code = k.BaseCode
}

if code == ' ' {
switch code {
case KeySpace:
// Space is the only invisible printable character.
sb.WriteString("space")
} else {
case KeyExtended:
// Write the actual text of the key when the key contains multiple
// runes.
sb.WriteString(k.Text)
default:
sb.WriteRune(code)
}
}
Expand Down
28 changes: 20 additions & 8 deletions kitty.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// enhancement protocol flags.
type setKittyKeyboardFlagsMsg int

// enableKittyKeyboard is a command to enable Kitty keyboard progressive
// EnableKittyKeyboard is a command to enable Kitty keyboard progressive
// enhancements.
//
// The flags parameter is a bitmask of the following
Expand All @@ -23,30 +23,42 @@
// 16: Report associated text
//
// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/ for more information.
func enableKittyKeyboard(flags int) Cmd { //nolint:unused
func EnableKittyKeyboard(flags int) Cmd { //nolint:unused
return func() Msg {
return setKittyKeyboardFlagsMsg(flags)
}
}

// disableKittyKeyboard is a command to disable Kitty keyboard progressive
// DisableKittyKeyboard is a command to disable Kitty keyboard progressive
// enhancements.
func disableKittyKeyboard() Msg { //nolint:unused
func DisableKittyKeyboard() Msg { //nolint:unused
return setKittyKeyboardFlagsMsg(0)
}

// kittyKeyboardMsg is a message that queries the current Kitty keyboard
// progressive enhancement flags.
type kittyKeyboardMsg struct{}

// kittyKeyboard is a command that queries the current Kitty keyboard
// KittyKeyboard is a command that queries the current Kitty keyboard
// progressive enhancement flags from the terminal.
func kittyKeyboard() Msg { //nolint:unused
func KittyKeyboard() Msg { //nolint:unused
return kittyKeyboardMsg{}
}

// _KittyKeyboardMsg represents Kitty keyboard progressive enhancement flags message.
type _KittyKeyboardMsg int
// KittyKeyboardMsg is a bitmask message representing Kitty keyboard
// progressive enhancement flags.
//
// The bitmaps represents the following:
//
// 0: Disable all features
// 1: Disambiguate escape codes
// 2: Report event types
// 4: Report alternate keys
// 8: Report all keys as escape codes
// 16: Report associated text
//
// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/ for more information.
type KittyKeyboardMsg int

// Kitty Clipboard Control Sequences
var kittyKeyMap = map[int]rune{
Expand Down Expand Up @@ -241,7 +253,7 @@

// alternate key reporting
switch len(params) {
case 3:

Check failure on line 256 in kitty.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 3, in <case> detected (gomnd)
// shifted key + base key
if b := rune(params[2]); unicode.IsPrint(b) {
// XXX: When alternate key reporting is enabled, the protocol
Expand Down
22 changes: 12 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,18 @@ func WithReportFocus() ProgramOption {
// keyboard sequences. This might also enable reporting of release key events
// depending on the terminal emulator supporting it.
//
// This is a syntactic sugar for WithKittyKeyboard(3) and WithXtermModifyOtherKeys(1).
// This is a syntactic sugar for WithKittyKeyboard(7) and WithXtermModifyOtherKeys(1).
func WithEnhancedKeyboard() ProgramOption {
return func(p *Program) {
_WithKittyKeyboard(ansi.KittyDisambiguateEscapeCodes |
ansi.KittyReportEventTypes)(p)
_WithModifyOtherKeys(1)(p)
WithKittyKeyboard(ansi.KittyDisambiguateEscapeCodes |
ansi.KittyReportEventTypes |
ansi.KittyReportAlternateKeys,
)(p)
WithModifyOtherKeys(1)(p)
}
}

// _WithKittyKeyboard enables support for the Kitty keyboard protocol. This
// WithKittyKeyboard enables support for the Kitty keyboard protocol. This
// protocol enables more key combinations and events than the traditional
// ambiguous terminal keyboard sequences.
//
Expand All @@ -288,14 +290,14 @@ func WithEnhancedKeyboard() ProgramOption {
// 16: Report associated text
//
// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/ for more information.
func _WithKittyKeyboard(flags int) ProgramOption {
func WithKittyKeyboard(flags int) ProgramOption {
return func(p *Program) {
p.kittyFlags = flags
p.startupOptions |= withKittyKeyboard
}
}

// _WithModifyOtherKeys enables support for the XTerm modifyOtherKeys feature.
// WithModifyOtherKeys enables support for the XTerm modifyOtherKeys feature.
// This feature allows the terminal to report ambiguous keys as escape codes.
// This is useful for terminals that don't support the Kitty keyboard protocol.
//
Expand All @@ -307,21 +309,21 @@ func _WithKittyKeyboard(flags int) ProgramOption {
// and Meta-<key>
//
// See https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
func _WithModifyOtherKeys(mode int) ProgramOption {
func WithModifyOtherKeys(mode int) ProgramOption {
return func(p *Program) {
p.modifyOtherKeys = mode
p.startupOptions |= withModifyOtherKeys
}
}

// _WithWindowsInputMode enables Windows Input Mode (win32-input-mode) which
// WithWindowsInputMode enables Windows Input Mode (win32-input-mode) which
// allows for more advanced input handling and reporting. This is experimental
// and may not work on all terminals.
//
// See
// https://github.com/microsoft/terminal/blob/main/doc/specs/%234999%20-%20Improved%20keyboard%20handling%20in%20Conpty.md
// for more information.
func _WithWindowsInputMode() ProgramOption { //nolint:unused
func WithWindowsInputMode() ProgramOption { //nolint:unused
return func(p *Program) {
p.startupOptions |= withWindowsInputMode
p.win32Input = true
Expand Down
4 changes: 2 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
case 'u' | '?'<<parser.MarkerShift:
// Kitty keyboard flags
if param := csi.Param(0); param != -1 {
return i, _KittyKeyboardMsg(param)
return i, KittyKeyboardMsg(param)
}
case 'R' | '?'<<parser.MarkerShift:
// This report may return a third parameter representing the page
Expand All @@ -266,7 +266,7 @@
case 'm' | '>'<<parser.MarkerShift:
// XTerm modifyOtherKeys
if paramsLen == 2 && csi.Param(0) == 4 && csi.Param(1) != -1 {
return i, modifyOtherKeysMsg(csi.Param(1))
return i, ModifyOtherKeysMsg(csi.Param(1))

Check failure on line 269 in parse.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int -> uint8 (gosec)

Check failure on line 269 in parse.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int -> uint8 (gosec)
}
case 'I':
return i, FocusMsg{}
Expand Down Expand Up @@ -418,15 +418,15 @@
case 8:
k = KeyPressMsg{Code: KeyEnd}
case 11, 12, 13, 14, 15:
k = KeyPressMsg{Code: KeyF1 + rune(param-11)}

Check failure on line 421 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 11, in <argument> detected (gomnd)
case 17, 18, 19, 20, 21:
k = KeyPressMsg{Code: KeyF6 + rune(param-17)}

Check failure on line 423 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 17, in <argument> detected (gomnd)
case 23, 24, 25, 26:
k = KeyPressMsg{Code: KeyF11 + rune(param-23)}

Check failure on line 425 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 23, in <argument> detected (gomnd)
case 28, 29:
k = KeyPressMsg{Code: KeyF15 + rune(param-28)}

Check failure on line 427 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 28, in <argument> detected (gomnd)
case 31, 32, 33, 34:
k = KeyPressMsg{Code: KeyF17 + rune(param-31)}

Check failure on line 429 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 31, in <argument> detected (gomnd)
}

// modifiers
Expand Down Expand Up @@ -836,10 +836,10 @@
default:
if b >= ansi.SOH && b <= ansi.SUB {
// Use lower case letters for control codes
code := rune(b + 0x60)

Check failure on line 839 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 0x60, in <argument> detected (gomnd)
return KeyPressMsg{Code: code, Mod: ModCtrl}
} else if b >= ansi.FS && b <= ansi.US {
code := rune(b + 0x40)

Check failure on line 842 in parse.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 0x40, in <argument> detected (gomnd)
return KeyPressMsg{Code: code, Mod: ModCtrl}
}
return UnknownMsg(b)
Expand Down
2 changes: 1 addition & 1 deletion screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestClearMsg(t *testing.T) {
},
{
name: "kitty_start",
cmds: []Cmd{disableKittyKeyboard, enableKittyKeyboard(3)},
cmds: []Cmd{DisableKittyKeyboard, EnableKittyKeyboard(3)},
expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[>u\x1b[>3u\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[>0u",
},
}
Expand Down
2 changes: 1 addition & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
case cursorColorMsg:
p.execute(ansi.RequestCursorColor)

case _KittyKeyboardMsg:
case KittyKeyboardMsg:
// Store the kitty flags whenever they are queried.
p.kittyFlags = int(msg)

Expand Down
8 changes: 4 additions & 4 deletions win32input.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
// enableWin32InputMsg is a message that enables Windows input mode.
type enableWin32InputMsg struct{}

// enableWindowsInputMode is a command that enables Windows input mode
// EnableWindowsInputMode is a command that enables Windows input mode
// (win32-input-mode).
//
// See
// https://github.com/microsoft/terminal/blob/main/doc/specs/%234999%20-%20Improved%20keyboard%20handling%20in%20Conpty.md
// for more information.
func enableWindowsInputMode() Msg { //nolint:unused
func EnableWindowsInputMode() Msg { //nolint:unused
return enableWin32InputMsg{}
}

// disableWin32InputMsg is a message that disables Windows input mode.
type disableWin32InputMsg struct{}

// disableWindowsInputMode is a command that disables Windows input mode
// DisableWindowsInputMode is a command that disables Windows input mode
// (win32-input-mode).
//
// See
// https://github.com/microsoft/terminal/blob/main/doc/specs/%234999%20-%20Improved%20keyboard%20handling%20in%20Conpty.md
// for more information.
func disableWindowsInputMode() Msg { //nolint:unused
func DisableWindowsInputMode() Msg { //nolint:unused
return disableWin32InputMsg{}
}

Expand Down
16 changes: 8 additions & 8 deletions xterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ type setModifyOtherKeysMsg int
// and Meta-<key>
//
// See https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
func enableModifyOtherKeys(mode int) Cmd { //nolint:unused
func EnableModifyOtherKeys(mode int) Cmd { //nolint:unused
return func() Msg {
return setModifyOtherKeysMsg(mode)
}
}

// disableModifyOtherKeys is a command to disable XTerm modifyOtherKeys mode.
func disableModifyOtherKeys() Msg { //nolint:unused
// DisableModifyOtherKeys is a command to disable XTerm modifyOtherKeys mode.
func DisableModifyOtherKeys() Msg { //nolint:unused
return setModifyOtherKeysMsg(0)
}

Expand Down Expand Up @@ -58,23 +58,23 @@ func parseXTermModifyOtherKeys(csi *ansi.CsiSequence) Msg {
// modifyOtherKeys mode.
type modifyOtherKeys struct{}

// _ModifyOtherKeys is a command that queries the terminal for its
// ModifyOtherKeys is a command that queries the terminal for its
// modifyOtherKeys mode.
func _ModifyOtherKeys() Msg { //nolint:unused
func ModifyOtherKeys() Msg { //nolint:unused
return modifyOtherKeys{}
}

// modifyOtherKeysMsg is a message that represents XTerm modifyOtherKeys
// ModifyOtherKeysMsg is a message that represents XTerm modifyOtherKeys
// report. Querying the terminal for the modifyOtherKeys mode will return a
// modifyOtherKeysMsg message with the current mode set.
// ModifyOtherKeysMsg message with the current mode set.
//
// 0: disable
// 1: enable mode 1
// 2: enable mode 2
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
type modifyOtherKeysMsg uint8
type ModifyOtherKeysMsg uint8

// TerminalVersionMsg is a message that represents the terminal version.
type TerminalVersionMsg string
Expand Down
Loading