refactor: change several message types to structs for extensibility

This commit is contained in:
Ayman Bagabas 2025-11-04 14:01:26 -05:00
parent 7a6e8fcb87
commit b947ade70d
No known key found for this signature in database
GPG key ID: C8D51D2157C919AC
6 changed files with 35 additions and 10 deletions

View file

@ -4,7 +4,9 @@ package tea
type Position struct{ X, Y int }
// CursorPositionMsg is a message that represents the terminal cursor position.
type CursorPositionMsg Position
type CursorPositionMsg struct {
X, Y int
}
// CursorShape represents a terminal cursor shape.
type CursorShape int

View file

@ -6,24 +6,26 @@ import (
// KeyboardEnhancementsMsg is a message that gets sent when the terminal
// supports keyboard enhancements.
type KeyboardEnhancementsMsg uint8
type KeyboardEnhancementsMsg struct {
Flags int
}
// SupportsKeyDisambiguation returns whether the terminal supports reporting
// disambiguous keys as escape codes.
func (k KeyboardEnhancementsMsg) SupportsKeyDisambiguation() bool {
return k&ansi.KittyDisambiguateEscapeCodes != 0
return k.Flags&ansi.KittyDisambiguateEscapeCodes != 0
}
// SupportsKeyReleases returns whether the terminal supports key release
// events.
func (k KeyboardEnhancementsMsg) SupportsKeyReleases() bool {
return k&ansi.KittyReportEventTypes != 0
return k.Flags&ansi.KittyReportEventTypes != 0
}
// SupportsUniformKeyLayout returns whether the terminal supports reporting key
// events as though they were on a PC-101 layout.
func (k KeyboardEnhancementsMsg) SupportsUniformKeyLayout() bool {
return k.SupportsKeyDisambiguation() &&
k&ansi.KittyReportAlternateKeys != 0 &&
k&ansi.KittyReportAllKeysAsEscapeCodes != 0
k.Flags&ansi.KittyReportAlternateKeys != 0 &&
k.Flags&ansi.KittyReportAllKeysAsEscapeCodes != 0
}

View file

@ -2,7 +2,14 @@ package tea
// PasteMsg is an message that is emitted when a terminal receives pasted text
// using bracketed-paste.
type PasteMsg string
type PasteMsg struct {
Content string
}
// String returns the pasted content as a string.
func (p PasteMsg) String() string {
return p.Content
}
// PasteStartMsg is an message that is emitted when the terminal starts the
// bracketed-paste text.

2
tea.go
View file

@ -692,7 +692,7 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
}
case CapabilityMsg:
switch msg {
switch msg.Content {
case "RGB", "Tc":
if *p.profile != colorprofile.TrueColor {
tc := colorprofile.TrueColor

View file

@ -38,4 +38,11 @@ func RequestCapability(s string) Cmd {
// (XTGETTCAP) requests.
//
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
type CapabilityMsg string
type CapabilityMsg struct {
Content string
}
// String returns the capability content as a string.
func (c CapabilityMsg) String() string {
return c.Content
}

View file

@ -1,7 +1,14 @@
package tea
// TerminalVersionMsg is a message that represents the terminal version.
type TerminalVersionMsg string
type TerminalVersionMsg struct {
Name string
}
// String returns the terminal name as a string.
func (t TerminalVersionMsg) String() string {
return t.Name
}
// terminalVersion is an internal message that queries the terminal for its
// version using XTVERSION.