@@ -97,35 +97,19 @@ func (p *Powerline) autoAdjustNumSegments(maxWidth int) (int, int, int) {
97
97
return len (p .left ), len (p .right ), 0
98
98
}
99
99
100
- l , r := 0 , 0
101
- currWidth , handledRight := 0 , true
102
- for {
103
- if l == len (p .left ) && r == len (p .right ) { // appended everything
100
+ idxLeft , idxRight := 0 , 0
101
+ isDone , usedWidth := false , 0
102
+ for idx := 0 ; ! isDone && idx < 1024 ; /* 512 segments per side */ idx ++ {
103
+ if idxLeft == len (p .left ) && idxRight == len (p .right ) { // appended everything
104
104
break
105
105
}
106
- if handledRight { // append a left segment
107
- if l < len (p .left ) {
108
- addlWidth := p .left [l ].Width ()
109
- if currWidth + addlWidth > maxWidth {
110
- break
111
- }
112
- currWidth += addlWidth
113
- l ++
114
- }
115
- handledRight = false
106
+ if idx % 2 == 0 { // append a left segment
107
+ idxLeft , usedWidth , isDone = appendSegmentIfUnderWidth (p .left , idxLeft , usedWidth , maxWidth )
116
108
} else { // append a right segment
117
- if r < len (p .right ) {
118
- addlWidth := p .right [r ].Width ()
119
- if currWidth + addlWidth > maxWidth {
120
- break
121
- }
122
- currWidth += addlWidth
123
- r ++
124
- }
125
- handledRight = true
109
+ idxRight , usedWidth , isDone = appendSegmentIfUnderWidth (p .right , idxRight , usedWidth , maxWidth )
126
110
}
127
111
}
128
- return l , r , maxWidth - currWidth
112
+ return idxLeft , idxRight , maxWidth - usedWidth
129
113
}
130
114
131
115
func (p * Powerline ) hasChangesLeft () bool {
@@ -167,27 +151,15 @@ func (p *Powerline) renderLeft(maxWidth, nsLeft, nsRight, paddingSpace int) stri
167
151
// inject all the segments now
168
152
sep := p .style .SeparatorLeft
169
153
for idx , segment := range segments {
154
+ // segment: set up margin and append
170
155
if idx == 0 {
171
156
segment .setPaddingLeft (p .style .MarginLeft )
172
157
}
173
-
174
- // segment
175
158
left = append (left , segment .Render ())
176
159
177
160
// separator
178
161
if len (sep ) > 0 {
179
- sepColor := prompt.Color {
180
- Background : p .style .Color .Background ,
181
- Foreground : segment .Color ().Background ,
182
- }
183
- if idx < len (segments )- 1 {
184
- sepColor .Background = segments [idx + 1 ].Color ().Background
185
- } else if idx == len (segments )- 1 {
186
- sepColor .Background = p .style .Color .Background
187
- }
188
- if p .style .InvertSeparatorColors {
189
- sepColor = sepColor .Invert ()
190
- }
162
+ sepColor := p .separatorColor (segments , idx , true )
191
163
left = append (left , sepColor .Sprint (sep ))
192
164
}
193
165
}
@@ -220,28 +192,54 @@ func (p *Powerline) renderRight(maxWidth, nsLeft, nsRight, paddingSpace int) str
220
192
for idx , segment := range segments {
221
193
// separator
222
194
if len (sep ) > 0 {
223
- sepColor := prompt.Color {
224
- Foreground : segment .Color ().Background ,
225
- Background : p .style .Color .Background ,
226
- }
227
- if idx > 0 {
228
- sepColor .Background = segments [idx - 1 ].Color ().Background
229
- }
230
- if p .style .InvertSeparatorColors {
231
- sepColor = sepColor .Invert ()
232
- }
195
+ sepColor := p .separatorColor (segments , idx , false )
233
196
right = append (right , sepColor .Sprint (sep ))
234
197
}
235
198
236
- // set up the margin
199
+ // segment: set up the margin and append
237
200
if idx == len (segments )- 1 {
238
201
segment .setPaddingLeft (p .style .MarginLeft )
239
202
}
240
-
241
- // segment
242
203
right = append (right , segment .Render ())
243
204
}
244
205
245
206
p .rightRendered = strings .Join (right , "" )
246
207
return p .rightRendered
247
208
}
209
+
210
+ func appendSegmentIfUnderWidth (segments []* Segment , idx int , usedWidth int , maxWidth int ) (int , int , bool ) {
211
+ if idx < len (segments ) {
212
+ addlWidth := segments [idx ].Width ()
213
+ if usedWidth + addlWidth > maxWidth {
214
+ return idx , usedWidth , true
215
+ }
216
+ usedWidth += addlWidth
217
+ idx ++
218
+ }
219
+ return idx , usedWidth , false
220
+ }
221
+
222
+ func (p * Powerline ) separatorColor (segments []* Segment , idx int , leftSide bool ) prompt.Color {
223
+ c := prompt.Color {
224
+ Background : p .style .Color .Background ,
225
+ Foreground : segments [idx ].Color ().Background ,
226
+ }
227
+
228
+ if leftSide {
229
+ if idx + 1 < len (segments ) {
230
+ c .Background = segments [idx + 1 ].Color ().Background
231
+ } else if idx == len (segments )- 1 { // last segment, use powerline's background
232
+ c .Background = p .style .Color .Background
233
+ }
234
+ } else { // right side
235
+ if idx > 0 {
236
+ c .Background = segments [idx - 1 ].Color ().Background
237
+ }
238
+ }
239
+
240
+ if p .style .InvertSeparatorColors {
241
+ c = c .Invert ()
242
+ }
243
+
244
+ return c
245
+ }
0 commit comments