From 6e1065830aebbef76ad787b982af5064ac584b55 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Thu, 25 Aug 2022 19:13:30 +0200 Subject: [PATCH] feat: support a Clear command --- nil_renderer.go | 1 + renderer.go | 3 +++ screen.go | 14 ++++++++++++++ standard_renderer.go | 8 ++++++++ tea.go | 3 +++ 5 files changed, 29 insertions(+) diff --git a/nil_renderer.go b/nil_renderer.go index 37b3c45..a022636 100644 --- a/nil_renderer.go +++ b/nil_renderer.go @@ -7,6 +7,7 @@ func (n nilRenderer) stop() {} func (n nilRenderer) kill() {} func (n nilRenderer) write(v string) {} func (n nilRenderer) repaint() {} +func (n nilRenderer) clearScreen() {} func (n nilRenderer) altScreen() bool { return false } func (n nilRenderer) enterAltScreen() {} func (n nilRenderer) exitAltScreen() {} diff --git a/renderer.go b/renderer.go index a975e33..a6f4162 100644 --- a/renderer.go +++ b/renderer.go @@ -21,6 +21,9 @@ type renderer interface { // in succession. repaint() + // Clears the terminal. + clearScreen() + // Whether or not the alternate screen buffer is enabled. altScreen() bool // Enable the alternate screen buffer. diff --git a/screen.go b/screen.go index e8df7b1..5780290 100644 --- a/screen.go +++ b/screen.go @@ -9,6 +9,20 @@ type WindowSizeMsg struct { Height int } +// ClearScreen is a special command that tells the program to clear the screen +// before the next update. This can be used to move the cursor to the top left +// of the screen and clear visual clutter when the alt screen is not in use. +// +// Note that it should never be necessary to call ClearScreen() for regular +// redraws. +func ClearScreen() Msg { + return clearScreenMsg{} +} + +// clearScreenMsg is an internal message that signals to clear the screen. +// You can send a clearScreenMsg with ClearScreen. +type clearScreenMsg struct{} + // EnterAltScreen is a special command that tells the Bubble Tea program to // enter the alternate screen buffer. // diff --git a/standard_renderer.go b/standard_renderer.go index feafb82..4edf214 100644 --- a/standard_renderer.go +++ b/standard_renderer.go @@ -245,6 +245,14 @@ func (r *standardRenderer) repaint() { r.lastRender = "" } +func (r *standardRenderer) clearScreen() { + r.mtx.Lock() + defer r.mtx.Unlock() + + r.out.ClearScreen() + r.out.MoveCursor(1, 1) +} + func (r *standardRenderer) altScreen() bool { return r.altScreenActive } diff --git a/tea.go b/tea.go index c561c2d..9b38062 100644 --- a/tea.go +++ b/tea.go @@ -378,6 +378,9 @@ func (p *Program) StartReturningModel() (Model, error) { p.shutdown(false) return model, nil + case clearScreenMsg: + p.renderer.clearScreen() + case enterAltScreenMsg: p.renderer.enterAltScreen()