mirror of
				https://github.com/Maks1mS/bubbles.git
				synced 2025-10-31 13:51:23 +03:00 
			
		
		
		
	Set ramp in NewModel via functional argument
This commit is contained in:
		
				
					committed by
					
						 Christian Rocha
						Christian Rocha
					
				
			
			
				
	
			
			
			
						parent
						
							d81e5713d4
						
					
				
				
					commit
					1e16eca939
				
			| @@ -11,20 +11,43 @@ import ( | ||||
|  | ||||
| var color func(string) termenv.Color = termenv.ColorProfile().Color | ||||
|  | ||||
| // Option is used to set options in NewModel. For example: | ||||
| // | ||||
| //     progress := NewModel( | ||||
| //	       WithRamp("#ff0000", "#0000ff"), | ||||
| //     ) | ||||
| type Option func(*Model) | ||||
|  | ||||
| // WithDefaultRamp sets a gradient fill with default colors. | ||||
| func WithDefaultRamp() Option { | ||||
| 	return WithRamp("#00dbde", "#fc00ff") | ||||
| } | ||||
|  | ||||
| // WithRamp sets a gradient fill blending between two colors. | ||||
| func WithRamp(colorA, colorB string) Option { | ||||
| 	a, _ := colorful.Hex(colorA) | ||||
| 	b, _ := colorful.Hex(colorB) | ||||
| 	return func(m *Model) { | ||||
| 		m.useRamp = true | ||||
| 		m.colorA = a | ||||
| 		m.colorB = b | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type Model struct { | ||||
| 	// Left side color of progress bar. By default, it's #00dbde | ||||
| 	StartColor colorful.Color | ||||
| 	colorA colorful.Color | ||||
|  | ||||
| 	// Left side color of progress bar. By default, it's #fc00ff | ||||
| 	EndColor colorful.Color | ||||
| 	colorB colorful.Color | ||||
|  | ||||
| 	// Width of progress bar in symbols. | ||||
| 	// Total width of the progress bar, including percentage, if set. | ||||
| 	Width int | ||||
|  | ||||
| 	// filament rune of done part of progress bar. it's █ by default | ||||
| 	// Rune for "filled" sections of the progress bar. | ||||
| 	Full rune | ||||
|  | ||||
| 	// empty rune of pending part of progress bar. it's ░ by default | ||||
| 	// Rune for "empty" sections of progress bar. | ||||
| 	Empty rune | ||||
|  | ||||
| 	// if true, gradient will be setted from start to end of filled part. Instead, it'll work | ||||
| @@ -33,21 +56,25 @@ type Model struct { | ||||
|  | ||||
| 	ShowPercent   bool | ||||
| 	PercentFormat string | ||||
|  | ||||
| 	useRamp bool | ||||
| } | ||||
|  | ||||
| // NewModel returns a model with default values. | ||||
| func NewModel() Model { | ||||
| 	startColor, _ := colorful.Hex("#00dbde") | ||||
| 	endColor, _ := colorful.Hex("#fc00ff") | ||||
| 	return Model{ | ||||
| 		StartColor:    startColor, | ||||
| 		EndColor:      endColor, | ||||
| func NewModel(opts ...Option) *Model { | ||||
| 	m := &Model{ | ||||
| 		Width:         40, | ||||
| 		Full:          '█', | ||||
| 		Empty:         '░', | ||||
| 		ShowPercent:   true, | ||||
| 		PercentFormat: " %3.0f%%", | ||||
| 	} | ||||
|  | ||||
| 	for _, opt := range opts { | ||||
| 		opt(m) | ||||
| 	} | ||||
|  | ||||
| 	return m | ||||
| } | ||||
|  | ||||
| func (m Model) View(percent float64) string { | ||||
| @@ -69,7 +96,7 @@ func (m Model) bar(percent float64, textWidth int) string { | ||||
| 			gradientPart = float64(len(ramp)) | ||||
| 		} | ||||
| 		percent := float64(i) / gradientPart | ||||
| 		c := m.StartColor.BlendLuv(m.EndColor, percent) | ||||
| 		c := m.colorA.BlendLuv(m.colorB, percent) | ||||
| 		ramp[i] = c.Hex() | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user