diff --git a/tools/tui/key-encoding.go b/tools/tui/key-encoding.go index 1b67e9227..339fdab24 100644 --- a/tools/tui/key-encoding.go +++ b/tools/tui/key-encoding.go @@ -51,6 +51,7 @@ type KeyEvent struct { ShiftedKey string AlternateKey string Text string + Handled bool } func KeyEventFromCSI(csi string) *KeyEvent { diff --git a/tools/tui/loop.go b/tools/tui/loop.go index 8be9c0fb1..57c74dc3a 100644 --- a/tools/tui/loop.go +++ b/tools/tui/loop.go @@ -19,9 +19,42 @@ type Loop struct { death_signal Signal exit_code int write_buf []byte + + // Callbacks + OnKeyEvent func(loop *Loop, event *KeyEvent) error + OnText func(loop *Loop, text string, from_key_event bool, in_bracketed_paste bool) error } func (self *Loop) handle_csi(raw []byte) error { + csi := string(raw) + ke := KeyEventFromCSI(csi) + if ke != nil { + return self.handle_key_event(ke) + } + return nil +} + +func (self *Loop) handle_key_event(ev *KeyEvent) error { + if self.OnKeyEvent != nil { + err := self.OnKeyEvent(self, ev) + if err != nil { + return err + } + if ev.Handled { + return nil + } + } + if ev.MatchesPressOrRepeat("ctrl+c") { + ev.Handled = true + return self.on_SIGINT() + } + if ev.MatchesPressOrRepeat("ctrl+z") { + ev.Handled = true + return self.on_SIGTSTP() + } + if ev.Text != "" && self.OnText != nil { + return self.OnText(self, ev.Text, true, false) + } return nil } @@ -46,6 +79,9 @@ func (self *Loop) handle_pm(raw []byte) error { } func (self *Loop) handle_rune(raw rune) error { + if self.OnText != nil { + return self.OnText(self, string(raw), false, self.escape_code_parser.InBracketedPaste()) + } return nil } diff --git a/tools/utils/escape_code_parser.go b/tools/utils/escape_code_parser.go index 99a0862be..de2ab33e1 100644 --- a/tools/utils/escape_code_parser.go +++ b/tools/utils/escape_code_parser.go @@ -51,6 +51,8 @@ type EscapeCodeParser struct { HandleAPC func([]byte) error } +func (self *EscapeCodeParser) InBracketedPaste() bool { return self.state == bracketed_paste } + func (self *EscapeCodeParser) Parse(data []byte) error { prev := UTF8_ACCEPT codep := UTF8_ACCEPT