From 2827e8fa038ad228d3b3c9195cfe51747152cf90 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 26 May 2022 16:38:23 -0400 Subject: [PATCH] Create SetDebug method. Non-stub only on a terminfo screen. --- console_win.go | 6 +++++ screen.go | 5 ++++ simulation.go | 4 ++++ tscreen.go | 62 +++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/console_win.go b/console_win.go index 5f0063e..b32e0e2 100644 --- a/console_win.go +++ b/console_win.go @@ -1327,3 +1327,9 @@ func (s *cScreen) Suspend() error { func (s *cScreen) Resume() error { return s.engage() } + +// Set Debug disables tty mode munging and makes +// control sequences visible. The ma +func (t *tScreen) SetDebug(mode bool) { + return errors.New("SetDebug is not supported on the Windows console") +} diff --git a/screen.go b/screen.go index 43c3a54..b972df7 100644 --- a/screen.go +++ b/screen.go @@ -248,6 +248,11 @@ type Screen interface { // does not support application-initiated resizing, whereas the legacy terminal does. // Also, some emulators can support this but may have it disabled by default. SetSize(int, int) + + // Set debug mode. Must be called before Init(). Inhibits switching to alternate + // buffer, disables tty mode-switching. Control sequences are shipped in a ASCII-ized + // form. Note, this entry point is experimental and subect to change. + SetDebug(bool) error } // NewScreen returns a default Screen suitable for the user's terminal diff --git a/simulation.go b/simulation.go index 9ad6131..5f1abef 100644 --- a/simulation.go +++ b/simulation.go @@ -547,3 +547,7 @@ func (s *simscreen) Suspend() error { func (s *simscreen) Resume() error { return nil } + +func (s *simscreen) SetDebug(bool) error { + return nil +} diff --git a/tscreen.go b/tscreen.go index 59afd2c..56fc8b9 100644 --- a/tscreen.go +++ b/tscreen.go @@ -17,6 +17,7 @@ package tcell import ( "bytes" "errors" + "fmt" "io" "os" "strconv" @@ -159,6 +160,7 @@ type tScreen struct { wg sync.WaitGroup mouseFlags MouseFlags pasteEnabled bool + debug bool sync.Mutex } @@ -876,6 +878,39 @@ func (t *tScreen) writeString(s string) { } func (t *tScreen) TPuts(s string) { + if t.debug { + v := "" + for i := range s { + // First, deal with printables + if s[i] >= 33 && s[i] <= 126 { + v += string(s[i]) + continue + } + switch s[i] { + case 7: // BEL + v += `\a` + case 8: // BS + v += `\b` + case 9: // TAB + v += `\t` + case 10: // LF + v += `\n` + case 11: // VT + v += `\v` + case 12: // FF + v += `\f` + case 13: // CR + v += `\r` + case 27: // ESC // ESC is why we have custom code here + v += `\e` + case 32: // SPC + v += `\s` + default: + v += fmt.Sprintf(`\%02x`, s[i]) + } + } + s = v + } if t.buffering { t.ti.TPuts(&t.buf, s) } else { @@ -1777,7 +1812,7 @@ func (t *tScreen) Resume() error { func (t *tScreen) engage() error { t.Lock() defer t.Unlock() - if t.tty == nil { + if !t.debug && t.tty == nil { return ErrNoScreen } t.tty.NotifyResize(func() { @@ -1789,8 +1824,10 @@ func (t *tScreen) engage() error { if t.running { return errors.New("already engaged") } - if err := t.tty.Start(); err != nil { - return err + if !t.debug { + if err := t.tty.Start(); err != nil { + return err + } } t.running = true if w, h, err := t.tty.WindowSize(); err == nil && w != 0 && h != 0 { @@ -1828,7 +1865,9 @@ func (t *tScreen) disengage() { t.running = false stopQ := t.stopQ close(stopQ) - _ = t.tty.Drain() + if !t.debug { + _ = t.tty.Drain() + } t.Unlock() t.tty.NotifyResize(nil) @@ -1850,7 +1889,9 @@ func (t *tScreen) disengage() { t.enableMouse(0) t.enablePasting(false) - _ = t.tty.Stop() + if !t.debug { + _ = t.tty.Stop() + } } // Beep emits a beep to the terminal. @@ -1863,5 +1904,14 @@ func (t *tScreen) Beep() error { // to it's initial state. It should not be called more than once. func (t *tScreen) finalize() { t.disengage() - _ = t.tty.Close() + if !t.debug { + _ = t.tty.Close() + } +} + +// Set Debug disables tty mode munging and makes +// control sequences visible. +func (t *tScreen) SetDebug(mode bool) error { + t.debug = mode + return nil } -- 2.30.2