Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bar.Finish() cause panic: runtime error: index out of range [-1] #212

Closed
baylesa-dev opened this issue Jan 22, 2025 · 2 comments · Fixed by #214
Closed

bar.Finish() cause panic: runtime error: index out of range [-1] #212

baylesa-dev opened this issue Jan 22, 2025 · 2 comments · Fixed by #214

Comments

@baylesa-dev
Copy link

baylesa-dev commented Jan 22, 2025

In my project, running bar.Finish() cause runtime error: index out of range [-1]. Any idea why ? 😬

goroutine 1 [running]:
github.com/schollz/progressbar/v3.(*ProgressBar).render(0xc0012d5688)
        /app/go/pkg/mod/github.com/schollz/progressbar/[email protected]/progressbar.go:942 +0x606
github.com/schollz/progressbar/v3.(*ProgressBar).Add64(0xc0012d5688, 0x0)
        /app/go/pkg/mod/github.com/schollz/progressbar/[email protected]/progressbar.go:729 +0x487
github.com/schollz/progressbar/v3.(*ProgressBar).Add(...)
        /app/go/pkg/mod/github.com/schollz/progressbar/[email protected]/progressbar.go:655
github.com/schollz/progressbar/v3.(*ProgressBar).Finish(0xc0012d5688)
        /app/go/pkg/mod/github.com/schollz/progressbar/[email protected]/progressbar.go:638 +0xa5
gitlab.com/baylesa/health-map-db/internal/importer/services.ImportAnnuaireSante(0xc00110fbc0, 0xc000c48b60, {0x62af8a0, 0xc000c27890})
        /app/Developer/health-map-db/internal/importer/services/import.go:88 +0x5ba
main.CreateCliCmd.func1({0x62af8a0, 0xc000c27890}, 0xc000c27890?)
        /app/Developer/health-map-db/cmd/importer/main.go:51 +0xe5
github.com/urfave/cli/v3.(*Command).Run(0xc000c06788, {0x62af8a0, 0xc000c27890}, {0xc000e1a0e0, 0x7, 0x7})
        /app/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:645 +0x1957
github.com/urfave/cli/v3.(*Command).Run(0xc000c06508, {0x62af8a0, 0xc000c27560}, {0xc000146000, 0x8, 0x8})
        /app/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:618 +0x18f4
main.main()
        /app/Developer/health-map-db/cmd/importer/main.go:25 +0x191
func main() {
	total := getTotal()
	bar := progressbar.NewOptions64(total,
		progressbar.OptionSetDescription("🤖 Importing practitioners"),
		progressbar.OptionSetWriter(os.Stderr),
		progressbar.OptionSetWidth(10),
		progressbar.OptionShowTotalBytes(false),
		progressbar.OptionFullWidth(),
		progressbar.OptionThrottle(65*time.Millisecond),
		progressbar.OptionShowCount(),
		progressbar.OptionSetMaxDetailRow(10),
		progressbar.OptionSpinnerType(14),
		progressbar.OptionOnCompletion(func() {
			fmt.Fprint(os.Stderr, "\n")
		}),
		progressbar.OptionSetRenderBlankState(true),
	)

	batchSize := 100

	for _, url := range urls {
		_ = bar.Add(batchSize)
	}

	_ = bar.Finish()
}
@FOXCraft40
Copy link

The issue comes from using progressbar.OptionSetMaxDetailRow(10) without actually calling bar.AddDetail(). This option enables multi-line rendering, but if you don't use it as intended, it understandably results in an index out-of-range error.

Removing the option fixes the issue since you're not using detail rows anyway.

@chengxilo
Copy link
Contributor

writeString(p.config, fmt.Sprintf("\u001B[%dB\r\u001B[%dC", p.config.maxDetailRow, len(p.state.details[len(p.state.details)-1])))

it's because this line. By default progress bar will get the length of the last detail and move the cursor back when finished so if there is not detail it cause index out of range. It's not stable enough so I am gonna try to fix it. Sorry about this bug.😵 Will pr for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants