From 01055d36b022ad846472d18aab29ad5f9f94d549 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 10 Apr 2026 15:37:20 +0530 Subject: [PATCH] Move roundtrip on exit flag into terminal options to have a consistent API --- tools/cmd/at/tty_io.go | 2 +- tools/tui/loop/api.go | 19 ++++++++++--------- tools/tui/loop/run.go | 3 ++- tools/tui/loop/terminal-state.go | 6 ++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/tools/cmd/at/tty_io.go b/tools/cmd/at/tty_io.go index 68e848279..ea5db0552 100644 --- a/tools/cmd/at/tty_io.go +++ b/tools/cmd/at/tty_io.go @@ -33,7 +33,7 @@ func do_chunked_io(io_data *rc_io_data) (serialized_response []byte, err error) } if io_data.on_key_event != nil { lp.FullKeyboardProtocol() - lp.NoRoundtripToTerminalOnExit = false + lp.NoRoundtripToTerminalOnExit() } else { lp.NoKeyboardStateChange() } diff --git a/tools/tui/loop/api.go b/tools/tui/loop/api.go index 514b28d80..946ef6793 100644 --- a/tools/tui/loop/api.go +++ b/tools/tui/loop/api.go @@ -62,13 +62,6 @@ type Loop struct { // Queried capabilities from terminal TerminalCapabilities TerminalCapabilities - // Set this to true to avoid doing a query response loop to the terminal at - // exit. This loop is needed for most kittens to ensure that in-flight - // responses such as in-band resize notifications, color queries, kitty - // keyboard events, etc. are not leaked to the shell. For some special - // purpose uses of the loop, this is not appropriate, hence this setting. - NoRoundtripToTerminalOnExit bool - // Suspend the loop restoring terminal state, and run the provided function. When it returns terminal state is // put back to what it was before suspending unless the function returns an error or an error occurs saving/restoring state. SuspendAndRun func(func() error) error @@ -147,10 +140,9 @@ func New(options ...func(self *Loop)) (*Loop, error) { func NewForSimpleInteraction() (*Loop, error) { lp, err := New( NoAlternateScreen, NoRestoreColors, NoMouseTracking, NoInBandResizeNotifications, - NoFocusTracking, NoKeyboardStateChange, + NoFocusTracking, NoKeyboardStateChange, NoRoundtripToTerminalOnExit, ) if err == nil { - lp.NoRoundtripToTerminalOnExit = true lp.terminal_options.color_scheme_change_notification = false } return lp, err @@ -177,6 +169,15 @@ func NoAlternateScreen(self *Loop) { self.terminal_options.Alternate_screen = false } +func (self *Loop) NoRoundtripToTerminalOnExit() *Loop { + self.terminal_options.roundtrip_on_exit = false + return self +} + +func NoRoundtripToTerminalOnExit(self *Loop) { + self.terminal_options.roundtrip_on_exit = false +} + func (self *Loop) OnlyDisambiguateKeys() *Loop { self.terminal_options.kitty_keyboard_mode = DISAMBIGUATE_KEYS return self diff --git a/tools/tui/loop/run.go b/tools/tui/loop/run.go index ed7bf05ad..cf6651cf7 100644 --- a/tools/tui/loop/run.go +++ b/tools/tui/loop/run.go @@ -30,6 +30,7 @@ func new_loop() *Loop { l.terminal_options.in_band_resize_notification = true l.terminal_options.color_scheme_change_notification = false l.terminal_options.kitty_keyboard_mode = DISAMBIGUATE_KEYS | REPORT_ALTERNATE_KEYS | REPORT_ALL_KEYS_AS_ESCAPE_CODES | REPORT_TEXT_WITH_KEYS + l.terminal_options.roundtrip_on_exit = true l.escape_code_parser.HandleCSI = l.handle_csi l.escape_code_parser.HandleOSC = l.handle_osc l.escape_code_parser.HandleDCS = l.handle_dcs @@ -452,7 +453,7 @@ func (self *Loop) run() (err error) { case <-tty_leftover_read_channel: default: } - if !self.NoRoundtripToTerminalOnExit { + if self.terminal_options.roundtrip_on_exit { // ensure that any terminal responses such as kitty keyboard events, // color scheme changes, in-band resize notifications, etc. do not // bleed into the shell. diff --git a/tools/tui/loop/terminal-state.go b/tools/tui/loop/terminal-state.go index dae8746c4..c3e7ad616 100644 --- a/tools/tui/loop/terminal-state.go +++ b/tools/tui/loop/terminal-state.go @@ -106,6 +106,12 @@ type TerminalStateOptions struct { in_band_resize_notification bool focus_tracking bool color_scheme_change_notification bool + // Set this to true to avoid doing a query response loop to the terminal at + // exit. This loop is needed for most kittens to ensure that in-flight + // responses such as in-band resize notifications, color queries, kitty + // keyboard events, etc. are not leaked to the shell. For some special + // purpose uses of the loop, this is not appropriate, hence this setting. + roundtrip_on_exit bool } func set_modes(sb *strings.Builder, modes ...Mode) {