feat: support a Clear command

This commit is contained in:
Raphael 'kena' Poss 2022-08-25 19:13:30 +02:00 committed by Christian Muehlhaeuser
parent 37b79f55f1
commit 6e1065830a
5 changed files with 29 additions and 0 deletions

View File

@ -7,6 +7,7 @@ func (n nilRenderer) stop() {}
func (n nilRenderer) kill() {} func (n nilRenderer) kill() {}
func (n nilRenderer) write(v string) {} func (n nilRenderer) write(v string) {}
func (n nilRenderer) repaint() {} func (n nilRenderer) repaint() {}
func (n nilRenderer) clearScreen() {}
func (n nilRenderer) altScreen() bool { return false } func (n nilRenderer) altScreen() bool { return false }
func (n nilRenderer) enterAltScreen() {} func (n nilRenderer) enterAltScreen() {}
func (n nilRenderer) exitAltScreen() {} func (n nilRenderer) exitAltScreen() {}

View File

@ -21,6 +21,9 @@ type renderer interface {
// in succession. // in succession.
repaint() repaint()
// Clears the terminal.
clearScreen()
// Whether or not the alternate screen buffer is enabled. // Whether or not the alternate screen buffer is enabled.
altScreen() bool altScreen() bool
// Enable the alternate screen buffer. // Enable the alternate screen buffer.

View File

@ -9,6 +9,20 @@ type WindowSizeMsg struct {
Height int 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 // EnterAltScreen is a special command that tells the Bubble Tea program to
// enter the alternate screen buffer. // enter the alternate screen buffer.
// //

View File

@ -245,6 +245,14 @@ func (r *standardRenderer) repaint() {
r.lastRender = "" 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 { func (r *standardRenderer) altScreen() bool {
return r.altScreenActive return r.altScreenActive
} }

3
tea.go
View File

@ -378,6 +378,9 @@ func (p *Program) StartReturningModel() (Model, error) {
p.shutdown(false) p.shutdown(false)
return model, nil return model, nil
case clearScreenMsg:
p.renderer.clearScreen()
case enterAltScreenMsg: case enterAltScreenMsg:
p.renderer.enterAltScreen() p.renderer.enterAltScreen()