drsmithfr / go-console Goto Github PK
View Code? Open in Web Editor NEWGoConsole: the golang component that eases the creation of beautiful command line interfaces.
Home Page: https://drsmithfr.github.io/go-console/
License: The Unlicense
GoConsole: the golang component that eases the creation of beautiful command line interfaces.
Home Page: https://drsmithfr.github.io/go-console/
License: The Unlicense
Hi, so I'm trying to use the new JSON to Table functionality, and for some reason, when building a script as a go_console.Command
I get a panic.
Here is an example:
func panicMain() {
script := go_console.Command{
Scripts: []*go_console.Script{
{
Name: "test",
Runner: func(cmd *go_console.Script) go_console.ExitCode {
tb := table.NewTable().SetParseMaxDepth(10).ParseJSON(getJsonBytes())
for _, i := range tb.GetRows().GetRows() {
fmt.Println(i)
}
rdr := table.NewRender(cmd.Output).SetContent(tb)
rdr.Render()
return go_console.ExitSuccess
},
},
},
}
script.Run()
}
func main() {
cmd := go_console.NewScript().Build()
jsonData := getJsonBytes()
tab := table.
NewTable().
ParseJSON(jsonData)
render := table.
NewRender(cmd.Output).
SetContent(tab)
render.Render()
}
func getJsonBytes() []byte {
str := `{
"type": "record"
}`
return []byte(str)
}
main
is directly taken from example and works fine
panicMain
has the following panic:
> go run .\main.go test
&{map[0:0xc0000666f0]}
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x517c5c]
goroutine 1 [running]:
github.com/DrSmithFr/go-console/table.(*TableRender).getCellWidth(0xc000076460, {0x5887e8?, 0xc00000a130?}, 0x0?)
C:/Users/Finicu/go/pkg/mod/github.com/!dr!smith!fr/[email protected]/table/table-render.go:859 +0x7c
github.com/DrSmithFr/go-console/table.(*TableRender).calculateColumnsWidth(0xc000076460, 0xc000123a50)
C:/Users/Finicu/go/pkg/mod/github.com/!dr!smith!fr/[email protected]/table/table-render.go:834 +0x246
github.com/DrSmithFr/go-console/table.(*TableRender).Render(0xc000076460)
C:/Users/Finicu/go/pkg/mod/github.com/!dr!smith!fr/[email protected]/table/table-render.go:202 +0x17a
main.main.func1(0xc000136000)
C:/Users/Finicu/GolandProjects/cli/main.go:20 +0x24c
github.com/DrSmithFr/go-console.(*Script).Build(0xc000136000)
C:/Users/Finicu/go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-script.go:184 +0x6f
github.com/DrSmithFr/go-console.(*Command).Run(0xc000123df0)
C:/Users/Finicu/go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-command.go:269 +0x57f
main.main()
C:/Users/Finicu/GolandProjects/cli/main.go:26 +0x105
exit status 2
When tracking down the stack trace, I arrive at this line: https://github.com/DrSmithFr/go-console/blob/master/table/table-render.go#L859 and indeed, printing t.output.Formatter()
seems to indicate it is nil.
I tried fixing this by manually setting script.Output = output.NewBufferedOutput(true, formatter.NewOutputFormatter())
before script.Run()
but still get the same panic. I'm not too sure if this is caused by the way my configuration is setup or is a bug.
I was wondering if Run
would be able to handle nil configs by itself and set some defaults? What do you think of that?
Steps to reproduce bug:
_ = go_console.NewCli()
Expected result:
Current result:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x533308]goroutine 1 [running]:
github.com/DrSmithFr/go-console.(*Cli).AddInputOption(0xc000188200, 0xc0000a70e0)
/home/efy/.go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-console.go:148 +0x48
github.com/DrSmithFr/go-console.(*Cli).addDefaultOptions(0x126c178?)
/home/efy/.go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-console.go:110 +0x85
github.com/DrSmithFr/go-console.CustomCli({0xdcb6e8?, 0xc0000000c0}, {0xdc8e18, 0xc000099340})
/home/efy/.go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-console.go:42 +0x4e
github.com/DrSmithFr/go-console.NewCli()
/home/efy/.go/pkg/mod/github.com/!dr!smith!fr/[email protected]/go-console.go:29 +0x51
Hello!
First of all, I want to say this is a fantastic package! The documentation is also amazing! Definitely going to use it in my upcoming project!
I have a feature request that could make this package even more great: Would it be possible to add support for easily rendering tables based on structs or JSON objects? An intuitive, reflective call like what's offered by https://github.com/lensesio/tableprinter lib would be amazing.
Here's a proposed solution that integrates seamlessly with the existing usage:
package main
import (
"github.com/DrSmithFr/go-console"
"github.com/DrSmithFr/go-console/table"
)
type Author struct {
Name string
Age int
}
type Book struct {
ISBN *string
Title *string
Author *Author
}
func main() {
cmd := go_console.NewScript().Build()
books := []Book{
{ISBN: ptrStr("99921-58-10-7"), Title: ptrStr("The Divine Comedy"), Author: &Author{Name: "Dante Alighieri", Age: 56}},
{ISBN: ptrStr("9971-5-0210-0"), Title: ptrStr("A Tale of Two Cities"), Author: &Author{Name: "Charles Dickens", Age: 58}},
{ISBN: ptrStr("960-425-059-0"), Title: ptrStr("The Lord of the Rings"), Author: &Author{Name: "J. R. R. Tolkien", Age: 81}},
{ISBN: ptrStr("80-902734-1-6"), Title: ptrStr("And Then There Were None"), Author: &Author{Name: "Agatha Christie", Age: 85}},
}
tab := table.
NewTable().
AddHeadersFromStructs(books).
AddRowsFromStructs(books)
render := table.
NewRender(cmd.Output).
SetContent(tab)
render.Render()
}
Once again, I love this package!!!!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.