fix: restoreTerminalState resets mouse handling state

- fixes mouse handling being enabled during sub commands
- fixes shutdown order: restore mouse state before alt screen
This commit is contained in:
Christian Muehlhaeuser 2022-10-09 05:22:17 +02:00
parent 4aa9ce489b
commit fd18c149df
3 changed files with 18 additions and 17 deletions

View File

@ -14,42 +14,42 @@ func TestClearMsg(t *testing.T) {
{ {
name: "clear_screen", name: "clear_screen",
cmds: []Cmd{ClearScreen}, 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", name: "altscreen",
cmds: []Cmd{EnterAltScreen, ExitAltScreen}, 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", name: "altscreen_autoexit",
cmds: []Cmd{EnterAltScreen}, 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", name: "mouse_cellmotion",
cmds: []Cmd{EnableMouseCellMotion}, 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", name: "mouse_allmotion",
cmds: []Cmd{EnableMouseAllMotion}, 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", name: "mouse_disable",
cmds: []Cmd{EnableMouseAllMotion, DisableMouse}, 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", name: "cursor_hide",
cmds: []Cmd{HideCursor}, 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", name: "cursor_hideshow",
cmds: []Cmd{HideCursor, ShowCursor}, 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",
}, },
} }

10
tea.go
View File

@ -18,7 +18,6 @@ import (
"runtime/debug" "runtime/debug"
"sync" "sync"
"syscall" "syscall"
"time"
"github.com/containerd/console" "github.com/containerd/console"
isatty "github.com/mattn/go-isatty" isatty "github.com/mattn/go-isatty"
@ -531,11 +530,8 @@ func (p *Program) shutdown(kill bool) {
p.renderer.stop() p.renderer.stop()
} }
} }
p.ExitAltScreen()
p.DisableMouseCellMotion()
p.DisableMouseAllMotion()
_ = p.restoreTerminalState()
_ = p.restoreTerminalState()
if p.restoreOutput != nil { if p.restoreOutput != nil {
_ = p.restoreOutput() _ = p.restoreOutput()
} }
@ -549,10 +545,6 @@ func (p *Program) ReleaseTerminal() error {
p.waitForReadLoop() p.waitForReadLoop()
p.altScreenWasActive = p.renderer.altScreen() 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() return p.restoreTerminalState()
} }

9
tty.go
View File

@ -30,6 +30,15 @@ func (p *Program) initTerminal() error {
func (p *Program) restoreTerminalState() error { func (p *Program) restoreTerminalState() error {
if p.renderer != nil { if p.renderer != nil {
p.renderer.showCursor() 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 { if p.console != nil {