GithubHelp home page GithubHelp logo

drsmithfr / go-console Goto Github PK

View Code? Open in Web Editor NEW
47.0 3.0 6.0 1.73 MB

GoConsole: the golang component that eases the creation of beautiful command line interfaces.

Home Page: https://drsmithfr.github.io/go-console/

License: The Unlicense

Go 99.83% Makefile 0.17%
go golang golang-library golang-package console input output

go-console's People

Contributors

artarts36 avatar drsmithfr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-console's Issues

Panic: nil pointer in getCellWidth: `t.output.Formatter()`

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?

Fluent API cause panic

Steps to reproduce bug:

  1. Type the following code:
    _ = go_console.NewCli()

Expected result:

  1. Cli is created

Current result:

  1. The following panic occurs:

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

Feature request: JSON / Struct to table print

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!!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.