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

Panic: integer divide by zero when setting a style #167

Closed
droslean opened this issue May 22, 2021 · 1 comment · Fixed by #168
Closed

Panic: integer divide by zero when setting a style #167

droslean opened this issue May 22, 2021 · 1 comment · Fixed by #168

Comments

@droslean
Copy link

droslean commented May 22, 2021

panic: runtime error: integer divide by zero

goroutine 1 [running]:
github.com/jedib0t/go-pretty/v6/text.RepeatAndTrim(0x0, 0x0, 0x5, 0x0, 0x0)
	/home/user/go/pkg/mod/github.com/jedib0t/go-pretty/v6@v6.2.1/text/string.go:110 +0x12d
github.com/jedib0t/go-pretty/v6/table.(*Table).initForRenderRowSeparator(0xc00018f5b0)
	/home/user/go/pkg/mod/github.com/jedib0t/go-pretty/v6@v6.2.1/table/table.go:746 +0x1a5
github.com/jedib0t/go-pretty/v6/table.(*Table).initForRender(0xc00018f5b0)
	/home/user/go/pkg/mod/github.com/jedib0t/go-pretty/v6@v6.2.1/table/table.go:569 +0x65
github.com/jedib0t/go-pretty/v6/table.(*Table).Render(0xc00018f5b0, 0xc000128640, 0x4)
	/home/user/go/pkg/mod/github.com/jedib0t/go-pretty/v6@v6.2.1/table/render.go:22 +0x45
main.main.func2(0xc000039140, 0x0, 0x0)

My code:

t := table.NewWriter()
style := table.Style{
	Name:    "my-style",
}
t.SetStyle(style)
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Name", "Address", "Phone", "Birthday"})

for i, info := range data.Items {
	t.AppendRows([]table.Row{{i, info.Name, info.Address, info.Phone, info.Birthday}})
}
t.AppendSeparator()
t.AppendFooter(table.Row{"Total", len(data.Items), "", ""})
t.Render()
@jedib0t
Copy link
Owner

jedib0t commented May 22, 2021

Hey thanks for reporting this. I've fixed the code that caused the panic and created a new tag v6.2.2 with the fix.

However, I should also note that if you want to customize the table styles, you have to do it completely instead of just setting the Name. Another alternative is to use an existing style with all the values, and then start modifying it to your liking.

// one approach
tw := table.NewWriter()
style := table.StyleDefault
style.Name = "my-style"
// more customizations
tw.SetStyle(style)

// another approach
table.Style().Name = "my-style"
// the call into Style() will init it with StyleDefault if there is no style set currently

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.

2 participants