/
engine.go
261 lines (240 loc) · 7.27 KB
/
engine.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"sync"
"time"
)
type engine struct {
settings *Settings
env environmentInfo
color *AnsiColor
renderer *AnsiRenderer
consoleTitle *consoleTitle
activeBlock *Block
activeSegment *Segment
previousActiveSegment *Segment
rprompt string
}
// SegmentTiming holds the timing context for a segment
type SegmentTiming struct {
name string
nameLength int
enabled bool
stringValue string
enabledDuration time.Duration
stringDuration time.Duration
}
func (e *engine) getPowerlineColor(foreground bool) string {
if e.previousActiveSegment == nil {
return Transparent
}
if !foreground && e.activeSegment.Style != Powerline {
return Transparent
}
if foreground && e.previousActiveSegment.Style != Powerline {
return Transparent
}
return e.previousActiveSegment.background()
}
func (e *engine) writePowerLineSeparator(background, foreground string, end bool) {
symbol := e.activeSegment.PowerlineSymbol
if end {
symbol = e.previousActiveSegment.PowerlineSymbol
}
if e.activeSegment.InvertPowerline {
e.color.write(foreground, background, symbol)
return
}
e.color.write(background, foreground, symbol)
}
func (e *engine) endPowerline() {
if e.activeSegment != nil &&
e.activeSegment.Style != Powerline &&
e.previousActiveSegment != nil &&
e.previousActiveSegment.Style == Powerline {
e.writePowerLineSeparator(e.getPowerlineColor(false), e.previousActiveSegment.background(), true)
}
}
func (e *engine) renderPowerLineSegment(text string) {
e.writePowerLineSeparator(e.activeSegment.background(), e.getPowerlineColor(true), false)
e.renderText(text)
}
func (e *engine) renderPlainSegment(text string) {
e.renderText(text)
}
func (e *engine) renderDiamondSegment(text string) {
e.color.write(Transparent, e.activeSegment.background(), e.activeSegment.LeadingDiamond)
e.renderText(text)
e.color.write(Transparent, e.activeSegment.background(), e.activeSegment.TrailingDiamond)
}
func (e *engine) renderText(text string) {
defaultValue := " "
if e.activeSegment.background() != "" {
defaultValue = fmt.Sprintf("<%s>\u2588</>", e.activeSegment.background())
}
text = e.color.formats.generateHyperlink(text)
prefix := e.activeSegment.getValue(Prefix, defaultValue)
postfix := e.activeSegment.getValue(Postfix, defaultValue)
e.color.write(e.activeSegment.background(), e.activeSegment.foreground(), fmt.Sprintf("%s%s%s", prefix, text, postfix))
}
func (e *engine) renderSegmentText(text string) {
switch e.activeSegment.Style {
case Plain:
e.renderPlainSegment(text)
case Diamond:
e.renderDiamondSegment(text)
case Powerline:
e.renderPowerLineSegment(text)
}
e.previousActiveSegment = e.activeSegment
}
func (e *engine) renderBlockSegments(block *Block) string {
defer e.resetBlock()
e.activeBlock = block
e.setStringValues(block.Segments)
for _, segment := range block.Segments {
if !segment.active {
continue
}
e.activeSegment = segment
e.endPowerline()
e.renderSegmentText(segment.stringValue)
}
if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline {
e.writePowerLineSeparator(Transparent, e.previousActiveSegment.background(), true)
}
return e.color.string()
}
func (e *engine) setStringValues(segments []*Segment) {
wg := sync.WaitGroup{}
wg.Add(len(segments))
defer wg.Wait()
cwd := e.env.getcwd()
for _, segment := range segments {
go func(s *Segment) {
defer wg.Done()
s.setStringValue(e.env, cwd)
}(segment)
}
}
func (e *engine) render() {
for _, block := range e.settings.Blocks {
// if line break, append a line break
switch block.Type {
case LineBreak:
e.renderer.write("\n")
case Prompt:
if block.VerticalOffset != 0 {
e.renderer.changeLine(block.VerticalOffset)
}
switch block.Alignment {
case Right:
e.renderer.carriageForward()
blockText := e.renderBlockSegments(block)
e.renderer.setCursorForRightWrite(blockText, block.HorizontalOffset)
e.renderer.write(blockText)
case Left:
e.renderer.write(e.renderBlockSegments(block))
}
case RPrompt:
e.rprompt = e.renderBlockSegments(block)
}
}
if e.settings.ConsoleTitle {
e.renderer.write(e.consoleTitle.getConsoleTitle())
}
e.renderer.creset()
if e.settings.FinalSpace {
e.renderer.write(" ")
}
e.renderer.osc99(e.env.getcwd())
e.print()
}
// debug will loop through your config file and output the timings for each segments
func (e *engine) debug() {
var segmentTimings []SegmentTiming
largestSegmentNameLength := 0
e.renderer.write("\n\x1b[1mHere are the timings of segments in your prompt:\x1b[0m\n\n")
// console title timing
start := time.Now()
consoleTitle := e.consoleTitle.getTemplateText()
duration := time.Since(start)
segmentTiming := SegmentTiming{
name: "ConsoleTitle",
nameLength: 12,
enabled: e.settings.ConsoleTitle,
stringValue: consoleTitle,
enabledDuration: 0,
stringDuration: duration,
}
segmentTimings = append(segmentTimings, segmentTiming)
// loop each segments of each blocks
for _, block := range e.settings.Blocks {
for _, segment := range block.Segments {
err := segment.mapSegmentWithWriter(e.env)
if err != nil || segment.shouldIgnoreFolder(e.env.getcwd()) {
continue
}
var segmentTiming SegmentTiming
segmentTiming.name = string(segment.Type)
segmentTiming.nameLength = len(segmentTiming.name)
if segmentTiming.nameLength > largestSegmentNameLength {
largestSegmentNameLength = segmentTiming.nameLength
}
// enabled() timing
start := time.Now()
segmentTiming.enabled = segment.enabled()
segmentTiming.enabledDuration = time.Since(start)
// string() timing
if segmentTiming.enabled {
start = time.Now()
segmentTiming.stringValue = segment.string()
segmentTiming.stringDuration = time.Since(start)
e.previousActiveSegment = nil
e.activeSegment = segment
e.renderSegmentText(segmentTiming.stringValue)
if e.activeSegment.Style == Powerline {
e.writePowerLineSeparator(Transparent, e.activeSegment.background(), true)
}
segmentTiming.stringValue = e.color.string()
e.color.builder.Reset()
}
segmentTimings = append(segmentTimings, segmentTiming)
}
}
// pad the output so the tabs render correctly
largestSegmentNameLength += 7
for _, segment := range segmentTimings {
duration := segment.enabledDuration.Milliseconds()
if segment.enabled {
duration += segment.stringDuration.Milliseconds()
}
segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.enabled)
e.renderer.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.stringValue))
}
fmt.Print(e.renderer.string())
}
func (e *engine) print() {
switch e.env.getShellName() {
case zsh:
if *e.env.getArgs().Eval {
fmt.Printf("PS1=\"%s\"", e.renderer.string())
fmt.Printf("\nRPROMPT=\"%s\"", e.rprompt)
return
}
case pwsh, powershell5, bash:
if e.rprompt != "" {
e.renderer.saveCursorPosition()
e.renderer.carriageForward()
e.renderer.setCursorForRightWrite(e.rprompt, 0)
e.renderer.write(e.rprompt)
e.renderer.restoreCursorPosition()
}
}
fmt.Print(e.renderer.string())
}
func (e *engine) resetBlock() {
e.color.reset()
e.previousActiveSegment = nil
e.activeBlock = nil
}