mirror of
				https://github.com/Maks1mS/bubbles.git
				synced 2025-11-04 07:31:23 +03:00 
			
		
		
		
	Add option to show percent
This commit is contained in:
		
				
					committed by
					
						
						Christian Rocha
					
				
			
			
				
	
			
			
			
						parent
						
							a2e0a2e72e
						
					
				
				
					commit
					b10dbcb4dd
				
			@@ -1,9 +1,11 @@
 | 
				
			|||||||
package progress
 | 
					package progress
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/lucasb-eyer/go-colorful"
 | 
						"github.com/lucasb-eyer/go-colorful"
 | 
				
			||||||
 | 
						"github.com/muesli/reflow/ansi"
 | 
				
			||||||
	"github.com/muesli/termenv"
 | 
						"github.com/muesli/termenv"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -18,14 +20,17 @@ type Model struct {
 | 
				
			|||||||
	Width int
 | 
						Width int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// filament rune of done part of progress bar. it's █ by default
 | 
						// filament rune of done part of progress bar. it's █ by default
 | 
				
			||||||
	FilamentSymbol rune
 | 
						Full rune
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// empty rune of pending part of progress bar. it's ░ by default
 | 
						// empty rune of pending part of progress bar. it's ░ by default
 | 
				
			||||||
	EmptySymbol rune
 | 
						Empty rune
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// if true, gradient will be setted from start to end of filled part. Instead, it'll work
 | 
						// if true, gradient will be setted from start to end of filled part. Instead, it'll work
 | 
				
			||||||
	// on all proggress bar length
 | 
						// on all proggress bar length
 | 
				
			||||||
	FullGradientMode bool
 | 
						FullGradientMode bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ShowPercent   bool
 | 
				
			||||||
 | 
						PercentFormat string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewModel returns a model with default values.
 | 
					// NewModel returns a model with default values.
 | 
				
			||||||
@@ -33,18 +38,31 @@ func NewModel() Model {
 | 
				
			|||||||
	startColor, _ := colorful.Hex("#00dbde")
 | 
						startColor, _ := colorful.Hex("#00dbde")
 | 
				
			||||||
	endColor, _ := colorful.Hex("#fc00ff")
 | 
						endColor, _ := colorful.Hex("#fc00ff")
 | 
				
			||||||
	return Model{
 | 
						return Model{
 | 
				
			||||||
		StartColor:     startColor,
 | 
							StartColor:    startColor,
 | 
				
			||||||
		EndColor:       endColor,
 | 
							EndColor:      endColor,
 | 
				
			||||||
		Width:          40,
 | 
							Width:         40,
 | 
				
			||||||
		FilamentSymbol: '█',
 | 
							Full:          '█',
 | 
				
			||||||
		EmptySymbol:    '░',
 | 
							Empty:         '░',
 | 
				
			||||||
 | 
							ShowPercent:   true,
 | 
				
			||||||
 | 
							PercentFormat: " %3.0f%%",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *Model) View(percent float64) string {
 | 
					func (m Model) View(percent float64) string {
 | 
				
			||||||
	ramp := make([]string, int(float64(m.Width)*percent))
 | 
						if m.ShowPercent {
 | 
				
			||||||
 | 
							s := fmt.Sprintf(m.PercentFormat, percent*100)
 | 
				
			||||||
 | 
							w := ansi.PrintableRuneWidth(s)
 | 
				
			||||||
 | 
							return m.bar(percent, w) + s
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return m.bar(percent, 0)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m Model) bar(percent float64, textWidth int) string {
 | 
				
			||||||
 | 
						w := m.Width - textWidth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ramp := make([]string, int(float64(w)*percent))
 | 
				
			||||||
	for i := 0; i < len(ramp); i++ {
 | 
						for i := 0; i < len(ramp); i++ {
 | 
				
			||||||
		gradientPart := float64(m.Width)
 | 
							gradientPart := float64(w)
 | 
				
			||||||
		if m.FullGradientMode {
 | 
							if m.FullGradientMode {
 | 
				
			||||||
			gradientPart = float64(len(ramp))
 | 
								gradientPart = float64(len(ramp))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -55,9 +73,9 @@ func (m *Model) View(percent float64) string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var fullCells string
 | 
						var fullCells string
 | 
				
			||||||
	for i := 0; i < len(ramp); i++ {
 | 
						for i := 0; i < len(ramp); i++ {
 | 
				
			||||||
		fullCells += termenv.String(string(m.FilamentSymbol)).Foreground(termenv.ColorProfile().Color(ramp[i])).String()
 | 
							fullCells += termenv.String(string(m.Full)).Foreground(termenv.ColorProfile().Color(ramp[i])).String()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fullCells += strings.Repeat(string(m.EmptySymbol), m.Width-len(ramp))
 | 
						fullCells += strings.Repeat(string(m.Empty), w-len(ramp))
 | 
				
			||||||
	return fullCells
 | 
						return fullCells
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user