2020-11-04 16:42:44 +03:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
2020-11-18 22:27:41 +03:00
|
|
|
"fmt"
|
2021-07-21 19:04:37 +03:00
|
|
|
"math"
|
2020-11-04 16:42:44 +03:00
|
|
|
"strings"
|
2021-07-08 20:02:52 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-07-21 19:04:37 +03:00
|
|
|
"github.com/charmbracelet/harmonica"
|
2021-07-21 22:00:30 +03:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2020-11-04 16:42:44 +03:00
|
|
|
"github.com/lucasb-eyer/go-colorful"
|
2020-11-18 22:27:41 +03:00
|
|
|
"github.com/muesli/reflow/ansi"
|
2020-11-04 16:42:44 +03:00
|
|
|
"github.com/muesli/termenv"
|
|
|
|
)
|
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
// Internal ID management. Used during animating to assure that frame messages
|
|
|
|
// can only be received by progress components that sent them.
|
|
|
|
var (
|
|
|
|
lastID int
|
|
|
|
idMtx sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return the next ID we should use on the model.
|
|
|
|
func nextID() int {
|
|
|
|
idMtx.Lock()
|
|
|
|
defer idMtx.Unlock()
|
|
|
|
lastID++
|
|
|
|
return lastID
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-07-21 19:04:37 +03:00
|
|
|
fps = 60
|
|
|
|
defaultWidth = 40
|
|
|
|
defaultFrequency = 18.0
|
|
|
|
defaultDamping = 1.0
|
2021-07-08 20:02:52 +03:00
|
|
|
)
|
2020-11-19 02:38:34 +03:00
|
|
|
|
2020-11-19 00:20:00 +03:00
|
|
|
// Option is used to set options in NewModel. For example:
|
|
|
|
//
|
|
|
|
// progress := NewModel(
|
|
|
|
// WithRamp("#ff0000", "#0000ff"),
|
2020-11-19 01:47:58 +03:00
|
|
|
// WithoutPercentage(),
|
2020-11-19 00:20:00 +03:00
|
|
|
// )
|
2021-07-08 20:02:52 +03:00
|
|
|
//
|
2021-07-22 02:24:34 +03:00
|
|
|
type Option func(*Model)
|
2020-11-19 00:20:00 +03:00
|
|
|
|
2020-11-20 18:47:01 +03:00
|
|
|
// WithDefaultGradient sets a gradient fill with default colors.
|
|
|
|
func WithDefaultGradient() Option {
|
2020-12-02 01:01:08 +03:00
|
|
|
return WithGradient("#5A56E0", "#EE6FF8")
|
2020-11-19 00:20:00 +03:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:47:01 +03:00
|
|
|
// WithGradient sets a gradient fill blending between two colors.
|
|
|
|
func WithGradient(colorA, colorB string) Option {
|
2021-07-22 02:24:34 +03:00
|
|
|
return func(m *Model) {
|
|
|
|
m.setRamp(colorA, colorB, false)
|
2020-11-19 00:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:47:01 +03:00
|
|
|
// WithDefaultScaledGradient sets a gradient with default colors, and scales the
|
2020-11-19 02:04:06 +03:00
|
|
|
// gradient to fit the filled portion of the ramp.
|
2020-11-20 18:47:01 +03:00
|
|
|
func WithDefaultScaledGradient() Option {
|
2020-12-02 01:01:08 +03:00
|
|
|
return WithScaledGradient("#5A56E0", "#EE6FF8")
|
2020-11-19 02:04:06 +03:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:47:01 +03:00
|
|
|
// WithScaledGradient scales the gradient to fit the width of the filled portion of
|
2020-11-19 01:47:58 +03:00
|
|
|
// the progress bar.
|
2020-11-20 18:47:01 +03:00
|
|
|
func WithScaledGradient(colorA, colorB string) Option {
|
2021-07-22 02:24:34 +03:00
|
|
|
return func(m *Model) {
|
|
|
|
m.setRamp(colorA, colorB, true)
|
2020-11-19 02:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 02:38:34 +03:00
|
|
|
// WithSolidFill sets the progress to use a solid fill with the given color.
|
2020-11-19 02:04:06 +03:00
|
|
|
func WithSolidFill(color string) Option {
|
2021-07-22 02:24:34 +03:00
|
|
|
return func(m *Model) {
|
2020-11-19 02:04:06 +03:00
|
|
|
m.FullColor = color
|
|
|
|
m.useRamp = false
|
2020-11-19 01:47:58 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2020-11-19 01:47:58 +03:00
|
|
|
// WithoutPercentage hides the numeric percentage.
|
|
|
|
func WithoutPercentage() Option {
|
2021-07-22 02:24:34 +03:00
|
|
|
return func(m *Model) {
|
2020-11-19 01:47:58 +03:00
|
|
|
m.ShowPercentage = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 02:26:30 +03:00
|
|
|
// WithWidth sets the initial width of the progress bar. Note that you can also
|
|
|
|
// set the width via the Width property, which can come in handy if you're
|
|
|
|
// waiting for a tea.WindowSizeMsg.
|
|
|
|
func WithWidth(w int) Option {
|
2021-07-22 02:24:34 +03:00
|
|
|
return func(m *Model) {
|
2020-11-19 02:26:30 +03:00
|
|
|
m.Width = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 01:15:38 +03:00
|
|
|
// WithSpringOptions sets the initial frequency and damping options for the
|
|
|
|
// progressbar's built-in spring-based animation. Frequency corresponds to
|
|
|
|
// speed, and damping to bounciness. For details see:
|
|
|
|
//
|
|
|
|
// https://github.com/charmbracelet/harmonica
|
2021-07-26 19:13:59 +03:00
|
|
|
func WithSpringOptions(frequency, damping float64) Option {
|
|
|
|
return func(m *Model) {
|
|
|
|
m.SetSpringOptions(frequency, damping)
|
|
|
|
m.springCustomized = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 04:04:33 +03:00
|
|
|
// WithColorProfile sets the color profile to use for the progress bar.
|
|
|
|
func WithColorProfile(p termenv.Profile) Option {
|
|
|
|
return func(m *Model) {
|
|
|
|
m.colorProfile = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
// FrameMsg indicates that an animation step should occur.
|
|
|
|
type FrameMsg struct {
|
2021-07-21 19:04:37 +03:00
|
|
|
id int
|
|
|
|
tag int
|
2021-07-08 20:02:52 +03:00
|
|
|
}
|
|
|
|
|
2020-11-19 02:17:27 +03:00
|
|
|
// Model stores values we'll use when rendering the progress bar.
|
2020-11-19 01:47:58 +03:00
|
|
|
type Model struct {
|
2021-07-21 19:04:37 +03:00
|
|
|
// An identifier to keep us from receiving messages intended for other
|
|
|
|
// progress bars.
|
2021-07-08 20:02:52 +03:00
|
|
|
id int
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
// An identifier to keep us from receiving frame messages too quickly.
|
|
|
|
tag int
|
|
|
|
|
2020-11-19 00:20:00 +03:00
|
|
|
// Total width of the progress bar, including percentage, if set.
|
2020-11-05 14:41:53 +03:00
|
|
|
Width int
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
// "Filled" sections of the progress bar.
|
2020-11-19 01:47:58 +03:00
|
|
|
Full rune
|
|
|
|
FullColor string
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
// "Empty" sections of progress bar.
|
2020-11-19 01:47:58 +03:00
|
|
|
Empty rune
|
|
|
|
EmptyColor string
|
2020-11-04 16:42:44 +03:00
|
|
|
|
2021-07-08 20:02:52 +03:00
|
|
|
// Settings for rendering the numeric percentage.
|
2020-11-19 02:49:26 +03:00
|
|
|
ShowPercentage bool
|
|
|
|
PercentFormat string // a fmt string for a float
|
2021-07-21 22:00:30 +03:00
|
|
|
PercentageStyle lipgloss.Style
|
2020-11-18 22:27:41 +03:00
|
|
|
|
2021-08-24 00:15:21 +03:00
|
|
|
// Members for animated transitions.
|
2021-07-26 19:13:59 +03:00
|
|
|
spring harmonica.Spring
|
|
|
|
springCustomized bool
|
2022-03-30 21:24:27 +03:00
|
|
|
percentShown float64 // percent currently displaying
|
|
|
|
targetPercent float64 // percent to which we're animating
|
2021-07-26 19:13:59 +03:00
|
|
|
velocity float64
|
2021-07-08 20:02:52 +03:00
|
|
|
|
|
|
|
// Gradient settings
|
2020-11-19 01:47:58 +03:00
|
|
|
useRamp bool
|
|
|
|
rampColorA colorful.Color
|
|
|
|
rampColorB colorful.Color
|
2020-11-19 00:20:00 +03:00
|
|
|
|
2020-11-19 01:47:58 +03:00
|
|
|
// When true, we scale the gradient to fit the width of the filled section
|
|
|
|
// of the progress bar. When false, the width of the gradient will be set
|
|
|
|
// to the full width of the progress bar.
|
|
|
|
scaleRamp bool
|
2022-06-02 04:04:33 +03:00
|
|
|
|
|
|
|
// Color profile for the progress bar.
|
|
|
|
colorProfile termenv.Profile
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
|
|
|
|
2022-01-11 02:27:33 +03:00
|
|
|
// New returns a model with default values.
|
|
|
|
func New(opts ...Option) Model {
|
2021-07-08 20:02:52 +03:00
|
|
|
m := Model{
|
2021-07-21 19:04:37 +03:00
|
|
|
id: nextID(),
|
|
|
|
Width: defaultWidth,
|
|
|
|
Full: '█',
|
|
|
|
FullColor: "#7571F9",
|
|
|
|
Empty: '░',
|
|
|
|
EmptyColor: "#606060",
|
|
|
|
ShowPercentage: true,
|
|
|
|
PercentFormat: " %3.0f%%",
|
2022-06-02 04:04:33 +03:00
|
|
|
colorProfile: termenv.ColorProfile(),
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
2021-07-26 19:13:59 +03:00
|
|
|
if !m.springCustomized {
|
|
|
|
m.SetSpringOptions(defaultFrequency, defaultDamping)
|
|
|
|
}
|
2021-07-21 19:04:37 +03:00
|
|
|
|
2020-11-19 00:20:00 +03:00
|
|
|
for _, opt := range opts {
|
2021-07-22 02:24:34 +03:00
|
|
|
opt(&m)
|
2020-11-19 00:20:00 +03:00
|
|
|
}
|
2021-07-22 02:24:34 +03:00
|
|
|
return m
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
|
|
|
|
2022-01-11 02:27:33 +03:00
|
|
|
// NewModel returns a model with default values.
|
|
|
|
//
|
|
|
|
// Deprecated. Use New instead.
|
|
|
|
var NewModel = New
|
|
|
|
|
2021-09-07 20:41:00 +03:00
|
|
|
// Init exists satisfy the tea.Model interface.
|
|
|
|
func (m Model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-24 00:15:21 +03:00
|
|
|
// Update is used to animation the progress bar during transitions. Use
|
2021-07-21 19:04:37 +03:00
|
|
|
// SetPercent to create the command you'll need to trigger the animation.
|
|
|
|
//
|
|
|
|
// If you're rendering with ViewAs you won't need this.
|
2021-09-07 20:41:00 +03:00
|
|
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2021-07-08 20:02:52 +03:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case FrameMsg:
|
2021-07-21 19:04:37 +03:00
|
|
|
if msg.id != m.id || msg.tag != m.tag {
|
2021-07-08 20:02:52 +03:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
// If we've more or less reached equilibrium, stop updating.
|
2022-03-30 21:24:27 +03:00
|
|
|
dist := math.Abs(m.percentShown - m.targetPercent)
|
2021-07-21 19:04:37 +03:00
|
|
|
if dist < 0.001 && m.velocity < 0.01 {
|
2021-07-08 20:02:52 +03:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:24:27 +03:00
|
|
|
m.percentShown, m.velocity = m.spring.Update(m.percentShown, m.velocity, m.targetPercent)
|
2021-07-08 20:02:52 +03:00
|
|
|
return m, m.nextFrame()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
// SetSpringOptions sets the frequency and damping for the current spring.
|
|
|
|
// Frequency corresponds to speed, and damping to bounciness. For details see:
|
2022-01-19 01:15:38 +03:00
|
|
|
//
|
|
|
|
// https://github.com/charmbracelet/harmonica
|
2021-07-21 19:04:37 +03:00
|
|
|
func (m *Model) SetSpringOptions(frequency, damping float64) {
|
|
|
|
m.spring = harmonica.NewSpring(harmonica.FPS(fps), frequency, damping)
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:24:27 +03:00
|
|
|
// Percent returns the current visible percentage on the model. This is only
|
2021-07-21 19:04:37 +03:00
|
|
|
// relevant when you're animating the progress bar.
|
|
|
|
//
|
|
|
|
// If you're rendering with ViewAs you won't need this.
|
|
|
|
func (m Model) Percent() float64 {
|
|
|
|
return m.targetPercent
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPercent sets the percentage state of the model as well as a command
|
|
|
|
// necessary for animating the progress bar to this new percentage.
|
|
|
|
//
|
|
|
|
// If you're rendering with ViewAs you won't need this.
|
2021-07-08 20:02:52 +03:00
|
|
|
func (m *Model) SetPercent(p float64) tea.Cmd {
|
2021-07-21 19:04:37 +03:00
|
|
|
m.targetPercent = math.Max(0, math.Min(1, p))
|
|
|
|
m.tag++
|
2021-07-08 20:02:52 +03:00
|
|
|
return m.nextFrame()
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
// IncrPercent increments the percentage by a given amount, returning a command
|
|
|
|
// necessary to animate the progress bar to the new percentage.
|
|
|
|
//
|
|
|
|
// If you're rendering with ViewAs you won't need this.
|
|
|
|
func (m *Model) IncrPercent(v float64) tea.Cmd {
|
|
|
|
return m.SetPercent(m.Percent() + v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecrPercent decrements the percentage by a given amount, returning a command
|
|
|
|
// necessary to animate the progress bar to the new percentage.
|
|
|
|
//
|
|
|
|
// If you're rendering with ViewAs you won't need this.
|
|
|
|
func (m *Model) DecrPercent(v float64) tea.Cmd {
|
|
|
|
return m.SetPercent(m.Percent() - v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// View renders the an animated progress bar in its current state. To render
|
|
|
|
// a static progress bar based on your own calculations use ViewAs instead.
|
|
|
|
func (m Model) View() string {
|
2022-03-30 21:24:27 +03:00
|
|
|
return m.ViewAs(m.percentShown)
|
2021-07-21 19:04:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ViewAs renders the progress bar with a given percentage.
|
|
|
|
func (m Model) ViewAs(percent float64) string {
|
|
|
|
b := strings.Builder{}
|
|
|
|
percentView := m.percentageView(percent)
|
|
|
|
m.barView(&b, percent, ansi.PrintableRuneWidth(percentView))
|
|
|
|
b.WriteString(percentView)
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) nextFrame() tea.Cmd {
|
|
|
|
return tea.Tick(time.Second/time.Duration(fps), func(time.Time) tea.Msg {
|
|
|
|
return FrameMsg{id: m.id, tag: m.tag}
|
2021-07-08 20:02:52 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
func (m Model) barView(b *strings.Builder, percent float64, textWidth int) {
|
2020-11-19 01:47:58 +03:00
|
|
|
var (
|
2021-07-21 19:04:37 +03:00
|
|
|
tw = max(0, m.Width-textWidth) // total width
|
|
|
|
fw = int(math.Round((float64(tw) * percent))) // filled width
|
2020-11-19 01:47:58 +03:00
|
|
|
p float64
|
|
|
|
)
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
fw = max(0, min(tw, fw))
|
|
|
|
|
2020-11-19 01:47:58 +03:00
|
|
|
if m.useRamp {
|
|
|
|
// Gradient fill
|
|
|
|
for i := 0; i < fw; i++ {
|
|
|
|
if m.scaleRamp {
|
|
|
|
p = float64(i) / float64(fw)
|
|
|
|
} else {
|
|
|
|
p = float64(i) / float64(tw)
|
|
|
|
}
|
|
|
|
c := m.rampColorA.BlendLuv(m.rampColorB, p).Hex()
|
|
|
|
b.WriteString(termenv.
|
|
|
|
String(string(m.Full)).
|
2022-06-02 04:04:33 +03:00
|
|
|
Foreground(m.color(c)).
|
2020-11-19 01:47:58 +03:00
|
|
|
String(),
|
|
|
|
)
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
2020-11-19 01:47:58 +03:00
|
|
|
} else {
|
|
|
|
// Solid fill
|
2022-06-02 04:04:33 +03:00
|
|
|
s := termenv.String(string(m.Full)).Foreground(m.color(m.FullColor)).String()
|
2020-11-19 01:47:58 +03:00
|
|
|
b.WriteString(strings.Repeat(s, fw))
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-19 01:47:58 +03:00
|
|
|
// Empty fill
|
2022-06-02 04:04:33 +03:00
|
|
|
e := termenv.String(string(m.Empty)).Foreground(m.color(m.EmptyColor)).String()
|
2021-07-21 19:04:37 +03:00
|
|
|
n := max(0, tw-fw)
|
|
|
|
b.WriteString(strings.Repeat(e, n))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Model) percentageView(percent float64) string {
|
|
|
|
if !m.ShowPercentage {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
percent = math.Max(0, math.Min(1, percent))
|
|
|
|
percentage := fmt.Sprintf(m.PercentFormat, percent*100) //nolint:gomnd
|
2021-07-21 22:00:30 +03:00
|
|
|
percentage = m.PercentageStyle.Inline(true).Render(percentage)
|
2021-07-21 19:04:37 +03:00
|
|
|
return percentage
|
2020-11-04 16:42:44 +03:00
|
|
|
}
|
2020-11-19 02:04:06 +03:00
|
|
|
|
2021-07-22 02:24:34 +03:00
|
|
|
func (m *Model) setRamp(colorA, colorB string, scaled bool) {
|
|
|
|
// In the event of an error colors here will default to black. For
|
|
|
|
// usability's sake, and because such an error is only cosmetic, we're
|
|
|
|
// ignoring the error for sake of usability.
|
|
|
|
a, _ := colorful.Hex(colorA)
|
|
|
|
b, _ := colorful.Hex(colorB)
|
2020-11-19 02:33:45 +03:00
|
|
|
|
2020-11-19 02:04:06 +03:00
|
|
|
m.useRamp = true
|
|
|
|
m.scaleRamp = scaled
|
|
|
|
m.rampColorA = a
|
|
|
|
m.rampColorB = b
|
|
|
|
}
|
2021-07-21 19:04:37 +03:00
|
|
|
|
2022-06-02 04:04:33 +03:00
|
|
|
func (m Model) color(c string) termenv.Color {
|
|
|
|
return m.colorProfile.Color(c)
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:04:37 +03:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|