Skip to content

Commit

Permalink
Use escape sequence in prepareArea
Browse files Browse the repository at this point in the history
Add SaveCursorPosition and
RestoreCurcorPosition to output interafce
and use it in prepareArea
Fix #70

Co-authored-by: Masashi Shibata <c-bata@users.noreply.github.com>
  • Loading branch information
unasuke and c-bata committed May 4, 2022
1 parent 82a9122 commit 5c25161
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
10 changes: 10 additions & 0 deletions output_vt100.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5c25161

Please sign in to comment.