From 43b11c30b36eddd75472efc83d8e76fadbf72e7f Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 22 Oct 2024 17:26:16 -0400 Subject: [PATCH] fix: make grapheme clustering opt-in This commit changes the behavior of grapheme clustering to be opt-in instead of opt-out. This is because some terminals like Apple Terminal don't support DECRQM and DECRPM, which are required for querying and setting the grapheme clustering state. Apple Terminal, instead, replies with a `p` character when querying the grapheme clustering state, which is not a valid response and breaks the program output. --- examples/simple/testdata/TestApp.golden | 2 +- options.go | 6 ++--- screen_test.go | 33 ++++++++++++++----------- tea.go | 4 +-- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/simple/testdata/TestApp.golden b/examples/simple/testdata/TestApp.golden index 2c245f9c58..28dc7991b3 100644 --- a/examples/simple/testdata/TestApp.golden +++ b/examples/simple/testdata/TestApp.golden @@ -1,4 +1,4 @@ -[?25l[?2004h[?2027h[?2027$p Hi. This program will exit in 10 seconds. +[?25l[?2004h Hi. This program will exit in 10 seconds. To quit sooner press ctrl-c, or press ctrl-z to suspend... Hi. This program will exit in 9 seconds. diff --git a/options.go b/options.go index 08fb7403ec..db0810b0e7 100644 --- a/options.go +++ b/options.go @@ -254,7 +254,7 @@ func WithKeyboardEnhancements(enhancements ...KeyboardEnhancement) ProgramOption } } -// WithoutGraphemeClustering disables grapheme clustering. This is useful if you +// WithGraphemeClustering disables grapheme clustering. This is useful if you // want to disable grapheme clustering for your program. // // Grapheme clustering is a character width calculation method that accurately @@ -263,8 +263,8 @@ func WithKeyboardEnhancements(enhancements ...KeyboardEnhancement) ProgramOption // characters. // // See https://mitchellh.com/writing/grapheme-clusters-in-terminals -func WithoutGraphemeClustering() ProgramOption { +func WithGraphemeClustering() ProgramOption { return func(p *Program) { - p.startupOptions |= withoutGraphemeClustering + p.startupOptions |= withGraphemeClustering } } diff --git a/screen_test.go b/screen_test.go index 4e569fd50f..4716bb1d03 100644 --- a/screen_test.go +++ b/screen_test.go @@ -17,62 +17,67 @@ func TestClearMsg(t *testing.T) { { name: "clear_screen", cmds: []Cmd{ClearScreen}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[2J\x1b[1;1H\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b[2J\x1b[1;1H\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "altscreen", cmds: []Cmd{EnterAltScreen, ExitAltScreen}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "altscreen_autoexit", cmds: []Cmd{EnterAltScreen}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1049l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1049l\x1b[?25h", }, { name: "mouse_cellmotion", cmds: []Cmd{EnableMouseCellMotion}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l", + expected: "\x1b[?25l\x1b[?2004h\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l", }, { name: "mouse_allmotion", cmds: []Cmd{EnableMouseAllMotion}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l", + expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l", }, { name: "mouse_disable", cmds: []Cmd{EnableMouseAllMotion, DisableMouse}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "cursor_hide", cmds: []Cmd{HideCursor}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "cursor_hideshow", cmds: []Cmd{HideCursor, ShowCursor}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?25h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l", + expected: "\x1b[?25l\x1b[?2004h\x1b[?25h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l", }, { name: "bp_stop_start", cmds: []Cmd{DisableBracketedPaste, EnableBracketedPaste}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "read_set_clipboard", cmds: []Cmd{ReadClipboard, SetClipboard("success")}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b]52;c;?\a\x1b]52;c;c3VjY2Vzcw==\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b]52;c;?\a\x1b]52;c;c3VjY2Vzcw==\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "bg_fg_cur_color", cmds: []Cmd{ForegroundColor, BackgroundColor, CursorColor}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b]10;?\a\x1b]11;?\a\x1b]12;?\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\x1b]10;?\a\x1b]11;?\a\x1b]12;?\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }, { name: "bg_set_color", cmds: []Cmd{SetBackgroundColor(color.RGBA{255, 255, 255, 255})}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b]11;#ffffff\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b]111\a", + expected: "\x1b[?25l\x1b[?2004h\x1b]11;#ffffff\a\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b]111\a", + }, + { + name: "grapheme_clustering", + cmds: []Cmd{EnableGraphemeClustering}, + expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[?2027l", }, } @@ -81,13 +86,13 @@ func TestClearMsg(t *testing.T) { tests = append(tests, test{ name: "kitty_start", cmds: []Cmd{DisableKeyboardEnhancements, EnableKeyboardEnhancements(WithKeyReleases)}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", + expected: "\x1b[?25l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h", }) } else { tests = append(tests, test{ name: "kitty_start", cmds: []Cmd{DisableKeyboardEnhancements, EnableKeyboardEnhancements(WithKeyReleases)}, - expected: "\x1b[?25l\x1b[?2004h\x1b[?2027h\x1b[?2027$p\x1b[>4;1m\x1b[>3u\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[>4;0m\x1b[>0u", + expected: "\x1b[?25l\x1b[?2004h\x1b[>4;1m\x1b[>3u\rsuccess\r\n\x1b[D\x1b[2K\r\x1b[?2004l\x1b[?25h\x1b[>4;0m\x1b[>0u", }) } diff --git a/tea.go b/tea.go index 22a07bc8db..b0028c2195 100644 --- a/tea.go +++ b/tea.go @@ -103,7 +103,7 @@ const ( withoutBracketedPaste withReportFocus withKeyboardEnhancements - withoutGraphemeClustering + withGraphemeClustering ) // channelHandlers manages the series of channels returned by various processes. @@ -733,7 +733,7 @@ func (p *Program) Run() (Model, error) { p.execute(ansi.EnableBracketedPaste) p.modes[ansi.BracketedPasteMode] = true } - if p.startupOptions&withoutGraphemeClustering == 0 { + if p.startupOptions&withGraphemeClustering != 0 { p.execute(ansi.EnableGraphemeClustering) p.execute(ansi.RequestGraphemeClustering) // We store the state of grapheme clustering after we query it and get