1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package pterm
import (
"io"
"strings"
"atomicgo.dev/cursor"
"github.com/pterm/pterm/internal"
)
// DefaultArea is the default area printer.
var DefaultArea = AreaPrinter{}
// AreaPrinter prints an area which can be updated easily.
// use this printer for live output like charts, algorithm visualizations, simulations and even games.
type AreaPrinter struct {
RemoveWhenDone bool
Fullscreen bool
Center bool
content string
isActive bool
area *cursor.Area
}
// GetContent returns the current area content.
func (p *AreaPrinter) GetContent() string {
return p.content
}
// WithRemoveWhenDone removes the AreaPrinter content after it is stopped.
func (p AreaPrinter) WithRemoveWhenDone(b ...bool) *AreaPrinter {
p.RemoveWhenDone = internal.WithBoolean(b)
return &p
}
// WithFullscreen sets the AreaPrinter height the same height as the terminal, making it fullscreen.
func (p AreaPrinter) WithFullscreen(b ...bool) *AreaPrinter {
p.Fullscreen = internal.WithBoolean(b)
return &p
}
// WithCenter centers the AreaPrinter content to the terminal.
func (p AreaPrinter) WithCenter(b ...bool) *AreaPrinter {
p.Center = internal.WithBoolean(b)
return &p
}
// SetWriter sets the writer for the AreaPrinter.
func (p *AreaPrinter) SetWriter(writer io.Writer) {
}
// Update overwrites the content of the AreaPrinter.
// Can be used live.
func (p *AreaPrinter) Update(text ...interface{}) {
if p.area == nil {
newArea := cursor.NewArea()
p.area = &newArea
}
str := Sprint(text...)
p.content = str
if p.Center {
str = DefaultCenter.Sprint(str)
}
if p.Fullscreen {
str = strings.TrimRight(str, "\n")
height := GetTerminalHeight()
contentHeight := strings.Count(str, "\n")
topPadding := 0
bottomPadding := height - contentHeight - 2
if p.Center {
topPadding = (bottomPadding / 2) + 1
bottomPadding /= 2
}
if height > contentHeight {
str = strings.Repeat("\n", topPadding) + str
str += strings.Repeat("\n", bottomPadding)
}
}
p.area.Update(str)
}
// Start the AreaPrinter.
func (p *AreaPrinter) Start(text ...interface{}) (*AreaPrinter, error) {
p.isActive = true
str := Sprint(text...)
newArea := cursor.NewArea()
p.area = &newArea
p.Update(str)
return p, nil
}
// Stop terminates the AreaPrinter immediately.
// The AreaPrinter will not resolve into anything.
func (p *AreaPrinter) Stop() error {
if !p.isActive {
return nil
}
p.isActive = false
if p.RemoveWhenDone {
p.Clear()
}
return nil
}
// GenericStart runs Start, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Start instead of this in your program.
func (p *AreaPrinter) GenericStart() (*LivePrinter, error) {
_, _ = p.Start()
lp := LivePrinter(p)
return &lp, nil
}
// GenericStop runs Stop, but returns a LivePrinter.
// This is used for the interface LivePrinter.
// You most likely want to use Stop instead of this in your program.
func (p *AreaPrinter) GenericStop() (*LivePrinter, error) {
_ = p.Stop()
lp := LivePrinter(p)
return &lp, nil
}
// Clear is a Wrapper function that clears the content of the Area
// moves the cursor to the bottom of the terminal, clears n lines upwards from
// the current position and moves the cursor again.
func (p *AreaPrinter) Clear() {
p.area.Clear()
}
|