Skip to content
Myr

fix: don't panic in ProgressBarState.String() for out-of-range values (#1748)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Ankit · committed by GitHub · parent faf4dcf · Browse files · 2 files+36 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: drew <me@andrinoff.com>
  • Signed-off-by: drew <me@andrinoff.com>

Diff stats

2 files+36 added−8 removed

  • other:+36 added−8 removed

2 files+36 added−8 removed

modified:tea_test.go+20 added
667 unmodified lines
668669 _ = r.CloseWithError(io.EOF)670 }671}672673// TestProgressBarStateStringOutOfRange is a regression test for674// https://github.com/charmbracelet/bubbletea/issues/1711: String panicked675// with an index-out-of-range error for any ProgressBarState outside the676// [ProgressBarNone, ProgressBarWarning] range. Since State is an exported677// field on ProgressBar, callers can assign such a value without going678// through NewProgressBar.679func TestProgressBarStateStringOutOfRange(t *testing.T) {680 for _, s := range []ProgressBarState{-1, 5, 100} {681 if got, want := s.String(), "Unknown"; got != want {682 t.Errorf("ProgressBarState(%d).String() = %q, want %q", int(s), got, want)683 }684 }685686 for s := ProgressBarNone; s <= ProgressBarWarning; s++ {687 if got := s.String(); got == "Unknown" {688 t.Errorf("ProgressBarState(%d).String() = %q, want a known name", int(s), got)689 }690 }691}
modified:tea.go+16 added-8 removed
315 unmodified lines
316 ProgressBarIndeterminate317 ProgressBarWarning318)319320// String return a human-readable value for the given [ProgressBarState].320// String returns a human-readable name for the given [ProgressBarState].321// Values outside the known range return "Unknown".322func (s ProgressBarState) String() string {322 return [...]string{323 "None",324 "Default",325 "Error",326 "Indeterminate",327 "Warning",328 }[s]323 switch s {324 case ProgressBarNone:325 return "None"326 case ProgressBarDefault:327 return "Default"328 case ProgressBarError:329 return "Error"330 case ProgressBarIndeterminate:331 return "Indeterminate"332 case ProgressBarWarning:333 return "Warning"334 default:335 return "Unknown"336 }337}338339// ProgressBar represents the terminal progress bar.340//
1107 unmodified lines