From fd18c149dfa11d1e88e3929bec3d093a892b1607 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 9 Oct 2022 05:22:17 +0200 Subject: [PATCH] fix: restoreTerminalState resets mouse handling state - fixes mouse handling being enabled during sub commands - fixes shutdown order: restore mouse state before alt screen --- screen_test.go | 16 ++++++++-------- tea.go | 10 +--------- tty.go | 9 +++++++++ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/screen_test.go b/screen_test.go index 5e3d4e6..33b5169 100644 --- a/screen_test.go +++ b/screen_test.go @@ -14,42 +14,42 @@ func TestClearMsg(t *testing.T) { { name: "clear_screen", cmds: []Cmd{ClearScreen}, - expected: "\x1b[?25l\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "altscreen", cmds: []Cmd{EnterAltScreen, ExitAltScreen}, - expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?1049lsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?1049lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "altscreen_autoexit", cmds: []Cmd{EnterAltScreen}, - expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?1049l\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1Hsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1049l", }, { name: "mouse_cellmotion", cmds: []Cmd{EnableMouseCellMotion}, - expected: "\x1b[?25l\x1b[?1002hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?1002hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "mouse_allmotion", cmds: []Cmd{EnableMouseAllMotion}, - expected: "\x1b[?25l\x1b[?1003hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?1003hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "mouse_disable", cmds: []Cmd{EnableMouseAllMotion, DisableMouse}, - expected: "\x1b[?25l\x1b[?1003h\x1b[?1002l\x1b[?1003lsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?1003h\x1b[?1002l\x1b[?1003lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "cursor_hide", cmds: []Cmd{HideCursor}, - expected: "\x1b[?25l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?25lsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, { name: "cursor_hideshow", cmds: []Cmd{HideCursor, ShowCursor}, - expected: "\x1b[?25l\x1b[?25l\x1b[?25hsuccess\r\n\x1b[0D\x1b[2K\x1b[?1002l\x1b[?1003l\x1b[?25h", + expected: "\x1b[?25l\x1b[?25l\x1b[?25hsuccess\r\n\x1b[0D\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l", }, } diff --git a/tea.go b/tea.go index 8a992e9..fbd7235 100644 --- a/tea.go +++ b/tea.go @@ -18,7 +18,6 @@ import ( "runtime/debug" "sync" "syscall" - "time" "github.com/containerd/console" isatty "github.com/mattn/go-isatty" @@ -531,11 +530,8 @@ func (p *Program) shutdown(kill bool) { p.renderer.stop() } } - p.ExitAltScreen() - p.DisableMouseCellMotion() - p.DisableMouseAllMotion() - _ = p.restoreTerminalState() + _ = p.restoreTerminalState() if p.restoreOutput != nil { _ = p.restoreOutput() } @@ -549,10 +545,6 @@ func (p *Program) ReleaseTerminal() error { p.waitForReadLoop() p.altScreenWasActive = p.renderer.altScreen() - if p.renderer.altScreen() { - p.ExitAltScreen() - time.Sleep(time.Millisecond * 10) // give the terminal a moment to catch up - } return p.restoreTerminalState() } diff --git a/tty.go b/tty.go index a38aa61..4f33d86 100644 --- a/tty.go +++ b/tty.go @@ -30,6 +30,15 @@ func (p *Program) initTerminal() error { func (p *Program) restoreTerminalState() error { if p.renderer != nil { p.renderer.showCursor() + p.renderer.disableMouseCellMotion() + p.renderer.disableMouseAllMotion() + + if p.renderer.altScreen() { + p.renderer.exitAltScreen() + + // give the terminal a moment to catch up + time.Sleep(time.Millisecond * 10) + } } if p.console != nil {