fix(progress): last gradient color off by one (#338)

* fix(progress): add failing test

The progress bar component does not render the gradient 100% correctly:
The very last color that appears in the rendered progress bar should be
the second color defined in the gradient but it is not, due to an
off-by-one one error.

This test case shows this. The next commit will contain the fix.

* fix(progress): use the second gradient color as the last char of the bar

Due to an off-by-one error, the very last rendered color+char was never
exactly the second specified gradient color. The fixed code looks less
elegant unfortunately, but now the last color is the desired one.
This commit is contained in:
Ben Dimbeck
2023-03-24 10:32:43 -07:00
committed by GitHub
parent 3a1a24e211
commit 876d5af152
2 changed files with 74 additions and 3 deletions
+8 -3
View File
@@ -299,10 +299,15 @@ func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
if m.useRamp {
// Gradient fill
for i := 0; i < fw; i++ {
if m.scaleRamp {
p = float64(i) / float64(fw)
if fw == 1 {
// this is up for debate: in a gradient of width=1, should the
// single character rendered be the first color, the last color
// or exactly 50% inbetween? I opted for 50%
p = 0.5
} else if m.scaleRamp {
p = float64(i) / float64(fw-1)
} else {
p = float64(i) / float64(tw)
p = float64(i) / float64(tw-1)
}
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
b.WriteString(termenv.