From 5c25161dc4ba016c4ac18cd94810b095f26d48a4 Mon Sep 17 00:00:00 2001 From: Yusuke Nakamura Date: Wed, 4 May 2022 21:18:11 +0900 Subject: [PATCH] Use escape sequence in prepareArea Add SaveCursorPosition and RestoreCurcorPosition to output interafce and use it in prepareArea Fix #70 Co-authored-by: Masashi Shibata --- output.go | 4 ++++ output_vt100.go | 10 ++++++++++ render.go | 8 ++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/output.go b/output.go index 42531b41..74143e79 100644 --- a/output.go +++ b/output.go @@ -146,6 +146,10 @@ type ConsoleWriter interface { ScrollDown() // ScrollUp scroll display up one line. ScrollUp() + // SaveCursorPosition saves current cursor position. + SaveCursorPosition() + // RestoreCursorPosition restores cursor position. + RestoreCursorPosition() /* Title */ diff --git a/output_vt100.go b/output_vt100.go index 20850fea..6e154697 100644 --- a/output_vt100.go +++ b/output_vt100.go @@ -174,6 +174,16 @@ func (w *VT100Writer) ScrollUp() { w.WriteRaw([]byte{0x1b, 'M'}) } +// SaveCursorPosition saves current cursor position. +func (w *VT100Writer) SaveCursorPosition() { + w.WriteRaw([]byte{0x1b, '[', 's'}) +} + +// RestoreCursorPosition restores cursor position. +func (w *VT100Writer) RestoreCursorPosition() { + w.WriteRaw([]byte{0x1b, '[', 'u'}) +} + /* Title */ // SetTitle sets a title of terminal window. diff --git a/render.go b/render.go index 299ac48b..d8410718 100644 --- a/render.go +++ b/render.go @@ -69,12 +69,12 @@ func (r *Render) TearDown() { } func (r *Render) prepareArea(lines int) { + r.out.SaveCursorPosition() + for i := 0; i < lines; i++ { - r.out.ScrollDown() - } - for i := 0; i < lines; i++ { - r.out.ScrollUp() + r.out.WriteStr("\n") } + r.out.RestoreCursorPosition() } // UpdateWinSize called when window size is changed.