diff --git a/progress/progress.go b/progress/progress.go index 4b066b9..b4c9129 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -92,7 +92,7 @@ func WithoutPercentage() Option { // waiting for a tea.WindowSizeMsg. func WithWidth(w int) Option { return func(m *Model) { - m.Width = w + m.width = w } } @@ -131,7 +131,7 @@ type Model struct { tag int // Total width of the progress bar, including percentage, if set. - Width int + width int // "Filled" sections of the progress bar. Full rune @@ -171,7 +171,7 @@ type Model struct { func New(opts ...Option) Model { m := Model{ id: nextID(), - Width: defaultWidth, + width: defaultWidth, Full: '█', FullColor: "#7571F9", Empty: '░', @@ -278,6 +278,16 @@ func (m Model) ViewAs(percent float64) string { return b.String() } +// SetWidth sets the width of the progress bar. +func (m *Model) SetWidth(w int) { + m.width = w +} + +// Width returns the width of the progress bar. +func (m Model) Width() int { + return m.width +} + 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} @@ -286,7 +296,7 @@ func (m *Model) nextFrame() tea.Cmd { func (m Model) barView(b *strings.Builder, percent float64, textWidth int) { var ( - tw = max(0, m.Width-textWidth) // total width + tw = max(0, m.width-textWidth) // total width fw = int(math.Round((float64(tw) * percent))) // filled width p float64 ) diff --git a/progress/progress_test.go b/progress/progress_test.go index 7af5ef1..a4ba720 100644 --- a/progress/progress_test.go +++ b/progress/progress_test.go @@ -43,7 +43,7 @@ func TestGradient(t *testing.T) { expLast := strings.Split(sb.String(), AnsiReset)[0] for _, width := range []int{3, 5, 50} { - p.Width = width + p.SetWidth(width) res := p.ViewAs(1.0) // extract colors from the progrss bar by splitting at p.Full+AnsiReset, leaving us with just the color sequences diff --git a/spinner/spinner.go b/spinner/spinner.go index 6e75540..3ea915d 100644 --- a/spinner/spinner.go +++ b/spinner/spinner.go @@ -195,14 +195,14 @@ func (m Model) tick(id, tag int) tea.Cmd { // spinner := New(WithSpinner(Dot)) type Option func(*Model) -// WithSpinner is an option to set the spinner. +// WithSpinner is an option to set the spinner. Pass this to [Spinner.New]. func WithSpinner(spinner Spinner) Option { return func(m *Model) { m.Spinner = spinner } } -// WithStyle is an option to set the spinner style. +// WithStyle is an option to set the spinner style. Pass this to [Spinner.New]. func WithStyle(style lipgloss.Style) Option { return func(m *Model) { m.Style = style