Skip to content

Commit 1e4dd55

Browse files
authored
powerline: simplify some logic and make it readable (#6)
1 parent 31e33dd commit 1e4dd55

11 files changed

+104
-105
lines changed

powerline/powerline.go

+49-51
Original file line numberDiff line numberDiff line change
@@ -97,35 +97,19 @@ func (p *Powerline) autoAdjustNumSegments(maxWidth int) (int, int, int) {
9797
return len(p.left), len(p.right), 0
9898
}
9999

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
104104
break
105105
}
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)
116108
} 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)
126110
}
127111
}
128-
return l, r, maxWidth - currWidth
112+
return idxLeft, idxRight, maxWidth - usedWidth
129113
}
130114

131115
func (p *Powerline) hasChangesLeft() bool {
@@ -167,27 +151,15 @@ func (p *Powerline) renderLeft(maxWidth, nsLeft, nsRight, paddingSpace int) stri
167151
// inject all the segments now
168152
sep := p.style.SeparatorLeft
169153
for idx, segment := range segments {
154+
// segment: set up margin and append
170155
if idx == 0 {
171156
segment.setPaddingLeft(p.style.MarginLeft)
172157
}
173-
174-
// segment
175158
left = append(left, segment.Render())
176159

177160
// separator
178161
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)
191163
left = append(left, sepColor.Sprint(sep))
192164
}
193165
}
@@ -220,28 +192,54 @@ func (p *Powerline) renderRight(maxWidth, nsLeft, nsRight, paddingSpace int) str
220192
for idx, segment := range segments {
221193
// separator
222194
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)
233196
right = append(right, sepColor.Sprint(sep))
234197
}
235198

236-
// set up the margin
199+
// segment: set up the margin and append
237200
if idx == len(segments)-1 {
238201
segment.setPaddingLeft(p.style.MarginLeft)
239202
}
240-
241-
// segment
242203
right = append(right, segment.Render())
243204
}
244205

245206
p.rightRendered = strings.Join(right, "")
246207
return p.rightRendered
247208
}
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+
}

powerline/powerline_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
testIP = "0.0.0.0"
1414
)
1515

16-
func BenchmarkPowerline_Render(b *testing.B) {
16+
func BenchmarkPowerlineRender(b *testing.B) {
1717
segHostname := &Segment{}
1818
segHostname.SetContent("hostname")
1919
segHostname.SetColor(prompt.Color{Foreground: termenv.ANSI256Color(7), Background: termenv.ANSI256Color(23)})

0 commit comments

Comments
 (0)