forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharea_printer.go
181 lines (152 loc) · 4.38 KB
/
area_printer.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package pterm
import (
"strings"
"atomicgo.dev/cursor"
"github.com/forvitinn/pterm/internal"
"github.com/jroimartin/gocui"
)
// 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
}
// 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 {
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
}
//not in use & not tested
//WIP
func (area AreaPrinter) GHandleManagers(kd GMapView2KeyDesc, managers ...gocui.Manager) error {
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
return err
}
// defer the showing of pterm ui
defer func() {
g.Close()
}()
g.SetManager(managers...)
// set all keybindings on each view
for view, kd_instance := range kd {
if err := g.SetKeybinding(view, kd_instance.Key, kd_instance.Mod, kd_instance.KeyFunc); err != nil {
return nil
}
}
// start mainloop
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
return err
}
return nil
}
// Give control over UI over to Gocui from pterm.area
// This function must be provided with a gocui layout and a Map which maps Layout Views(Strings) to Keybins and their functions
func (p *AreaPrinter) GHandleFunc(kd GMapView2KeyDesc, glay GLayout) error {
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
return err
}
defer g.Close()
g.SetManagerFunc(glay)
// set all keybindings on each view
for view, kd_instance := range kd {
if err := g.SetKeybinding(view, kd_instance.Key, kd_instance.Mod, kd_instance.KeyFunc); err != nil {
return nil
}
}
// start mainloop
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
return err
}
return nil
}
// 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()
}