Giter Club home page Giter Club logo

table's Introduction

Table

Generate tables. Either explicitly from headers and rows or automatically generate from any type.

table.Generate([]string{"Name", "Age"}, [][]string{{"John", "25"}, {"Jane", "23"}}).Print()
/*
+------+-----+
| Name | Age |
+------+-----+
| John |  25 |
| Jane |  23 |
+------+-----+
*/

For structs, maps, slices and others:

table.String([]Person{
    {"Alice", 23},
    {"Bob", 25},
}).Print()
/*
+-------+-----+
| Name  | Age |
+-------+-----+
| Alice |  23 |
| Bob   |  25 |
+-------+-----+
*/

With unicode table format and spreadsheet mode

table.StringWith(table.StyleUnicode, []Person{
    {"Alice", 23},
    {"Bob", 25},
}, table.WithSpreadsheet("#")).Print()
/*
╔═════╦═════════╦═══════╗
║  #  ║  Name   ║  Age  ║
╠═════╬═════════╬═══════╣
║  1  ║  Alice  ║   23  ║
║  2  ║  Bob    ║   25  ║
╚═════╩═════════╩═══════╝
*/

Using struct tags and options. Use the "table" tag (configurable) to control what value is used in the header for a field. Fields with tag value "-" will be skipped. Included and excluded fields can be explicitly specified with generalizer.Include and generalizer.Exclude or convenience functions table.Include and table.Exclude. The include and exclude options use the field name. If include is specified only fields in the list will be included, if exclude is specified those fields will not be included. Include is applied first and then exclude.

type User struct {
    ID           string
    DisplayName  string `table:"Display Name"`
    Password     string `table:"-"`
    CreatedAt    time.Time
}

table.String([]User{
    {"1", "John Doe", "johnspwd", time.Now()},
    {"2", "Jane Doe", "janespwd", time.Now()},
}, table.Exclude("DisplayName"), table.TimeFormat("2006-01-02 15:04:05")).Print()
/*
+----+---------------------+
| ID |      CreatedAt      |
+----+---------------------+
|  1 | 2020-01-02 20:30:09 |
|  2 | 2020-01-02 20:30:09 |
+----+---------------------+
*/

In the previous example "CreatedAt" was just the value of the header. Use the table.Params.HeaderFormatter to format header. table.AutoFormatHeader can be applied with table.WithAutoFormatHeader()

table.String([]User{
    {"1", "John Doe", "johnspwd", time.Now()},
    {"2", "Jane Doe", "janespwd", time.Now()},
},
    table.Exclude("DisplayName"),
    table.TimeFormat("2006-01-02 15:04:05"),
    table.WithAutoFormatHeader(),
).Print()
/*
+----+---------------------+
| ID |     Created At      |
+----+---------------------+
|  1 | 2020-01-02 20:30:09 |
|  2 | 2020-01-02 20:30:09 |
+----+---------------------+
*/

To reuse same configuration:

sc := table.New(
    table.Exclude("DisplayName"),
    table.TimeFormat("2006-01-02 15:04:05"),
    table.WithAutoFormatHeader(),
)

sc.String([]User{
    {"1", "John Doe", "johnspwd", time.Now()},
}).Print()

sc.String([]User{
    {"2", "Jane Doe", "janespwd", time.Now()},
}).Print()

/*
+----+---------------------+
| ID |     Created At      |
+----+---------------------+
|  1 | 2020-01-02 20:30:09 |
+----+---------------------+
+----+---------------------+
| ID |     Created At      |
+----+---------------------+
|  2 | 2020-01-02 20:30:09 |
+----+---------------------+
*/

table's People

Contributors

evenboee avatar

Watchers

 avatar

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.